Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limits.fileSize not working properly with different files #542

Open
2 tasks done
yaneony opened this issue Aug 27, 2024 · 1 comment
Open
2 tasks done

limits.fileSize not working properly with different files #542

yaneony opened this issue Aug 27, 2024 · 1 comment

Comments

@yaneony
Copy link

yaneony commented Aug 27, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.x.x

Plugin version

8.3.0

Node.js version

LTS

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Debian 12

Description

I have very different behavion on different files. Currently only tested with 2 image files, one is JPG ~2.3 Mb, another one is upscaled PNG ~22 Mb. Using following code:

const fastify = require('fastify')();

fastify.register(require('fastify-multipart'));

fastify.post('/upload', async function (req, res) {
  let file = await req.file({ limits: { fileSize: 1024 * 1024 }, throwFileSizeLimit: true });
  if (file.file.truncated) {
    console.log('File too large');
    res.send('File too large');
    return res;
  }
  res.send('OK');
});

fastify.listen(3000, (err) => {
  if (err) throw err;
  console.log(`server listening on ${fastify.server.address().port}`);
});

Problem is, while uploading one file, file limit is working fine and when uploading another one, it isn't working at all. If i read it right, limits are in bytes, so, using (1024*1024) should be limited to 1 Mb. Here are two responses I get:

<ref *1> {
  type: 'file',
  fieldname: 'files[]',
  filename: 'def_upscayl_4x_ultrasharp.png',
  encoding: '7bit',
  mimetype: 'image/png',
  file: FileStream {
    _events: {
      close: undefined,
      error: undefined,
      data: undefined,
      end: [Function (anonymous)],
      readable: undefined,
      limit: [Function (anonymous)]
    },
    _readableState: ReadableState {
      highWaterMark: 16384,
      buffer: [Array],
      bufferIndex: 0,
      length: 64840,
      pipes: [],
      awaitDrainWriters: null,
      [Symbol(kState)]: 1052940
    },
    _maxListeners: undefined,
    bytesRead: 64840,
    truncated: false,
    _eventsCount: 2,
    _read: [Function (anonymous)],
    [Symbol(shapeMode)]: true,
    [Symbol(kCapture)]: false
  },
  fields: { 'files[]': [Circular *1] },
  _buf: null,
  toBuffer: [AsyncFunction: toBuffer]
}

<ref *1> {
  type: 'file',
  fieldname: 'files[]',
  filename: 'SSL27917.JPG',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  file: FileStream {
    _events: {
      close: undefined,
      error: undefined,
      data: undefined,
      end: [Function (anonymous)],
      readable: undefined,
      limit: [Function (anonymous)]
    },
    _readableState: ReadableState {
      highWaterMark: 16384,
      buffer: [Array],
      bufferIndex: 0,
      length: 64851,
      pipes: [],
      awaitDrainWriters: null,
      [Symbol(kState)]: 1052940
    },
    _maxListeners: undefined,
    bytesRead: 64851,
    truncated: false,
    _eventsCount: 2,
    _read: [Function (anonymous)],
    [Symbol(shapeMode)]: true,
    [Symbol(kCapture)]: false
  },
  fields: { 'files[]': [Circular *1] },
  _buf: null,
  toBuffer: [AsyncFunction: toBuffer]
}

But, when you change the limit to only 1024, which would be 1Kb everything is working fine. So, what could be the problem?!
Here are files I'm uploading, so you can see the're have matching file size:
Y7NQ3JMB

Link to code that reproduces the bug

No response

Expected Behavior

No response

@amir1387aht

This comment was marked as abuse.

@mcollina
Copy link
Member

I'm a bit lost. You are reading only one file in your example. Can you add a complete example with scripts to reproduce?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants