mirror of
https://github.com/wekan/wekan.git
synced 2025-04-24 22:17:16 -04:00
removed createOnAfterUpload file and use existing code for initial file move to GridFS
This commit is contained in:
parent
fe018225b4
commit
9ef45a75af
4 changed files with 18 additions and 53 deletions
|
@ -32,9 +32,12 @@ Attachments = new FilesCollection({
|
|||
return ret;
|
||||
},
|
||||
onAfterUpload(fileObj) {
|
||||
// current storage is the filesystem, update object and database
|
||||
Object.keys(fileObj.versions).forEach(versionName => {
|
||||
fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterUpload();
|
||||
})
|
||||
fileObj.versions[versionName].storage = "fs";
|
||||
});
|
||||
Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
|
||||
moveToStorage(fileObj, "gridfs", fileStoreStrategyFactory);
|
||||
},
|
||||
interceptDownload(http, fileObj, versionName) {
|
||||
const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http);
|
||||
|
|
|
@ -27,9 +27,11 @@ Avatars = new FilesCollection({
|
|||
return 'avatar-too-big';
|
||||
},
|
||||
onAfterUpload(fileObj) {
|
||||
// current storage is the filesystem, update object and database
|
||||
Object.keys(fileObj.versions).forEach(versionName => {
|
||||
fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterUpload();
|
||||
fileObj.versions[versionName].storage = "fs";
|
||||
});
|
||||
Avatars.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
|
||||
},
|
||||
interceptDownload(http, fileObj, versionName) {
|
||||
const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import fs from 'fs';
|
||||
import { createObjectId } from './grid/createObjectId';
|
||||
import { createOnAfterUpload } from './fsHooks/createOnAfterUpload';
|
||||
import { createInterceptDownload } from './fsHooks/createInterceptDownload';
|
||||
import { createOnAfterRemove } from './fsHooks/createOnAfterRemove';
|
||||
|
||||
|
@ -26,7 +25,16 @@ export default class FileStoreStrategyFactory {
|
|||
*/
|
||||
getFileStrategy(filesCollection, fileObj, versionName, storage) {
|
||||
if (!storage) {
|
||||
storage = fileObj.versions[versionName].storage || "gridfs";
|
||||
storage = fileObj.versions[versionName].storage;
|
||||
if (!storage) {
|
||||
if (fileObj.meta.source == "import") {
|
||||
// uploaded by import, so it's in GridFS (MongoDB)
|
||||
storage = "gridfs";
|
||||
} else {
|
||||
// newly uploaded, so it's at the filesystem
|
||||
storage = "fs";
|
||||
}
|
||||
}
|
||||
}
|
||||
let ret;
|
||||
if (["fs", "gridfs"].includes(storage)) {
|
||||
|
@ -111,12 +119,6 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy {
|
|||
this.gridFsBucket = gridFsBucket;
|
||||
}
|
||||
|
||||
/** after successfull upload */
|
||||
onAfterUpload() {
|
||||
createOnAfterUpload(this.filesCollection, this.gridFsBucket, this.fileObj, this.versionName);
|
||||
super.onAfterUpload();
|
||||
}
|
||||
|
||||
/** download the file
|
||||
* @param http the current http request
|
||||
*/
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
import { Meteor } from 'meteor/meteor';
|
||||
import fs from 'fs';
|
||||
|
||||
export const createOnAfterUpload = function onAfterUpload(filesCollection, bucket, file, versionName) {
|
||||
const self = filesCollection;
|
||||
const metadata = { ...file.meta, versionName, fileId: file._id };
|
||||
fs.createReadStream(file.versions[versionName].path)
|
||||
|
||||
// this is where we upload the binary to the bucket using bucket.openUploadStream
|
||||
// see http://mongodb.github.io/node-mongodb-native/3.2/api/GridFSBucket.html#openUploadStream
|
||||
.pipe(
|
||||
bucket.openUploadStream(file.name, {
|
||||
contentType: file.type || 'binary/octet-stream',
|
||||
metadata,
|
||||
}),
|
||||
)
|
||||
|
||||
// and we unlink the file from the fs on any error
|
||||
// that occurred during the upload to prevent zombie files
|
||||
.on('error', err => {
|
||||
console.error("[createOnAfterUpload error]", err);
|
||||
self.unlink(self.collection.findOne(file._id), versionName); // Unlink files from FS
|
||||
})
|
||||
|
||||
// once we are finished, we attach the gridFS Object id on the
|
||||
// FilesCollection document's meta section and finally unlink the
|
||||
// upload file from the filesystem
|
||||
.on(
|
||||
'finish',
|
||||
Meteor.bindEnvironment(ver => {
|
||||
const property = `versions.${versionName}.meta.gridFsFileId`;
|
||||
|
||||
self.collection.update(file._id, {
|
||||
$set: {
|
||||
[property]: ver._id.toHexString(),
|
||||
},
|
||||
});
|
||||
|
||||
self.unlink(self.collection.findOne(file._id), versionName); // Unlink files from FS
|
||||
}),
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue