Skip to content

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

Fix: Issue by copying or moving a file to a folder where the file with the same name already exists (#2107)

Stanimir146f92e1a918
diff --git a/src/renderer/components/LocationView.tsx b/src/renderer/components/LocationView.tsx
index 470cc4bf5..36aa957fd 100644
--- a/src/renderer/components/LocationView.tsx
+++ b/src/renderer/components/LocationView.tsx
@@ -44,6 +44,7 @@ import { useSelectedEntriesContext } from '-/hooks/useSelectedEntriesContext';
import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
import { CommonLocation } from '-/utils/CommonLocation';
import { useFileUploadDialogContext } from '-/components/dialogs/hooks/useFileUploadDialogContext';
+import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';
interface Props {
location: CommonLocation;
@@ -64,6 +65,8 @@ function LocationView(props: Props) {
setSelectedLocation,
setLocationDirectoryContextMenuAnchorEl,
} = useCurrentLocationContext();
+ const { handleEntryExist, openEntryExistDialog } =
+ useEntryExistDialogContext();
const { setSelectedEntries } = useSelectedEntriesContext();
const { currentLocationPath, openDirectory } = useDirectoryContentContext();
const { showNotification } = useNotificationContext();
@@ -71,12 +74,7 @@ function LocationView(props: Props) {
const dispatch: AppDispatch = useDispatch();
- const {
- location,
- hideDrawer,
- setEditLocationDialogOpened,
- setDeleteLocationDialogOpened,
- } = props;
+ const { location, hideDrawer } = props;
const isCloudLocation = location.type === locationType.TYPE_CLOUD;
const handleLocationIconClick = (
@@ -171,7 +169,15 @@ function LocationView(props: Props) {
console.log('uploadFiles', error);
});
} else if (targetLocation.type === locationType.TYPE_LOCAL) {
- moveFiles(arrPath, targetPath, targetLocation.uuid);
+ handleEntryExist(selectedEntries, targetPath).then((exist) => {
+ if (exist) {
+ openEntryExistDialog(exist, () => {
+ moveFiles(arrPath, targetPath, targetLocation.uuid);
+ });
+ } else {
+ moveFiles(arrPath, targetPath, targetLocation.uuid);
+ }
+ });
} else {
showNotification(t('Moving file not possible'), 'error', true);
return;
diff --git a/src/renderer/components/dialogs/ConfirmDialog.tsx b/src/renderer/components/dialogs/ConfirmDialog.tsx
index 869df2c9c..bdfbead4d 100644
--- a/src/renderer/components/dialogs/ConfirmDialog.tsx
+++ b/src/renderer/components/dialogs/ConfirmDialog.tsx
@@ -61,6 +61,7 @@ function ConfirmDialog(props: Props) {
onClose={onClose}
keepMounted
scroll="paper"
+ style={{ zIndex: 1301 }}
>
<DialogTitle style={{ cursor: 'move' }} id="draggable-dialog-title">
{props.title}
diff --git a/src/renderer/components/dialogs/MoveCopyFilesDialog.tsx b/src/renderer/components/dialogs/MoveCopyFilesDialog.tsx
index 065ce1047..79b1a8aee 100644
--- a/src/renderer/components/dialogs/MoveCopyFilesDialog.tsx
+++ b/src/renderer/components/dialogs/MoveCopyFilesDialog.tsx
@@ -21,7 +21,6 @@ import { actions as AppActions, AppDispatch } from '-/reducers/app';
import { TS } from '-/tagspaces.namespace';
import DirectoryListView from '-/components/DirectoryListView';
import AppConfig from '-/AppConfig';
-import ConfirmDialog from '-/components/dialogs/ConfirmDialog';
import { useTranslation } from 'react-i18next';
import { useDirectoryContentContext } from '-/hooks/useDirectoryContentContext';
import { useIOActionsContext } from '-/hooks/useIOActionsContext';
@@ -29,6 +28,7 @@ import { useSelectedEntriesContext } from '-/hooks/useSelectedEntriesContext';
import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
import { getDirProperties } from '-/services/utils-io';
import { useFileUploadDialogContext } from '-/components/dialogs/hooks/useFileUploadDialogContext';
+import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';
interface Props {
open: boolean;
@@ -42,6 +42,8 @@ function MoveCopyFilesDialog(props: Props) {
const dispatch: AppDispatch = useDispatch();
const { currentLocation } = useCurrentLocationContext();
const { currentDirectoryPath } = useDirectoryContentContext();
+ const { handleEntryExist, openEntryExistDialog } =
+ useEntryExistDialogContext();
const { copyFiles, copyDirs, moveFiles, moveDirs } = useIOActionsContext();
const { selectedEntries } = useSelectedEntriesContext();
const { openFileUploadDialog } = useFileUploadDialogContext();
@@ -49,8 +51,6 @@ function MoveCopyFilesDialog(props: Props) {
const [targetPath, setTargetPath] = useState(
currentDirectoryPath ? currentDirectoryPath : '',
);
- const isCopy = useRef<boolean>(true);
- const [entriesExistPath, setEntriesExistPath] = useState<string[]>(undefined);
const dirProp = useRef({});
const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
@@ -96,18 +96,6 @@ function MoveCopyFilesDialog(props: Props) {
}
}, []);
- /*useEffect(() => {
- handleValidation();
- }, [targetPath]);*/
-
- /*function handleValidation() {
- if (targetPath && targetPath.length > 0) {
- setDisableConfirmButton(false);
- } else {
- setDisableConfirmButton(true);
- }
- }*/
-
function getEntriesCount(dirPaths): Array<any> {
return dirPaths.map((path) => {
const currDirProp = dirProp.current[path];
@@ -123,31 +111,7 @@ function MoveCopyFilesDialog(props: Props) {
return total;*/
}
- function handleCopyMove(copy = true) {
- if (selectedFiles.length > 0) {
- currentLocation
- .checkFilesExistPromise(selectedFiles, targetPath)
- .then((exist) => handleEntryExist(copy, exist));
- }
- if (selectedDirs.length > 0) {
- currentLocation
- .checkDirsExistPromise(selectedDirs, targetPath)
- .then((exist) => handleEntryExist(copy, exist));
- }
- }
-
- function handleEntryExist(copy: boolean, exist: string[]) {
- if (exist && exist.length > 0) {
- isCopy.current = copy;
- setEntriesExistPath(exist);
- } else if (copy) {
- handleCopyFiles();
- } else {
- handleMoveFiles();
- }
- }
-
- function handleCopyFiles() {
+ function handleCopy() {
dispatch(AppActions.resetProgress());
openFileUploadDialog(undefined, 'copyEntriesTitle');
if (selectedFiles.length > 0) {
@@ -169,7 +133,7 @@ function MoveCopyFilesDialog(props: Props) {
dispatch(AppActions.onUploadProgress(progress, abort, fileName));
};
- function handleMoveFiles() {
+ function handleMove() {
if (selectedFiles.length > 0) {
moveFiles(
selectedFiles,
@@ -192,11 +156,24 @@ function MoveCopyFilesDialog(props: Props) {
onClose(true);
}
- function formatFileExist(entries) {
- if (entries !== undefined) {
- return entries.join(', ');
- }
- return '';
+ function copyMove(copy: boolean) {
+ handleEntryExist(allEntries.current, targetPath).then((exist) => {
+ if (exist) {
+ openEntryExistDialog(exist, () => {
+ if (copy) {
+ handleCopy();
+ } else {
+ handleMove();
+ }
+ });
+ } else {
+ if (copy) {
+ handleCopy();
+ } else {
+ handleMove();
+ }
+ }
+ });
}
const theme = useTheme();
@@ -275,40 +252,15 @@ function MoveCopyFilesDialog(props: Props) {
// AppConfig.isAndroid ||
targetPath === currentDirectoryPath
}
- onClick={() => handleCopyMove(false)}
+ onClick={() => copyMove(false)}
color="primary"
variant="contained"
>
{t('core:moveEntriesButton')}
</Button>
)}
- <ConfirmDialog
- open={entriesExistPath !== undefined}
- onClose={() => {
- setEntriesExistPath(undefined);
- }}
- title={t('core:confirm')}
- content={
- formatFileExist(entriesExistPath) +
- ' exist do you want to override it?'
- }
- confirmCallback={(result) => {
- if (result) {
- if (isCopy.current) {
- handleCopyFiles();
- } else {
- handleMoveFiles();
- }
- } else {
- setEntriesExistPath(undefined);
- }
- }}
- cancelDialogTID="cancelOverwriteByCopyMoveDialog"
- confirmDialogTID="confirmOverwriteByCopyMoveDialog"
- confirmDialogContentTID="confirmDialogContent"
- />
<Button
- onClick={() => handleCopyMove(true)}
+ onClick={() => copyMove(true)}
data-tid="confirmCopyFiles"
disabled={!targetPath || targetPath === currentDirectoryPath}
color="primary"
diff --git a/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx b/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx
index 8dcb5dd60..eec7448d5 100644
--- a/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx
+++ b/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx
@@ -46,6 +46,7 @@ import { TS } from '-/tagspaces.namespace';
import { useEditedEntryMetaContext } from '-/hooks/useEditedEntryMetaContext';
import { executePromisesInBatches } from '-/services/utils-io';
import AppConfig from '-/AppConfig';
+import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';
interface Props {
open: boolean;
@@ -59,6 +60,8 @@ function MoveOrCopyFilesDialog(props: Props) {
const theme = useTheme();
+ const { handleEntryExist, openEntryExistDialog } =
+ useEntryExistDialogContext();
const { setReflectMetaActions } = useEditedEntryMetaContext();
const { currentLocation } = useCurrentLocationContext();
const { moveFiles, copyFiles } = useIOActionsContext();
@@ -77,6 +80,25 @@ function MoveOrCopyFilesDialog(props: Props) {
});
}
+ function handleMove(filePaths: string[]) {
+ moveFiles(filePaths, currentDirectoryPath, currentLocation.uuid).then(
+ (success) => {
+ if (success) {
+ generateThumbs(
+ filePaths.map((targetPath) =>
+ joinPaths(
+ currentLocation?.getDirSeparator(),
+ currentDirectoryPath,
+ extractFileName(targetPath, currentLocation?.getDirSeparator()),
+ ),
+ ),
+ );
+ }
+ return true;
+ },
+ );
+ }
+
return (
<Dialog
open={open}
@@ -125,28 +147,17 @@ function MoveOrCopyFilesDialog(props: Props) {
<Button
onClick={() => {
if (selectedFiles) {
- const filePaths = selectedFiles.map((file) => file.path);
- moveFiles(
- filePaths,
- currentDirectoryPath,
- currentLocation.uuid,
- ).then((success) => {
- if (success) {
- generateThumbs(
- filePaths.map((targetPath) =>
- joinPaths(
- currentLocation?.getDirSeparator(),
- currentDirectoryPath,
- extractFileName(
- targetPath,
- currentLocation?.getDirSeparator(),
- ),
- ),
- ),
- );
- }
- return true;
- });
+ handleEntryExist(selectedFiles, currentDirectoryPath).then(
+ (exist) => {
+ if (exist) {
+ openEntryExistDialog(exist, () => {
+ handleMove(selectedFiles.map((file) => file.path));
+ });
+ } else {
+ handleMove(selectedFiles.map((file) => file.path));
+ }
+ },
+ );
}
onClose();
}}
diff --git a/src/renderer/components/dialogs/hooks/EntryExistDialogContextProvider.tsx b/src/renderer/components/dialogs/hooks/EntryExistDialogContextProvider.tsx
new file mode 100644
index 000000000..1715ce1bb
--- /dev/null
+++ b/src/renderer/components/dialogs/hooks/EntryExistDialogContextProvider.tsx
@@ -0,0 +1,146 @@
+/**
+ * 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, { createContext, useMemo, useReducer, useRef } from 'react';
+import { useTranslation } from 'react-i18next';
+import ConfirmDialog from '-/components/dialogs/ConfirmDialog';
+import { TS } from '-/tagspaces.namespace';
+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
+
+type EntryExistDialogContextData = {
+ handleEntryExist: (
+ entries: TS.FileSystemEntry[],
+ targetPath: string,
+ ) => Promise<string[]>;
+ openEntryExistDialog: (
+ existPath: string[],
+ confirmCallback: () => void,
+ ) => void;
+};
+
+export const EntryExistDialogContext =
+ createContext<EntryExistDialogContextData>({
+ handleEntryExist: undefined,
+ openEntryExistDialog: undefined,
+ });
+
+export type EntryExistDialogContextProviderProps = {
+ children: React.ReactNode;
+};
+
+export const EntryExistDialogContextProvider = ({
+ children,
+}: EntryExistDialogContextProviderProps) => {
+ const { t } = useTranslation();
+ const { currentLocation } = useCurrentLocationContext();
+ const confirmOverride = useRef<() => void>(undefined);
+ const entriesExistPath = useRef<string[]>([]);
+
+ const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
+
+ async function handleEntryExist(
+ entries: TS.FileSystemEntry[],
+ targetPath: string,
+ ): Promise<string[]> {
+ if (entries) {
+ let arrExist = [];
+ const selectedFiles = entries
+ .filter((fsEntry) => fsEntry.isFile)
+ .map((fsentry) => fsentry.path);
+ if (selectedFiles.length > 0) {
+ const exist = await currentLocation.checkFilesExistPromise(
+ selectedFiles,
+ targetPath,
+ );
+ if (exist.length > 0) {
+ arrExist.push(...exist);
+ }
+ }
+ const selectedDirs = entries
+ .filter((fsEntry) => !fsEntry.isFile)
+ .map((fsentry) => fsentry.path);
+
+ if (selectedDirs.length > 0) {
+ const exist = await currentLocation.checkDirsExistPromise(
+ selectedDirs,
+ targetPath,
+ );
+ if (exist.length > 0) {
+ arrExist.push(...exist);
+ }
+ }
+ if (arrExist.length > 0) {
+ return arrExist;
+ }
+ }
+ return Promise.resolve(undefined);
+ }
+
+ function openDialog(existPath: string[], confirmCallback: () => void) {
+ confirmOverride.current = confirmCallback;
+ entriesExistPath.current = existPath;
+ forceUpdate();
+ }
+
+ function closeDialog() {
+ entriesExistPath.current = [];
+ forceUpdate();
+ }
+
+ function formatFileExist(entries) {
+ if (entries !== undefined) {
+ return entries.join(', ');
+ }
+ return '';
+ }
+
+ const context = useMemo(() => {
+ return {
+ handleEntryExist: handleEntryExist,
+ openEntryExistDialog: openDialog,
+ closeEntryExistDialog: closeDialog,
+ };
+ }, [currentLocation]);
+
+ return (
+ <EntryExistDialogContext.Provider value={context}>
+ <ConfirmDialog
+ open={entriesExistPath.current.length > 0}
+ onClose={() => {
+ closeDialog();
+ }}
+ title={t('core:confirm')}
+ content={
+ formatFileExist(entriesExistPath.current) +
+ ' exist do you want to override it?'
+ }
+ confirmCallback={(result) => {
+ if (result) {
+ confirmOverride.current();
+ } else {
+ entriesExistPath.current = [];
+ }
+ }}
+ cancelDialogTID="cancelOverwriteByCopyMoveDialog"
+ confirmDialogTID="confirmOverwriteByCopyMoveDialog"
+ confirmDialogContentTID="confirmDialogContent"
+ />
+ {children}
+ </EntryExistDialogContext.Provider>
+ );
+};
diff --git a/src/renderer/components/dialogs/hooks/useEntryExistDialogContext.tsx b/src/renderer/components/dialogs/hooks/useEntryExistDialogContext.tsx
new file mode 100644
index 000000000..0df0bd582
--- /dev/null
+++ b/src/renderer/components/dialogs/hooks/useEntryExistDialogContext.tsx
@@ -0,0 +1,6 @@
+import { useContext } from 'react';
+
+import { EntryExistDialogContext } from '-/components/dialogs/hooks/EntryExistDialogContextProvider';
+
+export const useEntryExistDialogContext = () =>
+ useContext(EntryExistDialogContext);
diff --git a/src/renderer/containers/DialogsRoot.tsx b/src/renderer/containers/DialogsRoot.tsx
index 0ca8c31e3..56d426e0c 100644
--- a/src/renderer/containers/DialogsRoot.tsx
+++ b/src/renderer/containers/DialogsRoot.tsx
@@ -18,6 +18,7 @@
import React from 'react';
import { FileUploadDialogContextProvider } from '-/components/dialogs/hooks/FileUploadDialogContextProvider';
+import { EntryExistDialogContextProvider } from '-/components/dialogs/hooks/EntryExistDialogContextProvider';
export type DialogsRootProps = {
children: React.ReactNode;
@@ -25,9 +26,11 @@ export type DialogsRootProps = {
function DialogsRoot({ children }: DialogsRootProps) {
return (
- <FileUploadDialogContextProvider>
- {children}
- </FileUploadDialogContextProvider>
+ <EntryExistDialogContextProvider>
+ <FileUploadDialogContextProvider>
+ {children}
+ </FileUploadDialogContextProvider>
+ </EntryExistDialogContextProvider>
);
}
diff --git a/src/renderer/perspectives/common/main-container.tsx b/src/renderer/perspectives/common/main-container.tsx
index 1bb530c48..536877ecd 100644
--- a/src/renderer/perspectives/common/main-container.tsx
+++ b/src/renderer/perspectives/common/main-container.tsx
@@ -5,6 +5,7 @@ import DragItemTypes from '-/components/DragItemTypes';
import React from 'react';
import AppConfig from '-/AppConfig';
import TagDropContainer from '-/components/TagDropContainer';
+import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';
export const fileOperationsEnabled = (selectedEntries) => {
let selectionContainsDirectories = false;
@@ -68,6 +69,8 @@ export const renderCell = (
onProgress?,
reflect?: boolean,
) => Promise<boolean>,
+ handleEntryExist,
+ openEntryExistDialog,
clearSelection: () => void,
isLast?: boolean,
) => {
@@ -249,8 +252,15 @@ export const renderCell = (
arrPath = [item.path];
}
console.log('Dropped files: ' + item.path);
- moveFiles(arrPath, item.targetPath, currentLocation.uuid);
- //clearSelection();
+ handleEntryExist(item.selectedEntries, item.targetPath).then((exist) => {
+ if (exist) {
+ openEntryExistDialog(exist, () => {
+ moveFiles(arrPath, item.targetPath, currentLocation.uuid);
+ });
+ } else {
+ moveFiles(arrPath, item.targetPath, currentLocation.uuid);
+ }
+ });
}
};
diff --git a/src/renderer/perspectives/grid/components/GridPagination.tsx b/src/renderer/perspectives/grid/components/GridPagination.tsx
index 2c74d30e0..cc92bcd31 100644
--- a/src/renderer/perspectives/grid/components/GridPagination.tsx
+++ b/src/renderer/perspectives/grid/components/GridPagination.tsx
@@ -44,6 +44,7 @@ import { useSelectedEntriesContext } from '-/hooks/useSelectedEntriesContext';
import { usePerspectiveSettingsContext } from '-/hooks/usePerspectiveSettingsContext';
import GridCellsContainer from './GridCellsContainer';
import { useSortedDirContext } from '-/perspectives/grid/hooks/useSortedDirContext';
+import { useEntryExistDialogContext } from '-/components/dialogs/hooks/useEntryExistDialogContext';
interface Props {
directories: Array<TS.FileSystemEntry>;
@@ -93,6 +94,8 @@ function GridPagination(props: Props) {
gridPageLimit,
singleClickAction,
} = usePerspectiveSettingsContext();
+ const { handleEntryExist, openEntryExistDialog } =
+ useEntryExistDialogContext();
const { lastSelectedEntryPath } = useSelectedEntriesContext();
const { openEntryInternal } = useOpenedEntryContext();
const { moveFiles, openFileNatively } = useIOActionsContext();
@@ -338,6 +341,8 @@ function GridPagination(props: Props) {
setDirContextMenuAnchorEl,
showNotification,
moveFiles,
+ handleEntryExist,
+ openEntryExistDialog,
clearSelection,
),
)}
@@ -362,6 +367,8 @@ function GridPagination(props: Props) {
setDirContextMenuAnchorEl,
showNotification,
moveFiles,
+ handleEntryExist,
+ openEntryExistDialog,
clearSelection,
index === dArray.length - 1,
),