Remove filesCollection from FileStoreStrategy classes

This commit is contained in:
Martin Filser 2022-04-07 23:06:16 +02:00
parent e75f423edd
commit 9d587e76ab
5 changed files with 26 additions and 29 deletions

View file

@ -40,13 +40,13 @@ Attachments = new FilesCollection({
moveToStorage(fileObj, STORAGE_NAME_GRIDFS, fileStoreStrategyFactory); moveToStorage(fileObj, STORAGE_NAME_GRIDFS, fileStoreStrategyFactory);
}, },
interceptDownload(http, fileObj, versionName) { 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; return ret;
}, },
onAfterRemove(files) { onAfterRemove(files) {
files.forEach(fileObj => { files.forEach(fileObj => {
Object.keys(fileObj.versions).forEach(versionName => { Object.keys(fileObj.versions).forEach(versionName => {
fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterRemove(); fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
}); });
}); });
}, },

View file

@ -34,13 +34,13 @@ Avatars = new FilesCollection({
Avatars.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } }); Avatars.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
}, },
interceptDownload(http, fileObj, versionName) { 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; return ret;
}, },
onAfterRemove(files) { onAfterRemove(files) {
files.forEach(fileObj => { files.forEach(fileObj => {
Object.keys(fileObj.versions).forEach(versionName => { Object.keys(fileObj.versions).forEach(versionName => {
fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName).onAfterRemove(); fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).onAfterRemove();
}); });
}); });
}, },

View file

