removed createOnAfterRemove file and use existing code for remove file from GridFS

This commit is contained in:
Martin Filser 2022-04-03 23:52:36 +02:00
parent 9ef45a75af
commit 99c37bd521
2 changed files with 24 additions and 19 deletions

View file

@ -1,7 +1,6 @@
import fs from 'fs'; import fs from 'fs';
import { createObjectId } from './grid/createObjectId'; import { createObjectId } from './grid/createObjectId';
import { createInterceptDownload } from './fsHooks/createInterceptDownload'; import { createInterceptDownload } from './fsHooks/createInterceptDownload';
import { createOnAfterRemove } from './fsHooks/createOnAfterRemove';
/** Factory for FileStoreStrategy */ /** Factory for FileStoreStrategy */
export default class FileStoreStrategyFactory { export default class FileStoreStrategyFactory {
@ -137,11 +136,9 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy {
* @return the read stream * @return the read stream
*/ */
getReadStream() { getReadStream() {
const gridFsFileId = (this.fileObj.versions[this.versionName].meta || {}) const gfsId = this.getGridFsObjectId();
.gridFsFileId;
let ret; let ret;
if (gridFsFileId) { if (gfsId) {
const gfsId = createObjectId({ gridFsFileId });
ret = this.gridFsBucket.openDownloadStream(gfsId); ret = this.gridFsBucket.openDownloadStream(gfsId);
} }
return ret; return ret;
@ -171,7 +168,15 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy {
/** remove the file */ /** remove the file */
unlink() { 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(); const gridFsFileIdName = this.getGridFsFileIdName();
Attachments.update({ _id: this.fileObj._id }, { $unset: { [gridFsFileIdName]: 1 } }); Attachments.update({ _id: this.fileObj._id }, { $unset: { [gridFsFileIdName]: 1 } });
} }
@ -183,6 +188,19 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy {
return "gridfs"; 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 /** returns the property name of gridFsFileId
* @return the property name of gridFsFileId * @return the property name of gridFsFileId
*/ */

View file

@ -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);
});
}
};