Skip to content

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

Upload files api opimize (#1962)

Stanimir67cb3de89142
diff --git a/app/components/FileUploadContainer.tsx b/app/components/FileUploadContainer.tsx
index 381974ca7..a4f1a1f7b 100644
--- a/app/components/FileUploadContainer.tsx
+++ b/app/components/FileUploadContainer.tsx
@@ -17,21 +17,15 @@
*/
import React, { forwardRef, Ref, useImperativeHandle, useRef } from 'react';
-import { Progress } from 'aws-sdk/clients/s3';
+import { useDispatch } from 'react-redux';
import { TS } from '-/tagspaces.namespace';
+import { actions as AppActions, AppDispatch } from '-/reducers/app';
+import IOActions from '-/reducers/io-actions';
interface Props {
+ id: string;
directoryPath: string;
- uploadFilesAPI: (
- files: Array<File>,
- destination: string,
- onUploadProgress?: (progress: Progress, response: any) => void
- ) => any;
- reflectCreateEntries: (fsEntries: Array<TS.FileSystemEntry>) => void;
- onUploadProgress: (progress: Progress, response: any) => void;
- toggleUploadDialog: () => void;
- toggleProgressDialog: () => void;
- resetProgress: () => void;
+ //toggleProgressDialog: () => void;
}
export interface FileUploadContainerRef {
@@ -40,6 +34,13 @@ export interface FileUploadContainerRef {
const FileUploadContainer = forwardRef(
(props: Props, ref: Ref<FileUploadContainerRef>) => {
+ const dispatch: AppDispatch = useDispatch();
+ const { id, directoryPath } = props;
+
+ const onUploadProgress = (progress, abort, fileName) => {
+ dispatch(AppActions.onUploadProgress(progress, abort, fileName));
+ };
+
useImperativeHandle(ref, () => ({
onFileUpload() {
/* if (AppConfig.isCordovaAndroid) {
@@ -83,16 +84,17 @@ const FileUploadContainer = forwardRef(
function handleFileInputChange(selection: any) {
// console.log("Selected File: "+JSON.stringify(selection.currentTarget.files[0]));
// const file = selection.currentTarget.files[0];
- props.resetProgress();
- props.toggleUploadDialog();
- props
- .uploadFilesAPI(
+ dispatch(AppActions.resetProgress());
+ dispatch(AppActions.toggleUploadDialog());
+ dispatch(
+ IOActions.uploadFilesAPI(
Array.from(selection.currentTarget.files),
- props.directoryPath,
- props.onUploadProgress
+ directoryPath,
+ onUploadProgress
)
- .then(fsEntries => {
- props.reflectCreateEntries(fsEntries);
+ )
+ .then((fsEntries: Array<TS.FileSystemEntry>) => {
+ dispatch(AppActions.reflectCreateEntries(fsEntries));
return true;
})
.catch(error => {
@@ -102,6 +104,7 @@ const FileUploadContainer = forwardRef(
return (
<input
+ id={id}
style={{ display: 'none' }}
ref={fileInput}
accept="*"
@@ -113,19 +116,4 @@ const FileUploadContainer = forwardRef(
}
);
-/* 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/dialogs/NewEntryDialog.tsx b/app/components/dialogs/NewEntryDialog.tsx
index 133f63afa..752a214a8 100644
--- a/app/components/dialogs/NewEntryDialog.tsx
+++ b/app/components/dialogs/NewEntryDialog.tsx
@@ -70,7 +70,7 @@ function NewEntryDialog(props: Props) {
>
<Accordion style={{ marginTop: 5 }} defaultExpanded>
<AccordionDetails style={{ paddingTop: 16 }}>
- <CreateFile onClose={onClose} />
+ <CreateFile onClose={onClose} tidPrefix="entry" />
</AccordionDetails>
</Accordion>
<Accordion>
@@ -82,7 +82,7 @@ function NewEntryDialog(props: Props) {
<Typography>{i18n.t('core:moreOperations')}</Typography>
</AccordionSummary>
<AccordionDetails>
- <CreateDirectory onClose={onClose} />
+ <CreateDirectory onClose={onClose} tidPrefix="entry" />
</AccordionDetails>
</Accordion>
<TargetPath />
diff --git a/app/components/dialogs/components/CreateDirectory.tsx b/app/components/dialogs/components/CreateDirectory.tsx
index 463d616ce..2c0954818 100644
--- a/app/components/dialogs/components/CreateDirectory.tsx
+++ b/app/components/dialogs/components/CreateDirectory.tsx
@@ -37,6 +37,9 @@ import Tooltip from '-/components/Tooltip';
import TextField from '@mui/material/TextField';
import { useTargetPathContext } from '-/components/dialogs/hooks/useTargetPathContext';
import Typography from '@mui/material/Typography';
+import FileUploadContainer, {
+ FileUploadContainerRef
+} from '-/components/FileUploadContainer';
const PREFIX = 'CreateDirectory';
@@ -53,16 +56,17 @@ const Root = styled('div')(() => ({
interface Props {
onClose: () => void;
+ tidPrefix?: string;
}
function CreateDirectory(props: Props) {
- const { onClose } = props;
+ const { onClose, tidPrefix } = props;
+ const fileUploadContainerRef = useRef<FileUploadContainerRef>(null);
const dispatch: AppDispatch = useDispatch();
const { targetDirectoryPath } = useTargetPathContext();
const fileUrl = useRef<string>();
const [invalidURL, setInvalidURL] = useState<boolean>(false);
- let fileInput: HTMLInputElement;
const noSuitableLocation = !targetDirectoryPath;
@@ -74,33 +78,13 @@ function CreateDirectory(props: Props) {
function addFile() {
// loadLocation();
- fileInput.click();
+ fileUploadContainerRef.current.onFileUpload();
+ onClose();
}
const onUploadProgress = (progress, abort, fileName) => {
dispatch(AppActions.onUploadProgress(progress, abort, fileName));
};
- function handleFileInputChange(selection: any) {
- // console.log("Selected File: "+JSON.stringify(selection.currentTarget.files[0]));
- // const file = selection.currentTarget.files[0];
- dispatch(AppActions.resetProgress());
- dispatch(AppActions.toggleUploadDialog());
- dispatch(
- IOActions.uploadFilesAPI(
- Array.from(selection.currentTarget.files),
- targetDirectoryPath,
- onUploadProgress
- )
- )
- .then((fsEntries: Array<TS.FileSystemEntry>) => {
- dispatch(AppActions.reflectCreateEntries(fsEntries));
- return true;
- })
- .catch(error => {
- console.log('uploadFiles', error);
- });
- onClose();
- }
const handleUrlChange = (event: React.ChangeEvent<HTMLInputElement>) => {
fileUrl.current = event.target.value;
@@ -180,6 +164,13 @@ function CreateDirectory(props: Props) {
}
}
+ function tid(tid) {
+ if (tidPrefix) {
+ return tidPrefix + tid;
+ }
+ return tid;
+ }
+
return (
<Root>
<Grid item xs={12}>
@@ -190,7 +181,7 @@ function CreateDirectory(props: Props) {
dispatch(AppActions.toggleLocationDialog());
}}
className={classes.createButton}
- data-tid="createLocationButton"
+ data-tid={tid('createLocationButton')}
>
<Tooltip title={i18n.t('createLocationTitle')}>
<Typography variant="button" display="block" gutterBottom>
@@ -207,7 +198,7 @@ function CreateDirectory(props: Props) {
dispatch(AppActions.toggleCreateDirectoryDialog());
}}
className={classes.createButton}
- data-tid="newSubDirectory"
+ data-tid={tid('newSubDirTID')}
disabled={noSuitableLocation}
>
{i18n.t('core:newSubdirectory')}
@@ -219,7 +210,7 @@ function CreateDirectory(props: Props) {
variant="outlined"
onClick={addFile}
className={classes.createButton}
- data-tid="addFilesButton"
+ data-tid={tid('addFilesButton')}
disabled={noSuitableLocation}
>
{i18n.t('addFiles')}
@@ -233,7 +224,7 @@ function CreateDirectory(props: Props) {
margin="dense"
name="name"
fullWidth={true}
- data-tid="newUrlTID"
+ data-tid={tid('newUrlTID')}
onKeyDown={event => {
if (event.key === 'Enter') {
downloadURL();
@@ -250,7 +241,7 @@ function CreateDirectory(props: Props) {
}}
>
<Button
- data-tid="cancelRenameEntryTID"
+ data-tid={tid('cancelRenameEntryTID')}
className={classes.createButton}
onClick={() => downloadURL()}
>
@@ -258,15 +249,10 @@ function CreateDirectory(props: Props) {
</Button>
</ButtonGroup>
</Grid>
- <input
- style={{ display: 'none' }}
- ref={input => {
- fileInput = input;
- }}
- accept="*"
- type="file"
- multiple
- onChange={handleFileInputChange}
+ <FileUploadContainer
+ id="createDirId"
+ ref={fileUploadContainerRef}
+ directoryPath={targetDirectoryPath}
/>
</Root>
);
diff --git a/app/components/dialogs/components/CreateFile.tsx b/app/components/dialogs/components/CreateFile.tsx
index 5b0a0563d..a2ee5315e 100644
--- a/app/components/dialogs/components/CreateFile.tsx
+++ b/app/components/dialogs/components/CreateFile.tsx
@@ -56,10 +56,11 @@ const StyledGrid = styled(Grid)(() => ({
interface Props {
//open: boolean;
onClose: () => void;
+ tidPrefix?: string;
}
function CreateFile(props: Props) {
- const { onClose } = props;
+ const { onClose, tidPrefix } = props;
const dispatch: AppDispatch = useDispatch();
const firstRWLocation = useSelector(getFirstRWLocation);
@@ -78,6 +79,13 @@ function CreateFile(props: Props) {
const noSuitableLocation = !targetDirectoryPath;
+ function tid(tid) {
+ if (tidPrefix) {
+ return tidPrefix + tid;
+ }
+ return tid;
+ }
+
function loadLocation() {
if (!currentDirectoryPath && firstRWLocation) {
dispatch(AppActions.openLocation(firstRWLocation));
@@ -186,7 +194,7 @@ function CreateFile(props: Props) {
defaultValue={fileName.current}
disabled={noSuitableLocation}
fullWidth={true}
- data-tid="newEntryDialogInputTID"
+ data-tid={tid('newEntryDialogInputTID')}
/>
{inputError && (
<FormHelperText>{i18n.t('core:fileNameHelp')}</FormHelperText>
@@ -204,7 +212,7 @@ function CreateFile(props: Props) {
// variant="contained"
onClick={createMarkdownFile}
className={classes.createButton}
- data-tid="createMarkdownButton"
+ data-tid={tid('createMarkdownButton')}
disabled={noSuitableLocation}
>
<Tooltip title={i18n.t('createMarkdownTitle')}>
@@ -222,7 +230,7 @@ function CreateFile(props: Props) {
// variant="contained"
onClick={createRichTextFile}
className={classes.createButton}
- data-tid="createRichTextFileButton"
+ data-tid={tid('createRichTextFileButton')}
disabled={noSuitableLocation}
>
<Tooltip title={i18n.t('createNoteTitle')}>
@@ -235,7 +243,7 @@ function CreateFile(props: Props) {
// variant="contained"
onClick={createTextFile}
className={classes.createButton}
- data-tid="createTextFileButton"
+ data-tid={tid('createTextFileButton')}
disabled={noSuitableLocation}
>
<Tooltip title={i18n.t('createTextFileTitle')}>
diff --git a/app/components/menus/DirectoryMenu.tsx b/app/components/menus/DirectoryMenu.tsx
index 604889ae4..1df51dd5a 100644
--- a/app/components/menus/DirectoryMenu.tsx
+++ b/app/components/menus/DirectoryMenu.tsx
@@ -39,7 +39,6 @@ import {
getSelectedEntries,
isReadOnlyMode
} from '-/reducers/app';
-import IOActions from '-/reducers/io-actions';
import TaggingActions from '-/reducers/tagging-actions';
import FileUploadContainer, {
FileUploadContainerRef
@@ -119,30 +118,10 @@ function DirectoryMenu(props: Props) {
dispatch(AppActions.toggleNewFileDialog());
};
- const uploadFilesAPI = (files, destination, onUploadProgress) => {
- dispatch(IOActions.uploadFilesAPI(files, destination, onUploadProgress));
- };
-
- const reflectCreateEntries = fsEntries => {
- dispatch(AppActions.reflectCreateEntries(fsEntries));
- };
-
- const onUploadProgress = (progress, response) => {
- dispatch(AppActions.onUploadProgress(progress, response));
- };
-
- const toggleUploadDialog = () => {
- dispatch(AppActions.toggleUploadDialog());
- };
-
const toggleProgressDialog = () => {
dispatch(AppActions.toggleProgressDialog());
};
- const resetProgress = () => {
- dispatch(AppActions.resetProgress());
- };
-
const addTags = (paths, tags, updateIndex) => {
dispatch(TaggingActions.addTags(paths, tags, updateIndex));
};
@@ -483,14 +462,9 @@ Do you want to continue?`)
{menuItems}
</Menu>
<FileUploadContainer
+ id="dirMenuId"
ref={fileUploadContainerRef}
directoryPath={directoryPath}
- onUploadProgress={onUploadProgress}
- toggleUploadDialog={toggleUploadDialog}
- toggleProgressDialog={toggleProgressDialog}
- resetProgress={resetProgress}
- reflectCreateEntries={reflectCreateEntries}
- uploadFilesAPI={uploadFilesAPI}
/>
</div>
);
diff --git a/app/containers/MainPage.tsx b/app/containers/MainPage.tsx
index 470ac6fe6..e6b57a655 100644
--- a/app/containers/MainPage.tsx
+++ b/app/containers/MainPage.tsx
@@ -799,12 +799,10 @@ function MainPage(props: Props) {
reflect={props.isCreateDirectoryOpened?.reflect}
/>
)}
- {props.isNewEntryDialogOpened && (
- <NewEntryDialog
- open={props.isNewEntryDialogOpened}
- onClose={toggleNewEntryDialog}
- />
- )}
+ <NewEntryDialog
+ open={props.isNewEntryDialogOpened}
+ onClose={toggleNewEntryDialog}
+ />
{props.isNewFileDialogOpened && (
<NewFileDialog
open={props.isNewFileDialogOpened}