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/apps/meteor/app/file-upload/server/config/FileSystem.ts

142 lines
3.7 KiB

import fsp from 'fs/promises';
import { UploadFS } from '../../../../server/ufs';
import { settings } from '../../../settings/server';
Convert rocketchat-file-upload to main module structure (#13094) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix missed export * Fix canBeDeleted and canBeCreated function, remove async
7 years ago
import { FileUploadClass, FileUpload } from '../lib/FileUpload';
import { getFileRange, setRangeHeaders } from '../lib/ranges';
const FileSystemUploads = new FileUploadClass({
name: 'FileSystem:Uploads',
// store setted bellow
async get(file, req, res) {
if (!this.store || !file) {
return;
}
const filePath = await this.store.getFilePath(file._id, file);
const options: { start?: number; end?: number } = {};
try {
const stat = await fsp.stat(filePath);
if (!stat?.isFile()) {
res.writeHead(404);
res.end();
return;
}
file = FileUpload.addExtensionTo(file);
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(file.name || '')}`);
file.uploadedAt && res.setHeader('Last-Modified', file.uploadedAt.toUTCString());
res.setHeader('Content-Type', file.type || 'application/octet-stream');
if (req.headers.range) {
const range = getFileRange(file, req);
if (range) {
setRangeHeaders(range, file, res);
if (range.outOfRange) {
return;
}
options.start = range.start;
options.end = range.stop;
}
}
// set content-length if range has not set
if (!res.getHeader('Content-Length')) {
res.setHeader('Content-Length', file.size || 0);
}
(await this.store.getReadStream(file._id, file, options)).pipe(res);
} catch (e) {
res.writeHead(404);
res.end();
}
},
async copy(file, out) {
if (!this.store) {
return;
}
const filePath = await this.store.getFilePath(file._id, file);
try {
const stat = await fsp.stat(filePath);
if (stat?.isFile()) {
file = FileUpload.addExtensionTo(file);
(await this.store.getReadStream(file._id, file)).pipe(out);
}
} catch (e) {
out.end();
}
},
});
const FileSystemAvatars = new FileUploadClass({
name: 'FileSystem:Avatars',
// store setted bellow
async get(file, _req, res) {
if (!this.store) {
return;
}
const filePath = await this.store.getFilePath(file._id, file);
try {
const stat = await fsp.stat(filePath);
if (stat?.isFile()) {
file = FileUpload.addExtensionTo(file);
(await this.store.getReadStream(file._id, file)).pipe(res);
}
} catch (e) {
res.writeHead(404);
res.end();
}
},
});
const FileSystemUserDataFiles = new FileUploadClass({
name: 'FileSystem:UserDataFiles',
async get(file, _req, res) {
if (!this.store) {
return;
}
const filePath = await this.store.getFilePath(file._id, file);
try {
const stat = await fsp.stat(filePath);
if (stat?.isFile()) {
file = FileUpload.addExtensionTo(file);
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(file.name || '')}`);
file.uploadedAt && res.setHeader('Last-Modified', file.uploadedAt.toUTCString());
res.setHeader('Content-Type', file.type || '');
res.setHeader('Content-Length', file.size || 0);
(await this.store.getReadStream(file._id, file)).pipe(res);
}
} catch (e) {
res.writeHead(404);
res.end();
}
},
});
settings.watch('FileUpload_FileSystemPath', function () {
const options = {
Convert rocketchat-file-upload to main module structure (#13094) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix missed export * Fix canBeDeleted and canBeCreated function, remove async
7 years ago
path: settings.get('FileUpload_FileSystemPath'), // '/tmp/uploads/photos',
};
FileSystemUploads.store = FileUpload.configureUploadsStore('Local', FileSystemUploads.name, options);
FileSystemAvatars.store = FileUpload.configureUploadsStore('Local', FileSystemAvatars.name, options);
FileSystemUserDataFiles.store = FileUpload.configureUploadsStore('Local', FileSystemUserDataFiles.name, options);
// DEPRECATED backwards compatibility (remove)
UploadFS.getStores().fileSystem = UploadFS.getStores()[FileSystemUploads.name];
});