Attachment file rename is now possible

- Relates to: #2099
This commit is contained in:
Martin Filser 2022-04-24 17:12:31 +02:00
parent 21c09c67e4
commit 26e1c1dc4a
5 changed files with 71 additions and 2 deletions

View file

@ -4,7 +4,7 @@ import { createBucket } from './lib/grid/createBucket';
import fs from 'fs';
import path from 'path';
import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
import FileStoreStrategyFactory, {moveToStorage, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
import FileStoreStrategyFactory, {moveToStorage, rename, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
let attachmentBucket;
let storagePath;
@ -87,6 +87,13 @@ if (Meteor.isServer) {
const fileObj = Attachments.findOne({_id: fileObjId});
moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
},
renameAttachment(fileObjId, newName) {
check(fileObjId, String);
check(newName, String);
const fileObj = Attachments.findOne({_id: fileObjId});
rename(fileObj, newName, fileStoreStrategyFactory);
},
});
Meteor.startup(() => {

View file

@ -114,6 +114,13 @@ class FileStoreStrategy {
unlink() {
}
/** rename the file (physical)
* @li at database the filename is updated after this method
* @param newFilePath the new file path
*/
rename(newFilePath) {
}
/** return the storage name
* @return the storage name
*/
@ -287,6 +294,14 @@ export class FileStoreStrategyFilesystem extends FileStoreStrategy {
fs.unlink(filePath, () => {});
}
/** rename the file (physical)
* @li at database the filename is updated after this method
* @param newFilePath the new file path
*/
rename(newFilePath) {
fs.renameSync(this.fileObj.versions[this.versionName].path, newFilePath);
}
/** return the storage name
* @return the storage name
*/
@ -389,3 +404,16 @@ export const copyFile = function(fileObj, newCardId, fileStoreStrategyFactory) {
readStream.pipe(writeStream);
};
export const rename = function(fileObj, newName, fileStoreStrategyFactory) {
Object.keys(fileObj.versions).forEach(versionName => {
const strategy = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName);
const newFilePath = strategy.getNewPath(fileStoreStrategyFactory.storagePath, newName);
strategy.rename(newFilePath);
Attachments.update({ _id: fileObj._id }, { $set: {
"name": newName,
[`versions.${versionName}.path`]: newFilePath,
} });
});
};