Multer writes file with zero size if originalname changes in the storage.filename prop.
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- javascript, node.js
- Domain
- backend
Research direction
Start with the reported storage.filename callback and generateUserFileName function, then reproduce the zero-byte result when fs.promises.access finds an existing filename. Trace the interaction between the async filename generation, destination callback, and Multer's file-writing flow. Done means identifying the cause and confirming that generated or unchanged names produce the expected non-empty upload.
Written by the indexing model from the issue text.
Description
Hello, dear! Odd multer behaviour is occured. If such filename are existed in directory then i set a new filename via generateUserFileName function which used as a tool for filename property of my multer storage. What i totaly doing wrong when i use async fs to check if the file exist? Because i don't have problems with multer that used to change originalname -> some newName in the another project without fs tool checkings
const fs = require('node:fs')
const multer = require('multer')
const path = require('node:path')
const FILE_UPLOAD_PATH = '/code/uploads'
const UPDATE_FILE_URL = /\/file\/update\/[\d]+/
async function generateUserFileName(originalname, request){
const userId = request.userId
const fileId = request.params?.id || null
const url = request.url
const file = await dao.getFile(fileId, userId)
const fileDir = path.resolve(FILE_UPLOAD_PATH, userId.toString())
const filePath = path.resolve(fileDir, originalname)
if (file){
const oldFileName = path.resolve(fileDir, file.name)
if (url.match(UPDATE_FILE_URL)){
try{
await fs.promises.access(oldFileName)
await fs.promises.unlink(oldFileName)
}
catch (error){
//pass
}
}
}
try{
await fs.promises.access(filePath) //if filename is exist then it generates a new name:
return path.parse(originalname).name + `_${Date.now()}` + path.extname(originalname)
}
catch (error){
return originalname //if file is not exist then filename is the same as it is in request.originalname
}
}
const storage = multer.diskStorage({
destination: async (request, file, callback) => {
console.log(`Userid: ${request.userId}`)
const fileDir = path.resolve(FILE_UPLOAD_PATH, request.userId.toString())
try{
await fs.promises.access(fileDir)
}
catch (error){
await fs.promises.mkdir(path.resolve(fileDir))
}
callback(null, fileDir)
},
filename: async (request, file, callback) => {
const fn = await generateUserFileName(file.originalname, request)
console.log(`File name ${fn} was generated successfully`)// Always, here my filename is generated successfully
callback(null, fn)
}
})
const upload = multer({
storage: storage
})
const uploadStorage = upload.single('file')
module.exports = {
uploadStorage,
deleteFile
}
If my filename is differ from originalname in the cases when such file is exist, then multer wrire empty file with zero size.
- Dominant language
- JavaScript
- Stars
- 12.1k
- Forks
- 1.2k
- Avg merge
- 16h 53m
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from expressjs/multer
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
expressjs/multer#1494 · 3 comments ·
Maintainers usually reply within 1 day
-
next() is called twice when the request closes after busboy fails to startPossibly taken @NgoQuocViet2001 claimed this 27 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
expressjs/multer#1403 · 1 comment ·
Maintainers usually reply within 1 day
-
DiskStorage does not remove uploaded files after fsync failurePossibly taken @Kase390 claimed this 16 days ago. Openbug
Difficulty 3/5 1-2 days Newbie friendliness 35/100
Maintainers usually reply within 1 day
-
v3: enforce a default for fieldArrayIndexLimitMay be free again @UlisesGascon claimed this 83 days ago, and no pull request is open. Opendiscuss
expressjs/multer#1461 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
All issues in expressjs/multer
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
anothaDev/luci-theme-vantage#143 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
kingcharleslzy-ai/dsh-purge#282 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
-
a-corriger bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
christian-raj/S-Aloha#340 ·
-
area:image documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day