mirror of
https://github.com/wekan/wekan.git
synced 2026-01-06 01:28:49 +01:00
Add support to validate attachment uploads by an external program
This commit is contained in:
parent
91b1713bf7
commit
b5d311c00c
9 changed files with 90 additions and 27 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import { Meteor } from 'meteor/meteor';
|
||||
import { FilesCollection } from 'meteor/ostrio:files';
|
||||
import { exec } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import { createBucket } from './lib/grid/createBucket';
|
||||
import fs from 'fs';
|
||||
import FileType from 'file-type';
|
||||
|
|
@ -7,12 +9,15 @@ import path from 'path';
|
|||
import { AttachmentStoreStrategyFilesystem, AttachmentStoreStrategyGridFs} from '/models/lib/attachmentStoreStrategy';
|
||||
import FileStoreStrategyFactory, {moveToStorage, rename, STORAGE_NAME_FILESYSTEM, STORAGE_NAME_GRIDFS} from '/models/lib/fileStoreStrategy';
|
||||
|
||||
let asyncExec;
|
||||
let attachmentUploadExternalProgram;
|
||||
let attachmentUploadMimeTypes = [];
|
||||
let attachmentUploadSize = 0;
|
||||
let attachmentBucket;
|
||||
let storagePath;
|
||||
|
||||
if (Meteor.isServer) {
|
||||
asyncExec = promisify(exec);
|
||||
attachmentBucket = createBucket('attachments');
|
||||
|
||||
if (process.env.ATTACHMENTS_UPLOAD_MIME_TYPES) {
|
||||
|
|
@ -28,6 +33,14 @@ if (Meteor.isServer) {
|
|||
}
|
||||
}
|
||||
|
||||
if (process.env.ATTACHMENTS_UPLOAD_EXTERNAL_PROGRAM) {
|
||||
attachmentUploadExternalProgram = process.env.ATTACHMENTS_UPLOAD_EXTERNAL_PROGRAM;
|
||||
|
||||
if (!attachmentUploadExternalProgram.includes("{file}")) {
|
||||
attachmentUploadExternalProgram = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
storagePath = path.join(process.env.WRITABLE_PATH, 'attachments');
|
||||
}
|
||||
|
||||
|
|
@ -56,26 +69,6 @@ Attachments = new FilesCollection({
|
|||
return ret;
|
||||
},
|
||||
onAfterUpload(fileObj) {
|
||||
let isValid = true;
|
||||
|
||||
if (attachmentUploadMimeTypes.length) {
|
||||
const mimeTypeResult = Promise.await(FileType.fromFile(fileObj.path));
|
||||
|
||||
const mimeType = (mimeTypeResult ? mimeTypeResult.mime : fileObj.type);
|
||||
const baseMimeType = mimeType.split('/', 1)[0];
|
||||
|
||||
isValid = attachmentUploadMimeTypes.includes(mimeType) || attachmentUploadMimeTypes.includes(baseMimeType + '/*') || attachmentUploadMimeTypes.includes('*');
|
||||
|
||||
if (!isValid) {
|
||||
console.log("Validation of uploaded file failed: file " + fileObj.path + " - mimetype " + mimeType);
|
||||
}
|
||||
}
|
||||
|
||||
if (attachmentUploadSize && fileObj.size > attachmentUploadSize) {
|
||||
console.log("Validation of uploaded file failed: file " + fileObj.path + " - size " + fileObj.size);
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
// current storage is the filesystem, update object and database
|
||||
Object.keys(fileObj.versions).forEach(versionName => {
|
||||
fileObj.versions[versionName].storage = STORAGE_NAME_FILESYSTEM;
|
||||
|
|
@ -83,12 +76,8 @@ Attachments = new FilesCollection({
|
|||
|
||||
Attachments.update({ _id: fileObj._id }, { $set: { "versions" : fileObj.versions } });
|
||||
|
||||
if (isValid) {
|
||||
let storage = fileObj.meta.copyStorage || STORAGE_NAME_GRIDFS;
|
||||
moveToStorage(fileObj, storage, fileStoreStrategyFactory);
|
||||
} else {
|
||||
this.remove(fileObj._id);
|
||||
}
|
||||
let storageDestination = fileObj.meta.copyStorage || STORAGE_NAME_GRIDFS;
|
||||
Meteor.defer(() => Meteor.call('validateAttachmentAndMoveToStorage', fileObj._id, storageDestination));
|
||||
},
|
||||
interceptDownload(http, fileObj, versionName) {
|
||||
const ret = fileStoreStrategyFactory.getFileStrategy(fileObj, versionName).interceptDownload(http, this.cacheControl);
|
||||
|
|
@ -148,6 +137,64 @@ if (Meteor.isServer) {
|
|||
const fileObj = Attachments.findOne({_id: fileObjId});
|
||||
rename(fileObj, newName, fileStoreStrategyFactory);
|
||||
},
|
||||
validateAttachment(fileObjId) {
|
||||
check(fileObjId, String);
|
||||
|
||||
const fileObj = Attachments.findOne({_id: fileObjId});
|
||||
let isValid = true;
|
||||
|
||||
if (attachmentUploadMimeTypes.length) {
|
||||
const mimeTypeResult = Promise.await(FileType.fromFile(fileObj.path));
|
||||
|
||||
const mimeType = (mimeTypeResult ? mimeTypeResult.mime : fileObj.type);
|
||||
const baseMimeType = mimeType.split('/', 1)[0];
|
||||
|
||||
isValid = attachmentUploadMimeTypes.includes(mimeType) || attachmentUploadMimeTypes.includes(baseMimeType + '/*') || attachmentUploadMimeTypes.includes('*');
|
||||
|
||||
if (!isValid) {
|
||||
console.log("Validation of uploaded file failed: file " + fileObj.path + " - mimetype " + mimeType);
|
||||
}
|
||||
}
|
||||
|
||||
if (attachmentUploadSize && fileObj.size > attachmentUploadSize) {
|
||||
console.log("Validation of uploaded file failed: file " + fileObj.path + " - size " + fileObj.size);
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
if (isValid && attachmentUploadExternalProgram) {
|
||||
let args = { ...attachmentUploadExternalProgram.args };
|
||||
|
||||
for (let key in args) {
|
||||
if (args[key] == "%file") {
|
||||
args[key] = fileObj.path;
|
||||
}
|
||||
}
|
||||
|
||||
Promise.await(asyncExec(attachmentUploadExternalProgram.replace("{file}", '"' + fileObj.path + '"')));
|
||||
isValid = fs.existsSync(fileObj.path);
|
||||
|
||||
if (!isValid) {
|
||||
console.log("Validation of uploaded file failed: file " + fileObj.path + " has been deleted externally");
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValid) {
|
||||
Attachments.remove(fileObjId);
|
||||
}
|
||||
},
|
||||
validateAttachmentAndMoveToStorage(fileObjId, storageDestination) {
|
||||
check(fileObjId, String);
|
||||
check(storageDestination, String);
|
||||
|
||||
Meteor.call('validateAttachment', fileObjId);
|
||||
|
||||
const fileObj = Attachments.findOne({_id: fileObjId});
|
||||
|
||||
if (fileObj) {
|
||||
console.debug("Validation of uploaded file completed: file " + fileObj.path + " - storage destination " + storageDestination);
|
||||
Meteor.defer(() => Meteor.call('moveAttachmentToStorage', fileObjId, storageDestination));
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Meteor.startup(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue