Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions apps/meteor/server/lib/media/file-upload/lib/FileUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ import { validateAndDecodeJWT, generateJWT } from '../../../utils/lib/JWTHelper'
import { fileUploadIsValidContentType } from '../../../utils/restrictions';

const cookie = new Cookies();

// Caps GIF frames decoded per thumbnail; limitInputPixels doesn't bound frame count.
const MAX_ANIMATED_THUMBNAIL_PAGES = 100;

let maxFileSize = 0;

settings.watch('FileUpload_MaxFileSize', async (value: string) => {
Expand Down Expand Up @@ -328,15 +332,18 @@ export const FileUpload = {
const store = FileUpload.getStore('Uploads');
const image = await store._store.getReadStream(file._id, file);

let transformer = sharp().resize({ width, height, fit: 'inside' });
let transformer = (
file.type === 'image/gif' ? sharp({ pages: Math.min(file.identify?.pages ?? 1, MAX_ANIMATED_THUMBNAIL_PAGES) }) : sharp()
Comment thread
julio-rocketchat marked this conversation as resolved.
Resolved
).resize({ width, height, fit: 'inside' });

if (file.type === 'image/svg+xml') {
transformer = transformer.png();
}
const result = transformer.toBuffer({ resolveWithObject: true }).then(({ data, info: { width, height, format } }) => ({
// pageHeight is the per-frame height; info.height is the full stacked height for animated input.
const result = transformer.toBuffer({ resolveWithObject: true }).then(({ data, info: { width, height, pageHeight, format } }) => ({
data,
width,
height,
height: pageHeight ?? height,
thumbFileType: (mime.lookup(format) as string) || '',
thumbFileName: file?.name as string,
originalFileId: file?._id as string,
Expand Down Expand Up @@ -391,6 +398,7 @@ export const FileUpload = {
height,
}
: undefined,
pages: metadata.pages,
};

const shouldRotate = settings.get<boolean>('FileUpload_RotateImages');
Expand Down Expand Up @@ -626,7 +634,9 @@ export const FileUpload = {
initialSize: file.size,
});

return new Promise((resolve, reject) => {
return new Promise<Buffer>((resolve, reject) => {
// Settle on write failure instead of hanging.
Comment thread
julio-rocketchat marked this conversation as resolved.
Resolved
buffer.on('error', reject);
buffer.on('finish', () => {
const contents = buffer.getContents();
if (contents === false) {
Expand Down
1 change: 1 addition & 0 deletions packages/core-typings/src/IUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface IUpload extends IRocketChatRecord {
width: number;
height: number;
};
pages?: number;
Comment thread
julio-rocketchat marked this conversation as resolved.
Resolved
};
store?: string;
path?: string;
Expand Down
Loading