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/config/FileSystem.js

84 lines
2.4 KiB

/* globals FileUpload, UploadFS */
import _ from 'underscore';
import fs from 'fs';
import { FileUploadClass } from '../lib/FileUpload';
const FileSystemUploads = new FileUploadClass({
name: 'FileSystem:Uploads',
// store setted bellow
get(file, req, res) {
const filePath = this.store.getFilePath(file._id, file);
try {
const stat = Meteor.wrapAsync(fs.stat)(filePath);
if (stat && stat.isFile()) {
file = FileUpload.addExtensionTo(file);
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${ encodeURIComponent(file.name) }`);
res.setHeader('Last-Modified', file.uploadedAt.toUTCString());
res.setHeader('Content-Type', file.type);
res.setHeader('Content-Length', file.size);
this.store.getReadStream(file._id, file).pipe(res);
}
} catch (e) {
res.writeHead(404);
res.end();
return;
}
}
});
const FileSystemAvatars = new FileUploadClass({
name: 'FileSystem:Avatars',
// store setted bellow
get(file, req, res) {
const reqModifiedHeader = req.headers['if-modified-since'];
if (reqModifiedHeader) {
if (reqModifiedHeader === (file.uploadedAt && file.uploadedAt.toUTCString())) {
res.setHeader('Last-Modified', reqModifiedHeader);
res.writeHead(304);
res.end();
return;
}
}
const filePath = this.store.getFilePath(file._id, file);
try {
const stat = Meteor.wrapAsync(fs.stat)(filePath);
if (stat && stat.isFile()) {
file = FileUpload.addExtensionTo(file);
res.setHeader('Content-Disposition', 'inline');
res.setHeader('Last-Modified', file.uploadedAt.toUTCString());
res.setHeader('Content-Type', file.type);
res.setHeader('Content-Length', file.size);
this.store.getReadStream(file._id, file).pipe(res);
}
} catch (e) {
res.writeHead(404);
res.end();
return;
}
}
});
const createFileSystemStore = _.debounce(function() {
const options = {
path: RocketChat.settings.get('FileUpload_FileSystemPath') //'/tmp/uploads/photos',
};
FileSystemUploads.store = FileUpload.configureUploadsStore('Local', FileSystemUploads.name, options);
FileSystemAvatars.store = FileUpload.configureUploadsStore('Local', FileSystemAvatars.name, options);
// DEPRECATED backwards compatibililty (remove)
UploadFS.getStores()['fileSystem'] = UploadFS.getStores()[FileSystemUploads.name];
}, 500);
RocketChat.settings.get('FileUpload_FileSystemPath', createFileSystemStore);