const destinationPath = '/destinationPath';
// when
- await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
+ await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
// then
expect(serverApi.put).toHaveBeenCalledTimes(1);
const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
const destinationPath = '/destinationPath';
- await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
+ await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
expect(serverApi.put).toHaveBeenCalledTimes(1);
expect(serverApi.put).toHaveBeenCalledWith(
const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
const destinationPath = '/';
- await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
+ await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
expect(serverApi.put).toHaveBeenCalledTimes(1);
expect(serverApi.put).toHaveBeenCalledWith(
const destinationPath = '/destinationPath';
// when
- await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, destinationUuid, destinationPath);
+ await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: destinationUuid}, destinationPath);
// then
expect(serverApi.put).toHaveBeenCalledTimes(2);
const destinationPath = '/destinationPath';
// when
- await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, srcCollectionUUID, destinationPath);
+ await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: srcCollectionUUID}, destinationPath);
// then
expect(serverApi.put).toHaveBeenCalledTimes(1);
// when
try {
- await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, destinationUuid, destinationPath);
+ await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: destinationUuid}, destinationPath);
} catch {}
// then
import { ApiActions } from "services/api/api-actions";
import { Session } from "models/session";
import { CommonService } from "services/common-service/common-service";
+import { snakeCase } from "lodash";
export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
+type CollectionPartialUpdateOrCreate = Partial<CollectionResource> & Pick<CollectionResource, "uuid"> |
+ Partial<CollectionResource> & Pick<CollectionResource, "ownerUuid">;
export const emptyCollectionPdh = 'd41d8cd98f00b204e9800998ecf8427e+0';
}, "/");
}
- private replaceFiles(collectionUuid: string, fileMap: {}, showErrors?: boolean) {
+ private replaceFiles(data: CollectionPartialUpdateOrCreate, fileMap: {}, showErrors?: boolean) {
const payload = {
collection: {
- preserve_version: true
+ preserve_version: true,
+ ...CommonService.mapKeys(snakeCase)(data),
+ // Don't send uuid in payload when creating
+ uuid: undefined,
},
replace_files: fileMap
};
-
- return CommonService.defaultResponse(
- this.serverApi
- .put<CollectionResource>(`/${this.resourceType}/${collectionUuid}`, payload),
- this.actions,
- true, // mapKeys
- showErrors
- );
+ if (data.uuid) {
+ return CommonService.defaultResponse(
+ this.serverApi
+ .put<CollectionResource>(`/${this.resourceType}/${data.uuid}`, payload),
+ this.actions,
+ true, // mapKeys
+ showErrors
+ );
+ } else {
+ return CommonService.defaultResponse(
+ this.serverApi
+ .post<CollectionResource>(`/${this.resourceType}`, payload),
+ this.actions,
+ true, // mapKeys
+ showErrors
+ );
+ }
}
async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress, targetLocation: string = '') {
}
async renameFile(collectionUuid: string, collectionPdh: string, oldPath: string, newPath: string) {
- return this.replaceFiles(collectionUuid, {
+ return this.replaceFiles({uuid: collectionUuid}, {
[this.combineFilePath([newPath])]: `${collectionPdh}${this.combineFilePath([oldPath])}`,
[this.combineFilePath([oldPath])]: '',
});
}
}, {})
- return this.replaceFiles(collectionUuid, fileMap, showErrors);
+ return this.replaceFiles({uuid: collectionUuid}, fileMap, showErrors);
}
- copyFiles(sourcePdh: string, files: string[], destinationCollectionUuid: string, destinationPath: string, showErrors?: boolean) {
+ copyFiles(sourcePdh: string, files: string[], destinationCollection: CollectionPartialUpdateOrCreate, destinationPath: string, showErrors?: boolean) {
const fileMap = files.reduce((obj, sourceFile) => {
const sourceFileName = sourceFile.split('/').filter(Boolean).slice(-1).join("");
return {
};
}, {});
- return this.replaceFiles(destinationCollectionUuid, fileMap, showErrors);
+ return this.replaceFiles(destinationCollection, fileMap, showErrors);
}
- moveFiles(sourceUuid: string, sourcePdh: string, files: string[], destinationCollectionUuid: string, destinationPath: string, showErrors?: boolean) {
- if (sourceUuid === destinationCollectionUuid) {
+ moveFiles(sourceUuid: string, sourcePdh: string, files: string[], destinationCollection: CollectionPartialUpdateOrCreate, destinationPath: string, showErrors?: boolean) {
+ if (sourceUuid === destinationCollection.uuid) {
const fileMap = files.reduce((obj, sourceFile) => {
const sourceFileName = sourceFile.split('/').filter(Boolean).slice(-1).join("");
return {
};
}, {});
- return this.replaceFiles(sourceUuid, fileMap, showErrors)
+ return this.replaceFiles({uuid: sourceUuid}, fileMap, showErrors)
} else {
- return this.copyFiles(sourcePdh, files, destinationCollectionUuid, destinationPath, showErrors)
+ return this.copyFiles(sourcePdh, files, destinationCollection, destinationPath, showErrors)
.then(() => {
return this.deleteFiles(sourceUuid, files, showErrors);
});
createDirectory(collectionUuid: string, path: string, showErrors?: boolean) {
const fileMap = {[this.combineFilePath([path])]: emptyCollectionPdh};
- return this.replaceFiles(collectionUuid, fileMap, showErrors);
+ return this.replaceFiles({uuid: collectionUuid}, fileMap, showErrors);
}
}
dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
- // Create new collection
- const newCollection = await services.collectionService.create({
- name,
- description,
- ownerUuid: projectUuid,
- uuid: undefined,
- });
-
// Get selected files
const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
.map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
// Copy files
- const updatedCollection = await services.collectionService.copyFiles(sourceCollection.portableDataHash, paths, newCollection.uuid, '/', false);
+ const updatedCollection = await services.collectionService.copyFiles(
+ sourceCollection.portableDataHash,
+ paths,
+ {
+ name,
+ description,
+ ownerUuid: projectUuid,
+ uuid: undefined,
+ },
+ '/',
+ false
+ );
dispatch(updateResources([updatedCollection]));
dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
.map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
// Copy files
- const updatedCollection = await services.collectionService.copyFiles(sourceCollection.portableDataHash, paths, collectionUuid, '/', false);
+ const updatedCollection = await services.collectionService.copyFiles(sourceCollection.portableDataHash, paths, {uuid: collectionUuid}, '/', false);
dispatch(updateResources([updatedCollection]));
dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
dispatch(startSubmit(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION));
- // Create new collection
- const newCollection = await services.collectionService.create({
- name,
- description,
- ownerUuid: projectUuid,
- uuid: undefined,
- });
-
// Get selected files
const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true)
.map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
// Move files
- const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, newCollection.uuid, '/', false);
+ const updatedCollection = await services.collectionService.moveFiles(
+ sourceCollection.uuid,
+ sourceCollection.portableDataHash,
+ paths,
+ {
+ name,
+ description,
+ ownerUuid: projectUuid,
+ uuid: undefined,
+ },
+ '/',
+ false
+ );
dispatch(updateResources([updatedCollection]));
dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_NEW_COLLECTION }));
.map(file => file.id.replace(new RegExp(`(^${sourceCollection.uuid})`), ''));
// Move files
- const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, collectionUuid, '/', false);
+ const updatedCollection = await services.collectionService.moveFiles(sourceCollection.uuid, sourceCollection.portableDataHash, paths, {uuid: collectionUuid}, '/', false);
dispatch(updateResources([updatedCollection]));
dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_MOVE_TO_SELECTED_COLLECTION }));