fs.createWriteStream cut file to 1mib

139 Views Asked by At

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

2

There are 2 best solutions below

0
Vladimir On BEST ANSWER

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

app.register(require('@fastify/multipart'), { limits: { fileSize: 5242880 } })
2
TBA On

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
});