Skip to content

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

fix Local file is directly overwritten by dnd from desktop (#2122)

Stanimircc4ca8f23ab8
diff --git a/src/main/mainEvents.ts b/src/main/mainEvents.ts
index 3a26f1310..5241c66b8 100644
--- a/src/main/mainEvents.ts
+++ b/src/main/mainEvents.ts
@@ -25,6 +25,7 @@ import {
deleteDirectoryPromise,
unZip,
getDirProperties,
+ isDirectory,
} from '@tagspaces/tagspaces-common-node/io-node';
import fs from 'fs-extra';
import path from 'path';
@@ -83,6 +84,10 @@ export default function loadMainEvents() {
const results = await isWorkerAvailable();
return results;
});
+ ipcMain.handle('isDirectory', async (event, path) => {
+ const results = await isDirectory(path);
+ return results;
+ });
ipcMain.handle('resolveRelativePaths', (event, relativePath) => {
return path.resolve(relativePath);
});
diff --git a/src/main/preload.ts b/src/main/preload.ts
index 009c0e69a..97b3f4b99 100644
--- a/src/main/preload.ts
+++ b/src/main/preload.ts
@@ -4,6 +4,7 @@ import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
export type Channels =
| 'isWorkerAvailable'
+ | 'isDirectory'
| 'resolveRelativePaths'
| 'set-language'
| 'setZoomFactor'
diff --git a/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx b/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx
index eec7448d5..7018af436 100644
--- a/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx
+++ b/src/renderer/components/dialogs/MoveOrCopyFilesDialog.tsx
@@ -45,13 +45,12 @@ import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
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;
onClose: (clearSelection?: boolean) => void;
- selectedFiles: Array<any>;
+ selectedFiles: Array<TS.FileSystemEntry>;
}
function MoveOrCopyFilesDialog(props: Props) {
@@ -99,9 +98,26 @@ function MoveOrCopyFilesDialog(props: Props) {
);
}
+ function handleCopy(filePaths: string[]) {
+ copyFiles(filePaths, currentDirectoryPath).then((success) => {
+ if (success) {
+ generateThumbs(
+ filePaths.map((targetPath) =>
+ joinPaths(
+ currentLocation?.getDirSeparator(),
+ currentDirectoryPath,
+ extractFileName(targetPath, currentLocation?.getDirSeparator()),
+ ),
+ ),
+ );
+ }
+ return true;
+ });
+ }
+
return (
<Dialog
- open={open}
+ open={open && selectedFiles && selectedFiles.length > 0}
onClose={onClose}
keepMounted
scroll="paper"
@@ -170,24 +186,17 @@ function MoveOrCopyFilesDialog(props: Props) {
<Button
onClick={() => {
if (selectedFiles) {
- const filePaths = selectedFiles.map((file) => file.path);
- copyFiles(filePaths, currentDirectoryPath).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, () => {
+ handleCopy(selectedFiles.map((file) => file.path));
+ });
+ } else {
+ handleCopy(selectedFiles.map((file) => file.path));
+ }
+ },
+ );
}
onClose();
}}
diff --git a/src/renderer/components/dialogs/hooks/MoveOrCopyFilesDialogContextProvider.tsx b/src/renderer/components/dialogs/hooks/MoveOrCopyFilesDialogContextProvider.tsx
index ef1e43d00..0376dd260 100644
--- a/src/renderer/components/dialogs/hooks/MoveOrCopyFilesDialogContextProvider.tsx
+++ b/src/renderer/components/dialogs/hooks/MoveOrCopyFilesDialogContextProvider.tsx
@@ -17,7 +17,11 @@
*/
import React, { createContext, useMemo, useReducer, useRef } from 'react';
+import { extractFileExtension } from '@tagspaces/tagspaces-common/paths';
+import { getUuid } from '@tagspaces/tagspaces-common/utils-io';
import LoadingLazy from '-/components/LoadingLazy';
+import { TS } from '-/tagspaces.namespace';
+import AppConfig from '-/AppConfig';
type MoveOrCopyFilesDialogContextData = {
openMoveOrCopyFilesDialog: (files: Array<File>) => void;
@@ -45,16 +49,37 @@ export const MoveOrCopyFilesDialogContextProvider = ({
children,
}: MoveOrCopyFilesDialogContextProviderProps) => {
const open = useRef<boolean>(false);
- const files = useRef<Array<File>>(undefined);
+ const files = useRef<TS.FileSystemEntry[]>(undefined);
const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
function openDialog(selectedFiles: Array<File>) {
open.current = true;
- files.current = selectedFiles;
+ isDirs(selectedFiles).then((isDirsArray) => {
+ files.current = selectedFiles.map((file, index) => ({
+ uuid: getUuid(),
+ name: file.name,
+ path: file.path,
+ isFile: !isDirsArray[index],
+ extension: extractFileExtension(file.path),
+ size: file.size,
+ lmdt: file.lastModified,
+ }));
+ forceUpdate();
+ });
forceUpdate();
}
+ function isDirs(files: Array<File>): Promise<boolean[]> {
+ if (AppConfig.isElectron) {
+ const filesPromises = files.map((file) =>
+ window.electronIO.ipcRenderer.invoke('isDirectory', file.path),
+ );
+ return Promise.all(filesPromises);
+ }
+ return Promise.resolve(files.map(() => true));
+ }
+
function closeDialog() {
open.current = false;
forceUpdate();
@@ -80,7 +105,7 @@ export const MoveOrCopyFilesDialogContextProvider = ({
<MoveOrCopyFilesDialogAsync
open={open.current}
onClose={closeDialog}
- selectedFiles={files.current}
+ selectedFiles={files.current?.filter((file) => file.isFile)} // todo enable for dir
/>
{children}
</MoveOrCopyFilesDialogContext.Provider>
diff --git a/src/renderer/tagspaces.namespace.d.ts b/src/renderer/tagspaces.namespace.d.ts
index 0153ee4b7..313da013c 100644
--- a/src/renderer/tagspaces.namespace.d.ts
+++ b/src/renderer/tagspaces.namespace.d.ts
@@ -184,7 +184,7 @@ export namespace TS {
name: string;
isFile: boolean;
isNewFile?: boolean;
- locationID: string;
+ locationID?: string;
//isAutoSaveEnabled?: boolean; // common with OpenedEntry
extension?: string;
textContent?: string;