Skip to content

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

fix copy and move files

Stanimir5221eca00b0e
diff --git a/src/renderer/components/dialogs/FileUploadDialog.tsx b/src/renderer/components/dialogs/FileUploadDialog.tsx
index 02cc11431..6fedcf597 100644
--- a/src/renderer/components/dialogs/FileUploadDialog.tsx
+++ b/src/renderer/components/dialogs/FileUploadDialog.tsx
@@ -256,9 +256,6 @@ function FileUploadDialog(props: Props) {
onClick={() => {
onClose();
dispatch(AppActions.clearUploadDialog());
- if (currentDirectoryPerspective === PerspectiveIDs.GRID) {
- loadDirectoryContent(currentDirectoryPath, false);
- }
}}
color="primary"
>
diff --git a/src/renderer/hooks/IOActionsContextProvider.tsx b/src/renderer/hooks/IOActionsContextProvider.tsx
index 1b7950ba1..dd43aeb1d 100644
--- a/src/renderer/hooks/IOActionsContextProvider.tsx
+++ b/src/renderer/hooks/IOActionsContextProvider.tsx
@@ -38,7 +38,9 @@ import PlatformIO from '-/services/platform-facade';
import { actions as AppActions, AppDispatch } from '-/reducers/app';
import { useDispatch, useSelector } from 'react-redux';
import {
+ executePromisesInBatches,
generateFileName,
+ getAllPropertiesPromise,
getThumbPath,
loadFileMetaDataPromise,
toFsEntry,
@@ -330,26 +332,23 @@ export const IOActionsContextProvider = ({
targetPath: string,
onProgress = undefined,
): Promise<boolean> {
+ const progress = dirPaths.length > 10 ? undefined : onProgress;
const promises = dirPaths.map(({ path, count }) => {
const dirName = extractDirectoryName(path, PlatformIO.getDirSeparator());
return moveDirectoryPromise(
{ path: path, total: count },
joinPaths(PlatformIO.getDirSeparator(), targetPath, dirName),
- dirPaths.length > 10 ? undefined : onProgress,
+ progress,
false,
)
.then((newDirPath) => {
// console.log('Moving dir from ' + path + ' to ' + targetPath);
- //return getAllPropertiesPromise(newDirPath).then(
- // (fsEntry: TS.FileSystemEntry) => {
const action: TS.EditAction = {
action: 'move',
entry: toFsEntry(newDirPath, false),
oldEntryPath: path,
};
return action;
- // },
- //);
})
.catch((err) => {
console.warn('Moving dirs failed ', err);
@@ -358,35 +357,25 @@ export const IOActionsContextProvider = ({
});
});
return executePromisesInBatches(promises).then((actions) => {
+ if (!progress) {
+ const progresses = actions.map((action) =>
+ action
+ ? {
+ path: action.entry.path,
+ progress: 100,
+ }
+ : {
+ path: action.entry.path,
+ progress: 0,
+ },
+ );
+ dispatch(AppActions.setProgresses(progresses));
+ }
setReflectActions(...actions.filter((value) => value !== undefined));
return true;
});
}
- async function executePromisesInBatches<T>(
- promises: Promise<T>[],
- batchSize = 5,
- ): Promise<T[]> {
- const results: T[] = [];
-
- for (let i = 0; i < promises.length; i += batchSize) {
- const batch = promises.slice(i, i + batchSize);
- const batchResults = await Promise.allSettled(batch);
- results.push(
- ...batchResults.map((result) => {
- if (result.status === 'fulfilled') {
- return result.value as T;
- } else {
- // Handle rejected promise here
- return undefined;
- }
- }),
- );
- }
-
- return results;
- }
-
function moveFiles(
paths: Array<string>,
targetPath: string,
@@ -403,74 +392,79 @@ export const IOActionsContextProvider = ({
paths.length > 10 ? undefined : onProgress,
false,
)
- .then(() => {
- showNotification(t('core:filesMovedSuccessful'));
- const moveMetaJobs = [];
- moveJobs.map((job) => {
- // Move revisions
- loadFileMetaDataPromise(job[0])
- .then((fsEntryMeta: TS.FileSystemEntryMeta) => {
- if (fsEntryMeta.id) {
- const backupDir = getBackupFileDir(
- job[0],
- fsEntryMeta.id,
- PlatformIO.getDirSeparator(),
- );
- const newBackupDir = getBackupFileDir(
- job[1],
- fsEntryMeta.id,
- PlatformIO.getDirSeparator(),
- );
- return PlatformIO.moveDirectoryPromise(
- { path: backupDir },
- newBackupDir,
- )
- .then(() => {
- console.log(
- 'Moving revisions successful from ' +
- backupDir +
- ' to ' +
- newBackupDir,
- );
- return true;
- })
- .catch((err) => {
- console.warn('Moving revisions failed ', err);
- });
- }
+ .then((moveArray) => {
+ if (moveArray !== undefined) {
+ showNotification(t('core:filesMovedSuccessful'));
+ const moveMetaJobs = [];
+ moveJobs.map((job) => {
+ // Move revisions
+ loadFileMetaDataPromise(job[0])
+ .then((fsEntryMeta: TS.FileSystemEntryMeta) => {
+ if (fsEntryMeta.id) {
+ const backupDir = getBackupFileDir(
+ job[0],
+ fsEntryMeta.id,
+ PlatformIO.getDirSeparator(),
+ );
+ const newBackupDir = getBackupFileDir(
+ job[1],
+ fsEntryMeta.id,
+ PlatformIO.getDirSeparator(),
+ );
+ return PlatformIO.moveDirectoryPromise(
+ { path: backupDir },
+ newBackupDir,
+ )
+ .then(() => {
+ console.log(
+ 'Moving revisions successful from ' +
+ backupDir +
+ ' to ' +
+ newBackupDir,
+ );
+ return true;
+ })
+ .catch((err) => {
+ console.warn('Moving revisions failed ', err);
+ });
+ }
+ })
+ .catch((err) => {
+ console.warn('loadFileMetaDataPromise', err);
+ });
+
+ // move meta
+ moveMetaJobs.push([
+ getMetaFileLocationForFile(job[0], PlatformIO.getDirSeparator()),
+ getMetaFileLocationForFile(job[1], PlatformIO.getDirSeparator()),
+ ]);
+ moveMetaJobs.push([
+ getThumbFileLocationForFile(
+ job[0],
+ PlatformIO.getDirSeparator(),
+ false,
+ ),
+ getThumbFileLocationForFile(
+ job[1],
+ PlatformIO.getDirSeparator(),
+ false,
+ ),
+ ]);
+ return true;
+ });
+ return moveFilesPromise(moveMetaJobs, undefined, false)
+ .then(() => {
+ console.log('Moving meta and thumbs successful');
+ return reflectMoveFiles(moveJobs);
})
.catch((err) => {
- console.warn('loadFileMetaDataPromise', err);
+ console.warn('At least one meta or thumb was not moved ' + err);
+ return reflectMoveFiles(moveJobs);
});
-
- // move meta
- moveMetaJobs.push([
- getMetaFileLocationForFile(job[0], PlatformIO.getDirSeparator()),
- getMetaFileLocationForFile(job[1], PlatformIO.getDirSeparator()),
- ]);
- moveMetaJobs.push([
- getThumbFileLocationForFile(
- job[0],
- PlatformIO.getDirSeparator(),
- false,
- ),
- getThumbFileLocationForFile(
- job[1],
- PlatformIO.getDirSeparator(),
- false,
- ),
- ]);
- return true;
- });
- return moveFilesPromise(moveMetaJobs, undefined, false)
- .then(() => {
- console.log('Moving meta and thumbs successful');
- return reflectMoveFiles(moveJobs);
- })
- .catch((err) => {
- console.warn('At least one meta or thumb was not moved ' + err);
- return reflectMoveFiles(moveJobs);
- });
+ } else {
+ showNotification(t('core:copyingFilesFailed'));
+ return false;
+ }
})
.catch((err) => {
console.warn('Moving files failed with ' + err);
@@ -484,23 +478,51 @@ export const IOActionsContextProvider = ({
targetPath: string,
onProgress = undefined,
): Promise<boolean> {
+ const progress = dirPaths.length > 10 ? undefined : onProgress;
const promises = dirPaths.map(({ path, count }) => {
const dirName = extractDirectoryName(path, PlatformIO.getDirSeparator());
return copyDirectoryPromise(
{ path: path, total: count },
joinPaths(PlatformIO.getDirSeparator(), targetPath, dirName),
- onProgress,
+ progress,
+ false,
)
- .then(() => {
- console.log('Copy dir from ' + path + ' to ' + targetPath);
- return true;
+ .then((newDirPath) => {
+ // console.log('Copy dir from ' + path + ' to ' + targetPath);
+ return getAllPropertiesPromise(newDirPath).then(
+ (fsEntry: TS.FileSystemEntry) => {
+ const action: TS.EditAction = {
+ action: 'add',
+ entry: fsEntry, //toFsEntry(newDirPath, false),
+ };
+ return action;
+ },
+ );
})
.catch((err) => {
console.warn('Copy dirs failed ', err);
showNotification(t('core:copyingFoldersFailed'));
+ return undefined;
});
});
- return Promise.all(promises).then(() => true);
+ return executePromisesInBatches(promises).then((actions) => {
+ if (!progress) {
+ const progresses = actions.map((action) =>
+ action
+ ? {
+ path: action.entry.path,
+ progress: 100,
+ }
+ : {
+ path: action.entry.path,
+ progress: 0,
+ },
+ );
+ dispatch(AppActions.setProgresses(progresses));
+ }
+ setReflectActions(...actions.filter((value) => value !== undefined));
+ return true;
+ });
}
function copyFiles(
@@ -508,7 +530,11 @@ export const IOActionsContextProvider = ({
targetPath: string,
onProgress,
): Promise<boolean> {
- return copyFilesWithProgress(paths, targetPath, onProgress)
+ return copyFilesWithProgress(
+ paths,
+ targetPath,
+ paths.length > 10 ? undefined : onProgress,
+ )
.then((success) => {
if (success) {
showNotification(t('core:filesCopiedSuccessful'));
@@ -524,7 +550,7 @@ export const IOActionsContextProvider = ({
return copyFilesWithProgress(
metaPaths,
getMetaDirectoryPath(targetPath),
- onProgress,
+ undefined, //metaPaths.length > 10 ? undefined : onProgress,
false,
)
.then(() => {
diff --git a/src/renderer/hooks/PlatformFacadeContextProvider.tsx b/src/renderer/hooks/PlatformFacadeContextProvider.tsx
index 6ba102009..e8bd4d4fe 100644
--- a/src/renderer/hooks/PlatformFacadeContextProvider.tsx
+++ b/src/renderer/hooks/PlatformFacadeContextProvider.tsx
@@ -32,7 +32,11 @@ import { useTranslation } from 'react-i18next';
import { actions as AppActions, AppDispatch } from '-/reducers/app';
import { useDispatch, useSelector } from 'react-redux';
import { useEditedEntryContext } from '-/hooks/useEditedEntryContext';
-import { getAllPropertiesPromise, toFsEntry } from '-/services/utils-io';
+import {
+ executePromisesInBatches,
+ getAllPropertiesPromise,
+ toFsEntry,
+} from '-/services/utils-io';
import { getUseTrashCan } from '-/reducers/settings';
type PlatformFacadeContextData = {
@@ -81,6 +85,7 @@ type PlatformFacadeContextData = {
param: any,
newDirPath: string,
onProgress?,
+ reflect?,
) => Promise<any>;
moveDirectoryPromise: (
param: any,
@@ -364,7 +369,7 @@ export const PlatformFacadeContextProvider = ({
): Promise<any> {
const flatArray = renameJobs.flat();
ignoreByWatcher(...flatArray);
- return Promise.all(
+ return executePromisesInBatches(
renameJobs.map(async (renameJob) => {
try {
return await PlatformFacade.renameFilePromise(
@@ -418,7 +423,7 @@ export const PlatformFacadeContextProvider = ({
): Promise<any> {
const flatArray = renameJobs.flat();
ignoreByWatcher(...flatArray);
- return Promise.all(
+ return executePromisesInBatches(
renameJobs.map(async (renameJob) => {
try {
return await PlatformFacade.renameFilePromise(
@@ -428,15 +433,17 @@ export const PlatformFacadeContextProvider = ({
);
} catch (err) {
console.warn('Error rename file:', err);
- return false;
+ return undefined;
}
}),
).then((ret) => {
- if (reflect) {
+ deignoreByWatcher(...flatArray);
+ const r = ret.filter((r) => r !== undefined);
+ if (reflect && r.length > 0) {
reflectMoveFiles(renameJobs);
+ return r;
}
- deignoreByWatcher(...flatArray);
- return ret;
+ return undefined;
});
}
@@ -444,19 +451,25 @@ export const PlatformFacadeContextProvider = ({
const promises = moveJobs.map((job) => {
return getAllPropertiesPromise(job[1]).then(
(newFsEntry: TS.FileSystemEntry) => {
- const actions: TS.EditAction[] = [
- {
- action: 'move',
- entry: newFsEntry,
- oldEntryPath: job[0],
- },
- ];
- return actions;
+ if (newFsEntry) {
+ const actions: TS.EditAction[] = [
+ {
+ action: 'move',
+ entry: newFsEntry,
+ oldEntryPath: job[0],
+ },
+ ];
+ return actions;
+ }
+ return undefined;
},
);
});
return Promise.all(promises).then((actionsArray) => {
- setReflectActions(...actionsArray.flat());
+ const actions = actionsArray.filter((a) => a !== undefined);
+ if (actions.length > 0) {
+ setReflectActions(...actions.flat());
+ }
return true;
});
}
@@ -486,6 +499,7 @@ export const PlatformFacadeContextProvider = ({
param: any,
newDirPath: string,
onProgress = undefined,
+ reflect = true,
): Promise<any> {
ignoreByWatcher(param.path, newDirPath);
return PlatformFacade.copyDirectoryPromise(
@@ -493,9 +507,11 @@ export const PlatformFacadeContextProvider = ({
newDirPath,
onProgress,
).then((result) => {
- getAllPropertiesPromise(param.path).then((fsEntry: TS.FileSystemEntry) =>
- reflectAddEntry(fsEntry),
- );
+ if (reflect) {
+ getAllPropertiesPromise(param.path).then(
+ (fsEntry: TS.FileSystemEntry) => reflectAddEntry(fsEntry),
+ );
+ }
deignoreByWatcher(param.path, newDirPath);
return result;
});
@@ -604,7 +620,7 @@ export const PlatformFacadeContextProvider = ({
}
return PlatformFacade.deleteDirectoryPromise(e.path, useTrashCan);
});
- return Promise.all(promises).then(() => {
+ return executePromisesInBatches(promises).then(() => {
reflectDeleteEntries(...entries);
deignoreByWatcher(...entriesPaths);
return true;
diff --git a/src/renderer/reducers/app.ts b/src/renderer/reducers/app.ts
index 12ff7aff6..be11b9495 100644
--- a/src/renderer/reducers/app.ts
+++ b/src/renderer/reducers/app.ts
@@ -47,6 +47,7 @@ export const types = {
DEVICE_ONLINE: 'APP/DEVICE_ONLINE',
DEVICE_OFFLINE: 'APP/DEVICE_OFFLINE',
PROGRESS: 'APP/PROGRESS',
+ PROGRESS_FINISH: 'APP/PROGRESS_FINISH',
RESET_PROGRESS: 'APP/RESET_PROGRESS',
LAST_BACKGROUND_IMAGE_CHANGE: 'APP/LAST_BACKGROUND_IMAGE_CHANGE',
LAST_BACKGROUND_COLOR_CHANGE: 'APP/LAST_BACKGROUND_COLOR_CHANGE',
@@ -278,6 +279,9 @@ export default (state: any = initialState, action: any) => {
});
return { ...state, progress: arrProgress };
}
+ case types.PROGRESS_FINISH: {
+ return { ...state, progress: action.progresses };
+ }
case types.RESET_PROGRESS: {
return { ...state, progress: [] };
}
@@ -737,6 +741,10 @@ export const actions = {
progress,
abort,
}),
+ setProgresses: (progresses: { path: string; progress: number }[]) => ({
+ type: types.PROGRESS_FINISH,
+ progresses,
+ }),
resetProgress: () => ({ type: types.RESET_PROGRESS }),
onUploadProgress:
(progress, abort, fileName = undefined) =>
diff --git a/src/renderer/services/utils-io.ts b/src/renderer/services/utils-io.ts
index c2f7b19f6..20ec550ad 100644
--- a/src/renderer/services/utils-io.ts
+++ b/src/renderer/services/utils-io.ts
@@ -1649,3 +1649,27 @@ export function openUrlForWeb(url) {
// rel: 'noopener noreferrer'
// }).click();
}
+
+export async function executePromisesInBatches<T>(
+ promises: Promise<T>[],
+ batchSize = 5,
+): Promise<T[]> {
+ const results: T[] = [];
+
+ for (let i = 0; i < promises.length; i += batchSize) {
+ const batch = promises.slice(i, i + batchSize);
+ const batchResults = await Promise.allSettled(batch);
+ results.push(
+ ...batchResults.map((result) => {
+ if (result.status === 'fulfilled') {
+ return result.value as T;
+ } else {
+ // Handle rejected promise here
+ return undefined;
+ }
+ }),
+ );
+ }
+
+ return results;
+}