Giter Site home page Giter Site logo

Comments (6)

Doc999tor avatar Doc999tor commented on April 28, 2024 1

The typings are not precise - it just says: Express.Multer.File but in fact there are two stages with different sets of fields:

  1. At multer.diskStorage.filename stage the file contaons only 4 fields: fieldname, originalname, encoding, mimetype as mentioned here
    At diskStorage.on finish when the file finished to be written to the disk, the file param is updated with additional fields from here with destination, filename, path, size fields here
    So, actually we can't rely on typings in diskStorage.filename hook
  2. For providing a random name you can send an updated filename in cb(null, ``${ file.originalname }-${Math.random()}``)
filename: function (req, file, cb) {
	cb(null, path.join(`${ TMP_DIR }/${ file.originalname }/${hash.digest("hex")}`))
}

So you don't need to stream the file to some additional location - by passing a correct path argument to the cb you'll get the file wherever you need

from multer.

EhsanKey avatar EhsanKey commented on April 28, 2024 1

Sorry for the late reply:

  1. After the upload, I receive the file buffer

In fact, despite the downsides of using MemoryStorage, it'll work - if the file exists, you just discard the buffer; if it doesn't you can write the buffer to the disk to the relevant directory

  1. upload process will be performed twice if the file does not exist

No, in this case the file will be uploaded and processed once - you upload it to MemoryStorage and then write it to the disk or discard

  1. we should delete it from the temporary file directory

In this case you don't need a temp directory - you can save the buffer directly to the needed directory

You're welcome, dear friend. Thank you for the comprehensive and thorough points. I will definitely apply the changes to my code.

from multer.

EhsanKey avatar EhsanKey commented on April 28, 2024

The typings are not precise - it just says: Express.Multer.File but in fact there are two stages with different sets of fields:

  1. At multer.diskStorage.filename stage the file contaons only 4 fields: fieldname, originalname, encoding, mimetype as mentioned here
    At diskStorage.on finish when the file finished to be written to the disk, the file param is updated with additional fields from here with destination, filename, path, size fields here
    So, actually we can't rely on typings in diskStorage.filename hook
  2. For providing a random name you can send an updated filename in cb(null,${ file.originalname }-${Math.random()})
filename: function (req, file, cb) {
	cb(null, path.join(`${ TMP_DIR }/${ file.originalname }/${hash.digest("hex")}`))
}

So you don't need to stream the file to some additional location - by passing a correct path argument to the cb you'll get the file wherever you need

Thank you.
I wanted to retrieve the file buffer before it is uploaded. The approach I took to retrieve the file buffer was actually wrong. To obtain the file buffer before uploading, I needed to set the value of the variable storage to multer.memoryStorage() like this:

const storage = multer.memoryStorage();

const upload = multer({
  storage,
});

Soon, I will make this part of my project open source on GitHub and share the link below this post.

from multer.

Doc999tor avatar Doc999tor commented on April 28, 2024
  1. For my point of view, your previous approach was correct and multer does it already: gets the file and stream it to some directory. Unlike some other languages, in Nodejs it's a bit more low-level, and you need to provide the upload destination, to ensure it's available etc.
    So you can create some temp dir, stream there the file, adjust the name, perform checks and everything you might need to do for your uploading strategy
  2. MemoryStorage is a bit tricky:
    i. You'll need to have enough available memory for file uploading
    ii. It's a bit more fragile since if the process crashes the Buffer contains the file will be lost

from multer.

EhsanKey avatar EhsanKey commented on April 28, 2024
  1. For my point of view, your previous approach was correct and multer does it already: gets the file and stream it to some directory. Unlike some other languages, in Nodejs it's a bit more low-level, and you need to provide the upload destination, to ensure it's available etc.
    So you can create some temp dir, stream there the file, adjust the name, perform checks and everything you might need to do for your uploading strategy
  2. MemoryStorage is a bit tricky:
    i. You'll need to have enough available memory for file uploading
    ii. It's a bit more fragile since if the process crashes the Buffer contains the file will be lost

Thank you for your guidance.

Let me explain a scenario based on your instructions. For example, I send a file to the server and upload it to a temporary directory. After the upload, I receive the file buffer and calculate its hash, then search for the hash among the files in the directories.

If the hash exists, I delete the temporary file; otherwise, if the hash does not exist, I need to move the uploaded file to the upload directory and perform the upload there.

Do you think the file upload process will be performed twice if the file does not exist?

Also, if the file already exists, we should delete it from the temporary file directory.

from multer.

Doc999tor avatar Doc999tor commented on April 28, 2024

Sorry for the late reply:

  1. After the upload, I receive the file buffer

In fact, despite the downsides of using MemoryStorage, it'll work - if the file exists, you just discard the buffer; if it doesn't you can write the buffer to the disk to the relevant directory

  1. upload process will be performed twice if the file does not exist

No, in this case the file will be uploaded and processed once - you upload it to MemoryStorage and then write it to the disk or discard

  1. we should delete it from the temporary file directory

In this case you don't need a temp directory - you can save the buffer directly to the needed directory

from multer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.