diff --git a/models/attachments.js b/models/attachments.js index 2d1000b16..ecdcdfbb8 100644 --- a/models/attachments.js +++ b/models/attachments.js @@ -40,13 +40,13 @@ Attachments = new FilesCollection({ moveToStorage(fileObj, STORAGE_NAME_GRIDFS, fileStoreStrategyFactory); }, interceptDownload(http, fileObj, versionName) { - const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http); + const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl); return ret; }, onAfterRemove(files) { files.forEach(fileObj => { Object.keys(fileObj.versions).forEach(versionName => { - fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterRemove(); + fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove(); }); }); }, diff --git a/models/avatars.js b/models/avatars.js index e179da244..310a41783 100644 --- a/models/avatars.js +++ b/models/avatars.js @@ -34,13 +34,13 @@ Avatars = new FilesCollection({ Avatars.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } }); }, interceptDownload(http, fileObj, versionName) { - const ret = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).interceptDownload(http); + const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl); return ret; }, onAfterRemove(files) { files.forEach(fileObj => { Object.keys(fileObj.versions).forEach(versionName => { - fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterRemove(); + fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove(); }); }); }, diff --git a/models/lib/attachmentStoreStrategy.js b/models/lib/attachmentStoreStrategy.js index e525647af..6e7ebb2c8 100644 --- a/models/lib/attachmentStoreStrategy.js +++ b/models/lib/attachmentStoreStrategy.js @@ -19,12 +19,11 @@ export class AttachmentStoreStrategyGridFs extends FileStoreStrategyGridFs { /** constructor * @param gridFsBucket use this GridFS Bucket - * @param filesCollection the current FilesCollection instance * @param fileObj the current file object * @param versionName the current version */ - constructor(gridFsBucket, filesCollection, fileObj, versionName) { - super(gridFsBucket, filesCollection, fileObj, versionName); + constructor(gridFsBucket, fileObj, versionName) { + super(gridFsBucket, fileObj, versionName); } /** after successfull upload */ @@ -48,12 +47,11 @@ export class AttachmentStoreStrategyGridFs extends FileStoreStrategyGridFs { export class AttachmentStoreStrategyFilesystem extends FileStoreStrategyFilesystem { /** constructor - * @param filesCollection the current FilesCollection instance * @param fileObj the current file object * @param versionName the current version */ - constructor(filesCollection, fileObj, versionName) { - super(filesCollection, fileObj, versionName); + constructor(fileObj, versionName) { + super(fileObj, versionName); } /** after successfull upload */ diff --git a/models/lib/fileStoreStrategy.js b/models/lib/fileStoreStrategy.js index 22c9cbf18..661d727b3 100644 --- a/models/lib/fileStoreStrategy.js +++ b/models/lib/fileStoreStrategy.js @@ -20,12 +20,11 @@ export default class FileStoreStrategyFactory { } /** returns the right FileStoreStrategy - * @param filesCollection the current FilesCollection instance * @param fileObj the current file object * @param versionName the current version * @param use this storage, or if not set, get the storage from fileObj */ - getFileStrategy(filesCollection, fileObj, versionName, storage) { + getFileStrategy(fileObj, versionName, storage) { if (!storage) { storage = fileObj.versions[versionName].storage; if (!storage) { @@ -41,9 +40,9 @@ export default class FileStoreStrategyFactory { let ret; if ([STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS].includes(storage)) { if (storage == STORAGE_NAME_FILESYSTEM) { - ret = new this.classFileStoreStrategyFilesystem(filesCollection, fileObj, versionName); + ret = new this.classFileStoreStrategyFilesystem(fileObj, versionName); } else if (storage == STORAGE_NAME_GRIDFS) { - ret = new this.classFileStoreStrategyGridFs(this.gridFsBucket, filesCollection, fileObj, versionName); + ret = new this.classFileStoreStrategyGridFs(this.gridFsBucket, fileObj, versionName); } } return ret; @@ -54,12 +53,10 @@ export default class FileStoreStrategyFactory { class FileStoreStrategy { /** constructor - * @param filesCollection the current FilesCollection instance * @param fileObj the current file object * @param versionName the current version */ - constructor(filesCollection, fileObj, versionName) { - this.filesCollection = filesCollection; + constructor(fileObj, versionName) { this.fileObj = fileObj; this.versionName = versionName; } @@ -70,8 +67,9 @@ class FileStoreStrategy { /** download the file * @param http the current http request + * @param cacheControl cacheControl of FilesCollection */ - interceptDownload(http) { + interceptDownload(http, cacheControl) { } /** after file remove */ @@ -112,26 +110,26 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy { /** constructor * @param gridFsBucket use this GridFS Bucket - * @param filesCollection the current FilesCollection instance * @param fileObj the current file object * @param versionName the current version */ - constructor(gridFsBucket, filesCollection, fileObj, versionName) { - super(filesCollection, fileObj, versionName); + constructor(gridFsBucket, fileObj, versionName) { + super(fileObj, versionName); this.gridFsBucket = gridFsBucket; } /** download the file * @param http the current http request + * @param cacheControl cacheControl of FilesCollection */ - interceptDownload(http) { + interceptDownload(http, cacheControl) { const readStream = this.getReadStream(); const downloadFlag = http?.params?.query?.download; let ret = false; if (readStream) { ret = true; - httpStreamOutput(readStream, this.fileObj.name, http, downloadFlag, this.filesCollection.cacheControl); + httpStreamOutput(readStream, this.fileObj.name, http, downloadFlag, cacheControl); } return ret; @@ -233,12 +231,11 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy { export class FileStoreStrategyFilesystem extends FileStoreStrategy { /** constructor - * @param filesCollection the current FilesCollection instance * @param fileObj the current file object * @param versionName the current version */ - constructor(filesCollection, fileObj, versionName) { - super(filesCollection, fileObj, versionName); + constructor(fileObj, versionName) { + super(fileObj, versionName); } /** returns a read stream @@ -285,8 +282,8 @@ export class FileStoreStrategyFilesystem extends FileStoreStrategy { */ export const moveToStorage = function(fileObj, storageDestination, fileStoreStrategyFactory) { Object.keys(fileObj.versions).forEach(versionName => { - const strategyRead = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName); - const strategyWrite = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName, storageDestination); + const strategyRead = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName); + const strategyWrite = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName, storageDestination); if (strategyRead.constructor.name != strategyWrite.constructor.name) { const readStream = strategyRead.getReadStream(); diff --git a/models/lib/httpStream.js b/models/lib/httpStream.js index 953d83134..4b156a9c8 100644 --- a/models/lib/httpStream.js +++ b/models/lib/httpStream.js @@ -13,7 +13,9 @@ export const httpStreamOutput = function(readStream, name, http, downloadFlag, c http.response.end('not found'); }); - http.response.setHeader('Cache-Control', cacheControl); + if (cacheControl) { + http.response.setHeader('Cache-Control', cacheControl); + } http.response.setHeader('Content-Disposition', getContentDisposition(name, http?.params?.query?.download)); };