Skip to content

Commit

Permalink
Use 'image' size limit in bookmarklet controller
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Jan 24, 2025
1 parent bfd76e1 commit 686576e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions app/support/download-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { pipeline, finished } from 'stream/promises';

import meter from 'stream-meter';
import mediaType from 'media-type';
import { parse as bytesParse } from 'bytes';
import config from 'config';

const fileSizeLimit = bytesParse(config.attachments.fileSizeLimit);
const sizeLimits = config.attachments.fileSizeLimitByType;

export async function downloadURL(url: string) {
export async function downloadURL(
url: string,
sizeLimit = sizeLimits['image'] ?? sizeLimits['default'],
) {
const parsedURL = new URL(url);

if (parsedURL.protocol !== 'http:' && parsedURL.protocol !== 'https:') {
Expand Down Expand Up @@ -43,8 +45,8 @@ export async function downloadURL(url: string) {
if (response.headers.has('content-length')) {
const contentLength = parseInt(response.headers.get('content-length')!);

if (!isNaN(contentLength) && contentLength > fileSizeLimit) {
throw new Error(`File is too large (${contentLength} bytes, max. ${fileSizeLimit})`);
if (!isNaN(contentLength) && contentLength > sizeLimit) {
throw new Error(`File is too large (${contentLength} bytes, max. ${sizeLimit})`);
}
}

Expand All @@ -54,7 +56,7 @@ export async function downloadURL(url: string) {
// @ts-expect-error
const inStream = response.body as NodeJS.ReadableStream;
const outStream = createWriteStream(filePath, { flags: 'w' });
await pipeline(inStream, meter(fileSizeLimit), outStream);
await pipeline(inStream, meter(sizeLimit), outStream);
await finished(outStream); // wait for the file to be written and closed

const stats = await fs.stat(filePath);
Expand Down
8 changes: 4 additions & 4 deletions test/functional/bookmarklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Readable } from 'stream';

import { fromPairs } from 'lodash';
import expect from 'unexpected';
import { parse as bytesParse } from 'bytes';
import config from 'config';

import cleanDB from '../dbCleaner';
Expand All @@ -21,7 +20,8 @@ import {
import { postResponse } from './schemaV2-helper';
import Session from './realtime-session';

const fileSizeLimit = bytesParse(config.attachments.fileSizeLimit);
const sizeLimits = config.attachments.fileSizeLimitByType;
const imageSizeLimit = sizeLimits['image'] ?? sizeLimits['default'];

describe('BookmarkletController', () => {
let rtPort;
Expand Down Expand Up @@ -122,11 +122,11 @@ describe('BookmarkletController', () => {
ctx.status = 200;
ctx.response.type = 'image/jpeg';
ctx.body = 'sss';
ctx.response.length = fileSizeLimit * 2;
ctx.response.length = imageSizeLimit * 2;
} else if (url === '/big-body.jpg') {
ctx.status = 200;
ctx.response.type = 'image/jpeg';
ctx.body = getStreamOfLength(fileSizeLimit * 2);
ctx.body = getStreamOfLength(imageSizeLimit * 2);
} else if (url === '/im%C3%A5ge.png') {
ctx.status = 200;
ctx.response.type = 'image/png';
Expand Down

0 comments on commit 686576e

Please sign in to comment.