diff --git a/models/lib/fileStoreStrategy.js b/models/lib/fileStoreStrategy.js index c4d0cfe47..c15bd6cc2 100644 --- a/models/lib/fileStoreStrategy.js +++ b/models/lib/fileStoreStrategy.js @@ -1,7 +1,6 @@ import fs from 'fs'; import { createObjectId } from './grid/createObjectId'; import { createInterceptDownload } from './fsHooks/createInterceptDownload'; -import { createOnAfterRemove } from './fsHooks/createOnAfterRemove'; /** Factory for FileStoreStrategy */ export default class FileStoreStrategyFactory { @@ -137,11 +136,9 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy { * @return the read stream */ getReadStream() { - const gridFsFileId = (this.fileObj.versions[this.versionName].meta || {}) - .gridFsFileId; + const gfsId = this.getGridFsObjectId(); let ret; - if (gridFsFileId) { - const gfsId = createObjectId({ gridFsFileId }); + if (gfsId) { ret = this.gridFsBucket.openDownloadStream(gfsId); } return ret; @@ -171,7 +168,15 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy { /** remove the file */ unlink() { - createOnAfterRemove(this.filesCollection, this.gridFsBucket, this.fileObj, this.versionName); + const gfsId = this.getGridFsObjectId(); + if (gfsId) { + this.gridFsBucket.delete(gfsId, err => { + if (err) { + console.error("error on gfs bucket.delete: ", err); + } + }); + } + const gridFsFileIdName = this.getGridFsFileIdName(); Attachments.update({ _id: this.fileObj._id }, { $unset: { [gridFsFileIdName]: 1 } }); } @@ -183,6 +188,19 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy { return "gridfs"; } + /** returns the GridFS Object-Id + * @return the GridFS Object-Id + */ + getGridFsObjectId() { + const gridFsFileId = (this.fileObj.versions[this.versionName].meta || {}) + .gridFsFileId; + let ret; + if (gridFsFileId) { + ret = createObjectId({ gridFsFileId }); + } + return ret; + } + /** returns the property name of gridFsFileId * @return the property name of gridFsFileId */ diff --git a/models/lib/fsHooks/createOnAfterRemove.js b/models/lib/fsHooks/createOnAfterRemove.js deleted file mode 100644 index 702eaa596..000000000 --- a/models/lib/fsHooks/createOnAfterRemove.js +++ /dev/null @@ -1,13 +0,0 @@ -import { createObjectId } from '../grid/createObjectId'; - -export const createOnAfterRemove = - function onAfterRemove(filesCollection, bucket, file, versionName) { - const gridFsFileId = (file.versions[versionName].meta || {}) - .gridFsFileId; - if (gridFsFileId) { - const gfsId = createObjectId({ gridFsFileId }); - bucket.delete(gfsId, err => { - console.error("error on gfs bucket.delete: ", err); - }); - } - };