snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
This commit is contained in:
129
apps/meteor/server/lib/media/file-upload/config/AmazonS3.ts
Normal file
129
apps/meteor/server/lib/media/file-upload/config/AmazonS3.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
|
||||
import _ from 'underscore';
|
||||
|
||||
import { forceDownload } from './helper';
|
||||
import { settings } from '../../../../settings';
|
||||
import { SystemLogger } from '../../../logger/system';
|
||||
import { FileUploadClass, FileUpload } from '../lib/FileUpload';
|
||||
import type { S3Options } from '../ufs/AmazonS3/server';
|
||||
import '../ufs/AmazonS3/server';
|
||||
|
||||
const hasScheme = (value: string) => /^[a-z][a-z0-9+.-]*:\/\//i.test(value);
|
||||
|
||||
const get: FileUploadClass['get'] = async function (this: FileUploadClass, file, req, res) {
|
||||
const forcedDownload = forceDownload(req);
|
||||
|
||||
const fileUrl = await this.store.getRedirectURL(file, forcedDownload);
|
||||
if (!fileUrl || !file.store) {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const storeType = file.store.split(':').pop();
|
||||
if (settings.get(`FileUpload_S3_Proxy_${storeType}`)) {
|
||||
const request = /^https:/.test(fileUrl) ? https : http;
|
||||
|
||||
FileUpload.proxyFile(file.name || '', fileUrl, forcedDownload, request, req, res);
|
||||
return;
|
||||
}
|
||||
|
||||
FileUpload.redirectToFile(fileUrl, req, res);
|
||||
};
|
||||
|
||||
const copy: FileUploadClass['copy'] = async function (this: FileUploadClass, file, out) {
|
||||
const fileUrl = await this.store.getRedirectURL(file);
|
||||
if (!fileUrl) {
|
||||
out.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const request = /^https:/.test(fileUrl) ? https : http;
|
||||
return new Promise((resolve) => {
|
||||
request.get(fileUrl, (fileRes) => fileRes.pipe(out).on('finish', () => resolve()));
|
||||
});
|
||||
};
|
||||
|
||||
const AmazonS3Uploads = new FileUploadClass({
|
||||
name: 'AmazonS3:Uploads',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const AmazonS3Avatars = new FileUploadClass({
|
||||
name: 'AmazonS3:Avatars',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const AmazonS3UserDataFiles = new FileUploadClass({
|
||||
name: 'AmazonS3:UserDataFiles',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const configure = _.debounce(() => {
|
||||
const Bucket = settings.get<string>('FileUpload_S3_Bucket');
|
||||
const Acl = settings.get<string>('FileUpload_S3_Acl');
|
||||
const AWSAccessKeyId = settings.get<string>('FileUpload_S3_AWSAccessKeyId');
|
||||
const AWSSecretAccessKey = settings.get<string>('FileUpload_S3_AWSSecretAccessKey');
|
||||
const URLExpiryTimeSpan = settings.get<number>('FileUpload_S3_URLExpiryTimeSpan');
|
||||
const Region = settings.get<string>('FileUpload_S3_Region');
|
||||
const ForcePathStyle = settings.get<boolean>('FileUpload_S3_ForcePathStyle');
|
||||
// const CDN = RocketChat.settings.get('FileUpload_S3_CDN');
|
||||
const BucketURL = settings.get<string>('FileUpload_S3_BucketURL');
|
||||
|
||||
if (!Bucket) {
|
||||
return;
|
||||
}
|
||||
|
||||
const config: Omit<S3Options, 'name' | 'getPath'> = {
|
||||
connection: {
|
||||
forcePathStyle: ForcePathStyle,
|
||||
followRegionRedirects: true,
|
||||
},
|
||||
params: {
|
||||
Bucket,
|
||||
ACL: Acl,
|
||||
},
|
||||
URLExpiryTimeSpan,
|
||||
};
|
||||
|
||||
if (Region) {
|
||||
config.connection.region = Region;
|
||||
}
|
||||
|
||||
// Back-compat: AWS SDK v2 defaulted unset region to us-east-1; v3 throws.
|
||||
if (!Region && !process.env.AWS_REGION) {
|
||||
config.connection.region = 'us-east-1';
|
||||
SystemLogger.warn(
|
||||
'FileUpload_S3_Region is empty and AWS_REGION is not set; defaulting to us-east-1. Set FileUpload_S3_Region or AWS_REGION to silence this warning.',
|
||||
);
|
||||
}
|
||||
|
||||
if (AWSAccessKeyId && AWSSecretAccessKey) {
|
||||
config.connection.credentials = {
|
||||
accessKeyId: AWSAccessKeyId,
|
||||
secretAccessKey: AWSSecretAccessKey,
|
||||
};
|
||||
}
|
||||
|
||||
// Back-compat: AWS SDK v2 accepted scheme-less endpoints; v3 throws.
|
||||
if (BucketURL) {
|
||||
const isValidScheme = hasScheme(BucketURL);
|
||||
config.connection.endpoint = isValidScheme ? BucketURL : `https://${BucketURL}`;
|
||||
if (!isValidScheme) {
|
||||
SystemLogger.warn(`FileUpload_S3_BucketURL "${BucketURL}" has no scheme; defaulting to "https://${BucketURL}".`);
|
||||
}
|
||||
}
|
||||
|
||||
AmazonS3Uploads.store = FileUpload.configureUploadsStore('AmazonS3', AmazonS3Uploads.name, config);
|
||||
AmazonS3Avatars.store = FileUpload.configureUploadsStore('AmazonS3', AmazonS3Avatars.name, config);
|
||||
AmazonS3UserDataFiles.store = FileUpload.configureUploadsStore('AmazonS3', AmazonS3UserDataFiles.name, config);
|
||||
}, 500);
|
||||
|
||||
settings.watchByRegex(/^FileUpload_S3_/, configure);
|
||||
144
apps/meteor/server/lib/media/file-upload/config/FileSystem.ts
Normal file
144
apps/meteor/server/lib/media/file-upload/config/FileSystem.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import fsp from 'node:fs/promises';
|
||||
|
||||
import { getContentDisposition } from './helper';
|
||||
import { settings } from '../../../../settings';
|
||||
import { UploadFS } from '../../../../ufs';
|
||||
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', `${getContentDisposition(req)}; filename*=UTF-8''${encodeURIComponent(file.name || '')}`);
|
||||
file.uploadedAt && res.setHeader('Last-Modified', file.uploadedAt.toUTCString());
|
||||
res.setHeader('Content-Type', file.type || 'application/octet-stream');
|
||||
res.setHeader('Accept-Ranges', 'bytes');
|
||||
|
||||
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', () => {
|
||||
const options = {
|
||||
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];
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
|
||||
import _ from 'underscore';
|
||||
|
||||
import { forceDownload } from './helper';
|
||||
import { settings } from '../../../../settings';
|
||||
import { FileUploadClass, FileUpload } from '../lib/FileUpload';
|
||||
import '../ufs/GoogleStorage/server';
|
||||
|
||||
const get: FileUploadClass['get'] = async function (this: FileUploadClass, file, req, res) {
|
||||
const forcedDownload = forceDownload(req);
|
||||
|
||||
const fileUrl = await this.store.getRedirectURL(file, forcedDownload);
|
||||
if (!fileUrl || !file.store) {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const storeType = file.store.split(':').pop();
|
||||
if (settings.get(`FileUpload_GoogleStorage_Proxy_${storeType}`)) {
|
||||
const request = /^https:/.test(fileUrl) ? https : http;
|
||||
|
||||
FileUpload.proxyFile(file.name || '', fileUrl, forcedDownload, request, req, res);
|
||||
return;
|
||||
}
|
||||
|
||||
FileUpload.redirectToFile(fileUrl, req, res);
|
||||
};
|
||||
|
||||
const copy: FileUploadClass['copy'] = async function (this: FileUploadClass, file, out) {
|
||||
const fileUrl = await this.store.getRedirectURL(file, false);
|
||||
|
||||
if (!fileUrl) {
|
||||
out.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const request = /^https:/.test(fileUrl) ? https : http;
|
||||
return new Promise((resolve) => {
|
||||
request.get(fileUrl, (fileRes) => fileRes.pipe(out).on('finish', () => resolve()));
|
||||
});
|
||||
};
|
||||
|
||||
const GoogleCloudStorageUploads = new FileUploadClass({
|
||||
name: 'GoogleCloudStorage:Uploads',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const GoogleCloudStorageAvatars = new FileUploadClass({
|
||||
name: 'GoogleCloudStorage:Avatars',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const GoogleCloudStorageUserDataFiles = new FileUploadClass({
|
||||
name: 'GoogleCloudStorage:UserDataFiles',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const configure = _.debounce(() => {
|
||||
const bucket = settings.get('FileUpload_GoogleStorage_Bucket');
|
||||
const projectId = settings.get('FileUpload_GoogleStorage_ProjectId');
|
||||
const accessId = settings.get('FileUpload_GoogleStorage_AccessId');
|
||||
const secret = settings.get('FileUpload_GoogleStorage_Secret');
|
||||
const URLExpiryTimeSpan = settings.get<number>('FileUpload_GoogleStorage_URLExpiryTimeSpan');
|
||||
|
||||
if (!bucket || !accessId || !secret) {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = {
|
||||
connection: {
|
||||
credentials: {
|
||||
client_email: accessId,
|
||||
private_key: secret,
|
||||
},
|
||||
projectId,
|
||||
},
|
||||
bucket,
|
||||
URLExpiryTimeSpan,
|
||||
};
|
||||
|
||||
GoogleCloudStorageUploads.store = FileUpload.configureUploadsStore('GoogleStorage', GoogleCloudStorageUploads.name, config);
|
||||
GoogleCloudStorageAvatars.store = FileUpload.configureUploadsStore('GoogleStorage', GoogleCloudStorageAvatars.name, config);
|
||||
GoogleCloudStorageUserDataFiles.store = FileUpload.configureUploadsStore('GoogleStorage', GoogleCloudStorageUserDataFiles.name, config);
|
||||
}, 500);
|
||||
|
||||
settings.watchByRegex(/^FileUpload_GoogleStorage_/, configure);
|
||||
211
apps/meteor/server/lib/media/file-upload/config/GridFS.ts
Normal file
211
apps/meteor/server/lib/media/file-upload/config/GridFS.ts
Normal file
@@ -0,0 +1,211 @@
|
||||
import type * as http from 'node:http';
|
||||
import type { TransformCallback, TransformOptions } from 'node:stream';
|
||||
import stream from 'node:stream';
|
||||
import zlib from 'node:zlib';
|
||||
|
||||
import type { IUpload } from '@rocket.chat/core-typings';
|
||||
import { Logger } from '@rocket.chat/logger';
|
||||
|
||||
import { getContentDisposition } from './helper';
|
||||
import { UploadFS } from '../../../../ufs';
|
||||
import { FileUploadClass, FileUpload } from '../lib/FileUpload';
|
||||
import { getFileRange, setRangeHeaders } from '../lib/ranges';
|
||||
|
||||
const logger = new Logger('FileUpload');
|
||||
|
||||
class ExtractRange extends stream.Transform {
|
||||
private start: number;
|
||||
|
||||
private stop: number;
|
||||
|
||||
private bytes_read: number;
|
||||
|
||||
constructor(options: TransformOptions & { start: number; stop: number }) {
|
||||
super(options);
|
||||
|
||||
this.start = options.start;
|
||||
this.stop = options.stop;
|
||||
this.bytes_read = 0;
|
||||
}
|
||||
|
||||
override _transform(chunk: any, _enc: BufferEncoding, cb: TransformCallback) {
|
||||
if (this.bytes_read > this.stop) {
|
||||
// done reading
|
||||
this.end();
|
||||
} else if (this.bytes_read + chunk.length < this.start) {
|
||||
// this chunk is still before the start byte
|
||||
} else {
|
||||
let start;
|
||||
let stop;
|
||||
|
||||
if (this.start <= this.bytes_read) {
|
||||
start = 0;
|
||||
} else {
|
||||
start = this.start - this.bytes_read;
|
||||
}
|
||||
if (this.stop - this.bytes_read + 1 < chunk.length) {
|
||||
stop = this.stop - this.bytes_read + 1;
|
||||
} else {
|
||||
stop = chunk.length;
|
||||
}
|
||||
const newchunk = chunk.slice(start, stop);
|
||||
this.push(newchunk);
|
||||
}
|
||||
this.bytes_read += chunk.length;
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
// code from: https://github.com/jalik/jalik-ufs/blob/master/ufs-server.js#L310
|
||||
const readFromGridFS = async function (
|
||||
storeName: string | undefined,
|
||||
fileId: string,
|
||||
file: IUpload,
|
||||
req: http.IncomingMessage,
|
||||
res: http.ServerResponse,
|
||||
) {
|
||||
if (!storeName) {
|
||||
return;
|
||||
}
|
||||
const store = UploadFS.getStore(storeName);
|
||||
const rs = await store.getReadStream(fileId, file);
|
||||
const ws = new stream.PassThrough();
|
||||
|
||||
res.setHeader('Accept-Ranges', 'bytes');
|
||||
|
||||
[rs, ws].forEach((stream) =>
|
||||
stream.on('error', (err) => {
|
||||
store.onReadError.call(store, err, fileId, file);
|
||||
res.end();
|
||||
}),
|
||||
);
|
||||
|
||||
ws.on('close', () => {
|
||||
// Close output stream at the end
|
||||
ws.emit('end');
|
||||
});
|
||||
|
||||
// Transform stream
|
||||
store.transformRead(rs, ws, fileId, file, req);
|
||||
|
||||
const range = getFileRange(file, req);
|
||||
if (range) {
|
||||
setRangeHeaders(range, file, res);
|
||||
|
||||
if (range.outOfRange) {
|
||||
return;
|
||||
}
|
||||
|
||||
logger.debug('File upload extracting range');
|
||||
ws.pipe(new ExtractRange({ start: range.start, stop: range.stop })).pipe(res);
|
||||
return;
|
||||
}
|
||||
|
||||
const accept = (Array.isArray(req.headers['accept-encoding']) ? req.headers['accept-encoding'][0] : req.headers['accept-encoding']) || '';
|
||||
|
||||
// Compress data using gzip
|
||||
if (accept.match(/\bgzip\b/)) {
|
||||
res.setHeader('Content-Encoding', 'gzip');
|
||||
res.removeHeader('Content-Length');
|
||||
res.writeHead(200);
|
||||
ws.pipe(zlib.createGzip()).pipe(res);
|
||||
return;
|
||||
}
|
||||
|
||||
// Compress data using deflate
|
||||
if (accept.match(/\bdeflate\b/)) {
|
||||
res.setHeader('Content-Encoding', 'deflate');
|
||||
res.removeHeader('Content-Length');
|
||||
res.writeHead(200);
|
||||
ws.pipe(zlib.createDeflate()).pipe(res);
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(200);
|
||||
ws.pipe(res);
|
||||
};
|
||||
|
||||
const copyFromGridFS = async function (storeName: string | undefined, fileId: string, file: IUpload, out: stream.Writable) {
|
||||
if (!storeName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const store = UploadFS.getStore(storeName);
|
||||
const rs = await store.getReadStream(fileId, file);
|
||||
|
||||
[rs, out].forEach((stream) =>
|
||||
stream.on('error', (err) => {
|
||||
store.onReadError.call(store, err, fileId, file);
|
||||
out.end();
|
||||
}),
|
||||
);
|
||||
|
||||
rs.pipe(out);
|
||||
};
|
||||
|
||||
FileUpload.configureUploadsStore('GridFS', 'GridFS:Uploads', {
|
||||
collectionName: 'rocketchat_uploads',
|
||||
});
|
||||
|
||||
FileUpload.configureUploadsStore('GridFS', 'GridFS:UserDataFiles', {
|
||||
collectionName: 'rocketchat_userDataFiles',
|
||||
});
|
||||
|
||||
// DEPRECATED: backwards compatibility (remove)
|
||||
UploadFS.getStores().rocketchat_uploads = UploadFS.getStores()['GridFS:Uploads'];
|
||||
|
||||
FileUpload.configureUploadsStore('GridFS', 'GridFS:Avatars', {
|
||||
collectionName: 'rocketchat_avatars',
|
||||
});
|
||||
|
||||
new FileUploadClass({
|
||||
name: 'GridFS:Uploads',
|
||||
|
||||
async get(file, req, res) {
|
||||
file = FileUpload.addExtensionTo(file);
|
||||
|
||||
res.setHeader('Content-Disposition', `${getContentDisposition(req)}; filename*=UTF-8''${encodeURIComponent(file.name || '')}`);
|
||||
file.uploadedAt && res.setHeader('Last-Modified', file.uploadedAt.toUTCString());
|
||||
res.setHeader('Content-Type', file.type || 'application/octet-stream');
|
||||
res.setHeader('Content-Length', file.size || 0);
|
||||
|
||||
await readFromGridFS(file.store, file._id, file, req, res);
|
||||
},
|
||||
|
||||
async copy(file, out) {
|
||||
await copyFromGridFS(file.store, file._id, file, out);
|
||||
},
|
||||
});
|
||||
|
||||
new FileUploadClass({
|
||||
name: 'GridFS:UserDataFiles',
|
||||
|
||||
async get(file, req, res) {
|
||||
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 readFromGridFS(file.store, file._id, file, req, res);
|
||||
},
|
||||
|
||||
async copy(file, out) {
|
||||
await copyFromGridFS(file.store, file._id, file, out);
|
||||
},
|
||||
});
|
||||
|
||||
new FileUploadClass({
|
||||
name: 'GridFS:Avatars',
|
||||
|
||||
async get(file, req, res) {
|
||||
file = FileUpload.addExtensionTo(file);
|
||||
|
||||
await readFromGridFS(file.store, file._id, file, req, res);
|
||||
},
|
||||
|
||||
async copy(file, out) {
|
||||
await copyFromGridFS(file.store, file._id, file, out);
|
||||
},
|
||||
});
|
||||
74
apps/meteor/server/lib/media/file-upload/config/Webdav.ts
Normal file
74
apps/meteor/server/lib/media/file-upload/config/Webdav.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import _ from 'underscore';
|
||||
|
||||
import { settings } from '../../../../settings';
|
||||
import { SystemLogger } from '../../../logger/system';
|
||||
import { FileUploadClass, FileUpload } from '../lib/FileUpload';
|
||||
import '../ufs/Webdav/server';
|
||||
|
||||
const get: FileUploadClass['get'] = async function (this: FileUploadClass, file, _req, res) {
|
||||
(await this.store.getReadStream(file._id, file))
|
||||
.on('error', () => {
|
||||
SystemLogger.error('An error ocurred when fetching the file');
|
||||
res.writeHead(503);
|
||||
res.end();
|
||||
})
|
||||
.on('data', (chunk) => {
|
||||
res.write(chunk);
|
||||
})
|
||||
.on('end', res.end.bind(res));
|
||||
};
|
||||
|
||||
const copy: FileUploadClass['copy'] = async function (this: FileUploadClass, file, out) {
|
||||
return new Promise(async (resolve) => {
|
||||
(await this.store.getReadStream(file._id, file)).pipe(out).on('finish', () => resolve());
|
||||
});
|
||||
};
|
||||
|
||||
const WebdavUploads = new FileUploadClass({
|
||||
name: 'Webdav:Uploads',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const WebdavAvatars = new FileUploadClass({
|
||||
name: 'Webdav:Avatars',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const WebdavUserDataFiles = new FileUploadClass({
|
||||
name: 'Webdav:UserDataFiles',
|
||||
get,
|
||||
copy,
|
||||
// store setted bellow
|
||||
});
|
||||
|
||||
const configure = _.debounce(() => {
|
||||
const uploadFolderPath = settings.get('FileUpload_Webdav_Upload_Folder_Path');
|
||||
const server = settings.get('FileUpload_Webdav_Server_URL');
|
||||
const username = settings.get('FileUpload_Webdav_Username');
|
||||
const password = settings.get('FileUpload_Webdav_Password');
|
||||
|
||||
if (!server || !username || !password) {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = {
|
||||
connection: {
|
||||
credentials: {
|
||||
server,
|
||||
username,
|
||||
password,
|
||||
},
|
||||
},
|
||||
uploadFolderPath,
|
||||
};
|
||||
|
||||
WebdavUploads.store = FileUpload.configureUploadsStore('Webdav', WebdavUploads.name, config);
|
||||
WebdavAvatars.store = FileUpload.configureUploadsStore('Webdav', WebdavAvatars.name, config);
|
||||
WebdavUserDataFiles.store = FileUpload.configureUploadsStore('Webdav', WebdavUserDataFiles.name, config);
|
||||
}, 500);
|
||||
|
||||
settings.watchByRegex(/^FileUpload_Webdav_/, configure);
|
||||
@@ -0,0 +1,26 @@
|
||||
import _ from 'underscore';
|
||||
|
||||
import { settings } from '../../../../settings';
|
||||
import { UploadFS } from '../../../../ufs';
|
||||
import { SystemLogger } from '../../../logger/system';
|
||||
import './AmazonS3';
|
||||
import './FileSystem';
|
||||
import './GoogleStorage';
|
||||
import './GridFS';
|
||||
import './Webdav';
|
||||
|
||||
const configStore = _.debounce(() => {
|
||||
const store = settings.get('FileUpload_Storage_Type');
|
||||
|
||||
if (store) {
|
||||
SystemLogger.info({
|
||||
msg: 'Setting default file store',
|
||||
store,
|
||||
});
|
||||
UploadFS.getStores().Avatars = UploadFS.getStore(`${store}:Avatars`);
|
||||
UploadFS.getStores().Uploads = UploadFS.getStore(`${store}:Uploads`);
|
||||
UploadFS.getStores().UserDataFiles = UploadFS.getStore(`${store}:UserDataFiles`);
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
settings.watchByRegex(/^FileUpload_/, configStore);
|
||||
21
apps/meteor/server/lib/media/file-upload/config/helper.ts
Normal file
21
apps/meteor/server/lib/media/file-upload/config/helper.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type http from 'node:http';
|
||||
import URL from 'node:url';
|
||||
|
||||
export const forceDownload = (req: http.IncomingMessage): boolean => {
|
||||
const { query } = URL.parse(req.url || '', true);
|
||||
|
||||
const forceDownload = typeof query.download !== 'undefined';
|
||||
if (forceDownload) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return query.contentDisposition === 'attachment';
|
||||
};
|
||||
|
||||
export const getContentDisposition = (req: http.IncomingMessage): string => {
|
||||
const { query } = URL.parse(req.url || '', true);
|
||||
if (query.contentDisposition === 'inline') {
|
||||
return 'inline';
|
||||
}
|
||||
return 'attachment';
|
||||
};
|
||||
Reference in New Issue
Block a user