The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-file-upload/server/lib/FileUpload.js

44 lines
974 B

/* globals FileUpload:true */
import mime from 'mime-type/with-db';
FileUpload.handlers = {};
FileUpload.addHandler = function(store, handler) {
this.handlers[store] = handler;
};
FileUpload.delete = function(fileId) {
const file = RocketChat.models.Uploads.findOneById(fileId);
if (!file) {
return;
}
this.handlers[file.store].delete(file);
return RocketChat.models.Uploads.deleteFile(file._id);
};
FileUpload.get = function(file, req, res, next) {
if (file.store && this.handlers && this.handlers[file.store] && this.handlers[file.store].get) {
this.handlers[file.store].get.call(this, file, req, res, next);
} else {
res.writeHead(404);
res.end();
return;
}
};
FileUpload.addExtensionTo = function(file) {
if (mime.lookup(file.name) === file.type) {
return file;
}
const ext = mime.extension(file.type);
if (ext && false === new RegExp(`\.${ ext }$`, 'i').test(file.name)) {
file.name = `${ file.name }.${ ext }`;
}
return file;
};