Multi-File Storage.

Thanks to mfilser !

Related https://github.com/wekan/wekan/pull/4484

Merge branch 'master' into upgrade-meteor
This commit is contained in:
Lauri Ojansivu 2022-04-22 00:55:42 +03:00
commit 68e8155805
29 changed files with 921 additions and 276 deletions

View file

@ -1,30 +1,17 @@
import { Meteor } from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';
import path from 'path';
import { createBucket } from './lib/grid/createBucket';
import { createOnAfterUpload } from './lib/fsHooks/createOnAfterUpload';
import { createInterceptDownload } from './lib/fsHooks/createInterceptDownload';
import { createOnAfterRemove } from './lib/fsHooks/createOnAfterRemove';
import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
import FileStoreStrategyFactory, {moveToStorage, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
let attachmentBucket;
let storagePath;
if (Meteor.isServer) {
attachmentBucket = createBucket('attachments');
storagePath = path.join(process.env.WRITABLE_PATH, 'attachments');
}
const insertActivity = (fileObj, activityType) =>
Activities.insert({
userId: fileObj.userId,
type: 'card',
activityType,
attachmentId: fileObj._id,
// this preserves the name so that notifications can be meaningful after
// this file is removed
attachmentName: fileObj.name,
boardId: fileObj.meta.boardId,
cardId: fileObj.meta.cardId,
listId: fileObj.meta.listId,
swimlaneId: fileObj.meta.swimlaneId,
});
export const fileStoreStrategyFactory = new FileStoreStrategyFactory(AttachmentStoreStrategyFilesystem, storagePath, AttachmentStoreStrategyGridFs, attachmentBucket);
// XXX Enforce a schema for the Attachments FilesCollection
// see: https://github.com/VeliovGroup/Meteor-Files/wiki/Schema
@ -33,26 +20,34 @@ Attachments = new FilesCollection({
debug: false, // Change to `true` for debugging
collectionName: 'attachments',
allowClientCode: true,
namingFunction(opts) {
const filenameWithoutExtension = opts.name.replace(/(.+)\..+/, "$1");
const ret = opts.meta.fileId + "-original-" + filenameWithoutExtension;
// remove fileId from meta, it was only stored there to have this information here in the namingFunction function
delete opts.meta.fileId;
return ret;
},
storagePath() {
if (process.env.WRITABLE_PATH) {
return path.join(process.env.WRITABLE_PATH, 'uploads', 'attachments');
}
return path.normalize(`assets/app/uploads/${this.collectionName}`);
const ret = fileStoreStrategyFactory.storagePath;
return ret;
},
onAfterUpload: function onAfterUpload(fileRef) {
createOnAfterUpload(attachmentBucket).call(this, fileRef);
// If the attachment doesn't have a source field
// or its source is different than import
if (!fileRef.meta.source || fileRef.meta.source !== 'import') {
// Add activity about adding the attachment
insertActivity(fileRef, 'addAttachment');
}
onAfterUpload(fileObj) {
// current storage is the filesystem, update object and database
Object.keys(fileObj.versions).forEach(versionName => {
fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
});
Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
moveToStorage(fileObj, STORAGE_NAME_GRIDFS, fileStoreStrategyFactory);
},
interceptDownload: createInterceptDownload(attachmentBucket),
onAfterRemove: function onAfterRemove(files) {
createOnAfterRemove(attachmentBucket).call(this, files);
interceptDownload(http, fileObj, versionName) {
const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
return ret;
},
onAfterRemove(files) {
files.forEach(fileObj => {
insertActivity(fileObj, 'deleteAttachment');
Object.keys(fileObj.versions).forEach(versionName => {
fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
});
});
},
// We authorize the attachment download either:
@ -81,6 +76,16 @@ if (Meteor.isServer) {
fetch: ['meta'],
});
Meteor.methods({
moveAttachmentToStorage(fileObjId, storageDestination) {
check(fileObjId, String);
check(storageDestination, String);
const fileObj = Attachments.findOne({_id: fileObjId});
moveToStorage(fileObj, storageDestination, fileStoreStrategyFactory);
},
});
Meteor.startup(() => {
Attachments.collection.createIndex({ cardId: 1 });
});