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);
},
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();
});
});
},

View file

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

View file

@ -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 */

View file

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

View file

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