1

I use NestJS and fastify for my api-server. For files uploading used @fastify/multipart. This is what the function for downloading a file looks like

async uploadFile(path, file): Promise<void> {
    const normalPath = resolve(`${process.cwd()}/uploads/files/${path}/${file.filename}`)
    
    await pump(file.file, createWriteStream(normalPath))
}

In the "file" comes the fastify-type

interface MultipartFile {
      toBuffer: () => Promise<Buffer>
      file: NodeJS.ReadableStream
      filepath: string
      fieldname: string
      filename: string
      encoding: string
      mimetype: string
      fields: import('@fastify/multipart').MultipartFields
    }

Uploading a file via HTTP passes in its entirety, but as soon as the file is written to disk there is only 1024 kb (in some cases less, I do not understand the pattern)

I also tried to pass a ReadableStream to a function like this:

export const writeFileStream = (stream, path) => {
  return new Promise((resolve, reject) =>
    stream
      .on('error', error => {
        if (stream.truncated) unlinkSync(path)
        reject(error)
      })
      .pipe(createWriteStream(path))
      .on('error', error => reject(error))
      .on('finish', () => resolve({ path }))
  )
}

No errors, but the result is the same: 1 megabyte of the entire file. This code used to work, the environment did not change. What can it be? The bug is observed in both local (windows) and container (alpine) environments

Vladimir
  • 53
  • 1
  • 5

2 Answers2

2

By default, the maximum payload the Fastify server is allowed to accept is 1MiB (https://www.fastify.io/docs/latest/Reference/Server/#bodylimit)

You can override it when you create a new Fastify server by setting the bodyLimit property

const app = Fastify({
  ...
  bodyLimit: 10485760, // 10MiB
});
TBA
  • 601
  • 5
  • 6
  • You are partly right, it was necessary to increase the limit in the @fastify/multipart configuration – Vladimir May 24 '23 at 22:05
  • @Vladimir can you add an answer with the code for increasing limit in @fastify/multipart configuration? I can be useful for other users as well – TBA May 29 '23 at 10:48
0

The problem is solved by increasing the limit in the @fastify/multipart configuration

app.register(require('@fastify/multipart'), { limits: { fileSize: 5242880 } })
Vladimir
  • 53
  • 1
  • 5