Skip to content

TagSpaces is an offline, open source, document manager with tagging support

TSN555 - Import files broken on android (#1309)

Stanimir8ea021138a5e
diff --git a/app/components/FileUploadContainer.tsx b/app/components/FileUploadContainer.tsx
new file mode 100644
index 000000000..35f8b37ae
--- /dev/null
+++ b/app/components/FileUploadContainer.tsx
@@ -0,0 +1,132 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2017-present TagSpaces UG (haftungsbeschraenkt)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License (version 3) as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+import React, { forwardRef, Ref, useImperativeHandle, useRef } from 'react';
+import { Progress } from 'aws-sdk/clients/s3';
+import { FileSystemEntry } from '-/services/utils-io';
+
+interface Props {
+ directoryPath: string;
+ uploadFilesAPI: (
+ files: Array<File>,
+ destination: string,
+ onUploadProgress?: (progress: Progress, response: any) => void
+ ) => any;
+ reflectCreateEntries: (fsEntries: Array<FileSystemEntry>) => void;
+ onUploadProgress: (progress: Progress, response: any) => void;
+ toggleUploadDialog: () => void;
+ toggleProgressDialog: () => void;
+ resetProgress: () => void;
+}
+
+export interface FileUploadContainerRef {
+ onFileUpload: () => void;
+}
+
+const FileUploadContainer = forwardRef(
+ (props: Props, ref: Ref<FileUploadContainerRef>) => {
+ useImperativeHandle(ref, () => ({
+ onFileUpload() {
+ /* if (AppConfig.isCordovaAndroid) {
+ PlatformIO.selectFileDialog()
+ .then(file => {
+ console.log('file', file.uri);
+ return [file.uri];
+ })
+ .then(arrFiles => {
+ props
+ .uploadFiles(
+ arrFiles,
+ props.directoryPath,
+ props.onUploadProgress
+ )
+ .then(fsEntries => {
+ props.reflectCreateEntries(fsEntries);
+ return true;
+ })
+ .catch(error => {
+ console.log('uploadFiles', error);
+ });
+ props.toggleUploadDialog();
+ return true;
+ })
+ .catch(error => {
+ console.log('uploadFiles', error);
+ });
+ } else { */
+ fileInput.current.click();
+ // }
+ }
+ }));
+
+ /* if (AppConfig.isCordovaAndroid) {
+ return null;
+ } */
+
+ const fileInput = useRef<HTMLInputElement>(null);
+
+ function handleFileInputChange(selection: any) {
+ // console.log("Selected File: "+JSON.stringify(selection.currentTarget.files[0]));
+ // const file = selection.currentTarget.files[0];
+ props.resetProgress();
+
+ props
+ .uploadFilesAPI(
+ Array.from(selection.currentTarget.files),
+ props.directoryPath,
+ props.onUploadProgress
+ )
+ .then(fsEntries => {
+ props.reflectCreateEntries(fsEntries);
+ return true;
+ })
+ .catch(error => {
+ console.log('uploadFiles', error);
+ });
+ props.toggleUploadDialog();
+ }
+
+ return (
+ <input
+ style={{ display: 'none' }}
+ ref={fileInput}
+ accept="*"
+ type="file"
+ multiple
+ onChange={handleFileInputChange}
+ />
+ );
+ }
+);
+
+/* function mapDispatchToProps(dispatch) {
+ return bindActionCreators(
+ {
+ onUploadProgress: AppActions.onUploadProgress,
+ toggleUploadDialog: AppActions.toggleUploadDialog,
+ toggleProgressDialog: AppActions.toggleProgressDialog,
+ resetProgress: AppActions.resetProgress,
+ reflectCreateEntries: AppActions.reflectCreateEntries,
+ uploadFilesAPI: IOActions.uploadFilesAPI,
+ uploadFiles: IOActions.uploadFiles
+ },
+ dispatch
+ );
+} */
+
+export default FileUploadContainer;
diff --git a/app/components/menus/DirectoryMenu.tsx b/app/components/menus/DirectoryMenu.tsx
index 4a892a123..a6556b15a 100644
--- a/app/components/menus/DirectoryMenu.tsx
+++ b/app/components/menus/DirectoryMenu.tsx
@@ -16,7 +16,7 @@
*
*/
-import React, { useState } from 'react';
+import React, { useRef, useState } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import ListItemIcon from '@material-ui/core/ListItemIcon';
@@ -59,6 +59,9 @@ import IOActions from '-/reducers/io-actions';
import { Tag } from '-/reducers/taglibrary';
import TaggingActions from '-/reducers/tagging-actions';
import { FileSystemEntry, getAllPropertiesPromise } from '-/services/utils-io';
+import FileUploadContainer, {
+ FileUploadContainerRef
+} from '-/components/FileUploadContainer';
interface Props {
open: boolean;
@@ -99,7 +102,8 @@ interface Props {
}
const DirectoryMenu = (props: Props) => {
- let fileInput; // Object | null;
+ // let fileInput; // Object | null;
+ const fileUploadContainerRef = useRef<FileUploadContainerRef>(null);
const [
isCreateDirectoryDialogOpened,
@@ -192,7 +196,7 @@ const DirectoryMenu = (props: Props) => {
function addExistingFile() {
props.onClose();
- fileInput.click();
+ fileUploadContainerRef.current.onFileUpload();
}
function importMacTags() {
@@ -312,88 +316,6 @@ Do you want to continue?`)
});
}
- function handleFileInputChange(selection: any) {
- // console.log("Selected File: "+JSON.stringify(selection.currentTarget.files[0]));
- // const file = selection.currentTarget.files[0];
- props.resetProgress();
-
- props
- .uploadFilesAPI(
- Array.from(selection.currentTarget.files),
- props.directoryPath,
- props.onUploadProgress
- )
- .then(fsEntries => {
- props.reflectCreateEntries(fsEntries);
- return true;
- })
- .catch(error => {
- console.log('uploadFiles', error);
- });
- props.toggleUploadDialog();
- /*
- const filePath =
- normalizePath(props.directoryPath) +
- PlatformIO.getDirSeparator() +
- decodeURIComponent(file.name);
- const reader = new FileReader();
- reader.onload = (event: any) => {
- // console.log('Content on file read complete: ' + JSON.stringify(event));
- // change name for ios fakepath
- // if (AppConfig.isCordovaiOS) {
- // const fileExt = extractFileExtension(addFileInputName);
- // addFileInputName = AppConfig.beginTagContainer + formatDateTime4Tag(new Date(), true) + AppConfig.endTagContainer + fileExt;
- // }
- // TODO event.currentTarget.result is ArrayBuffer
- // Sample call from PRO version using content = Utils.base64ToArrayBuffer(baseString);
- PlatformIO.getPropertiesPromise(filePath)
- .then(entryProps => {
- if (entryProps) {
- props.showNotification(
- 'File with the same name already exist, importing skipped!',
- 'warning',
- true
- );
- } else {
- PlatformIO.saveBinaryFilePromise(
- filePath,
- event.currentTarget.result,
- true
- )
- .then(() => {
- props.showNotification(
- 'File ' + filePath + ' successfully imported.',
- 'default',
- true
- );
- props.reflectCreateEntry(filePath, true);
- return true;
- })
- .catch(error => {
- // TODO showAlertDialog("Saving " + filePath + " failed.");
- console.error('Save to file ' + filePath + ' failed ' + error);
- props.showNotification(
- 'Importing file ' + filePath + ' failed.',
- 'error',
- true
- );
- return true;
- });
- }
- return true;
- })
- .catch(err => {
- console.log('Error getting properties ' + err);
- });
- };
-
- if (AppConfig.isCordova) {
- reader.readAsDataURL(file);
- } else {
- reader.readAsArrayBuffer(file);
- } */
- }
-
function getDirPath(dirPath: string) {
const fileName = extractFileName(dirPath, PlatformIO.getDirSeparator());
return props.directoryPath && fileName
@@ -597,15 +519,15 @@ Do you want to continue?`)
<ListItemText primary={i18n.t('core:directoryPropertiesTitle')} />
</MenuItem>
</Menu>
- <input
- style={{ display: 'none' }}
- ref={input => {
- fileInput = input;
- }}
- accept="*"
- type="file"
- multiple
- onChange={handleFileInputChange}
+ <FileUploadContainer
+ ref={fileUploadContainerRef}
+ directoryPath={props.directoryPath}
+ onUploadProgress={props.onUploadProgress}
+ toggleUploadDialog={props.toggleUploadDialog}
+ toggleProgressDialog={props.toggleProgressDialog}
+ resetProgress={props.resetProgress}
+ reflectCreateEntries={props.reflectCreateEntries}
+ uploadFilesAPI={props.uploadFilesAPI}
/>
</div>
);
diff --git a/app/reducers/io-actions.ts b/app/reducers/io-actions.ts
index f2603c64e..5faaef13f 100644
--- a/app/reducers/io-actions.ts
+++ b/app/reducers/io-actions.ts
@@ -179,7 +179,7 @@ const actions = {
targetPath: string,
onUploadProgress?: (progress: Progress, response: any) => void
) => (dispatch: (actions: Object) => void) => {
- if (AppConfig.isElectron || AppConfig.isCordova) {
+ if (AppConfig.isElectron || AppConfig.isCordovaiOS) {
const arrFiles = [];
for (let i = 0; i < files.length; i += 1) {
arrFiles.push(files[i].path);