@ -19,12 +19,11 @@ export class AttachmentStoreStrategyGridFs extends FileStoreStrategyGridFs {
/** constructor /** constructor
* @param gridFsBucket use this GridFS Bucket * @param gridFsBucket use this GridFS Bucket
* @param filesCollection the current FilesCollection instance
* @param fileObj the current file object * @param fileObj the current file object
* @param versionName the current version * @param versionName the current version
*/ */
constructor(gridFsBucket, filesCollection, fileObj, versionName) { constructor(gridFsBucket, fileObj, versionName) {
super(gridFsBucket, filesCollection, fileObj, versionName); super(gridFsBucket, fileObj, versionName);
} }
/** after successfull upload */ /** after successfull upload */
@ -48,12 +47,11 @@ export class AttachmentStoreStrategyGridFs extends FileStoreStrategyGridFs {
export class AttachmentStoreStrategyFilesystem extends FileStoreStrategyFilesystem { export class AttachmentStoreStrategyFilesystem extends FileStoreStrategyFilesystem {
/** constructor /** constructor
* @param filesCollection the current FilesCollection instance
* @param fileObj the current file object * @param fileObj the current file object
* @param versionName the current version * @param versionName the current version
*/ */
constructor(filesCollection, fileObj, versionName) { constructor(fileObj, versionName) {
super(filesCollection, fileObj, versionName); super(fileObj, versionName);
} }
/** after successfull upload */ /** after successfull upload */

View file

@ -20,12 +20,11 @@ export default class FileStoreStrategyFactory {
} }
/** returns the right FileStoreStrategy /** returns the right FileStoreStrategy
* @param filesCollection the current FilesCollection instance
* @param fileObj the current file object * @param fileObj the current file object
* @param versionName the current version * @param versionName the current version
* @param use this storage, or if not set, get the storage from fileObj * @param use this storage, or if not set, get the storage from fileObj
*/ */
getFileStrategy(filesCollection, fileObj, versionName, storage) { getFileStrategy(fileObj, versionName, storage) {
if (!storage) { if (!storage) {
storage = fileObj.versions[versionName].storage; storage = fileObj.versions[versionName].storage;
if (!storage) { if (!storage) {
@ -41,9 +40,9 @@ export default class FileStoreStrategyFactory {
let ret; let ret;
if ([STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS].includes(storage)) { if ([STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS].includes(storage)) {
if (storage == STORAGE_NAME_FILESYSTEM) { if (storage == STORAGE_NAME_FILESYSTEM) {
ret = new this.classFileStoreStrategyFilesystem(filesCollection, fileObj, versionName); ret = new this.classFileStoreStrategyFilesystem(fileObj, versionName);
} else if (storage == STORAGE_NAME_GRIDFS) { } 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; return ret;
@ -54,12 +53,10 @@ export default class FileStoreStrategyFactory {
class FileStoreStrategy { class FileStoreStrategy {
/** constructor /** constructor
* @param filesCollection the current FilesCollection instance
* @param fileObj the current file object * @param fileObj the current file object
* @param versionName the current version * @param versionName the current version
*/ */
constructor(filesCollection, fileObj, versionName) { constructor(fileObj, versionName) {
this.filesCollection = filesCollection;
this.fileObj = fileObj; this.fileObj = fileObj;
this.versionName = versionName; this.versionName = versionName;
} }
@ -70,8 +67,9 @@ class FileStoreStrategy {
/** download the file /** download the file
* @param http the current http request * @param http the current http request
* @param cacheControl cacheControl of FilesCollection
*/ */
interceptDownload(http) { interceptDownload(http, cacheControl) {
} }
/** after file remove */ /** after file remove */
@ -112,26 +110,26 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy {
/** constructor /** constructor
* @param gridFsBucket use this GridFS Bucket * @param gridFsBucket use this GridFS Bucket
* @param filesCollection the current FilesCollection instance
* @param fileObj the current file object * @param fileObj the current file object
* @param versionName the current version * @param versionName the current version
*/ */
constructor(gridFsBucket, filesCollection, fileObj, versionName) { constructor(gridFsBucket, fileObj, versionName) {
super(filesCollection, fileObj, versionName); super(fileObj, versionName);
this.gridFsBucket = gridFsBucket; this.gridFsBucket = gridFsBucket;
} }
/** download the file /** download the file
* @param http the current http request * @param http the current http request
* @param cacheControl cacheControl of FilesCollection
*/ */
interceptDownload(http) { interceptDownload(http, cacheControl) {
const readStream = this.getReadStream(); const readStream = this.getReadStream();
const downloadFlag = http?.params?.query?.download; const downloadFlag = http?.params?.query?.download;
let ret = false; let ret = false;
if (readStream) { if (readStream) {
ret = true; ret = true;
httpStreamOutput(readStream, this.fileObj.name, http, downloadFlag, this.filesCollection.cacheControl); httpStreamOutput(readStream, this.fileObj.name, http, downloadFlag, cacheControl);
} }
return ret; return ret;
@ -233,12 +231,11 @@ export class FileStoreStrategyGridFs extends FileStoreStrategy {
export class FileStoreStrategyFilesystem extends FileStoreStrategy { export class FileStoreStrategyFilesystem extends FileStoreStrategy {
/** constructor /** constructor
* @param filesCollection the current FilesCollection instance
* @param fileObj the current file object * @param fileObj the current file object
* @param versionName the current version * @param versionName the current version
*/ */
constructor(filesCollection, fileObj, versionName) { constructor(fileObj, versionName) {
super(filesCollection, fileObj, versionName); super(fileObj, versionName);
} }
/** returns a read stream /** returns a read stream
@ -285,8 +282,8 @@ export class FileStoreStrategyFilesystem extends FileStoreStrategy {
*/ */
export const moveToStorage = function(fileObj, storageDestination, fileStoreStrategyFactory) { export const moveToStorage = function(fileObj, storageDestination, fileStoreStrategyFactory) {
Object.keys(fileObj.versions).forEach(versionName => { Object.keys(fileObj.versions).forEach(versionName => {
const strategyRead = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName); const strategyRead = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName);
const strategyWrite = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName, storageDestination); const strategyWrite = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName, storageDestination);
if (strategyRead.constructor.name != strategyWrite.constructor.name) { if (strategyRead.constructor.name != strategyWrite.constructor.name) {
const readStream = strategyRead.getReadStream(); const readStream = strategyRead.getReadStream();

View file

@ -13,7 +13,9 @@ export const httpStreamOutput = function(readStream, name, http, downloadFlag, c
http.response.end('not found'); 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)); http.response.setHeader('Content-Disposition', getContentDisposition(name, http?.params?.query?.download));
}; };