注意到刚收到 file 时的 originalname 形如 file-1732762348744,不知什么时机会变成 originalnameSystem,而 originalname 变回正确的前端的文件名。为此改正命名的时机。并且使用 mime 库来正确处理 text/plain 等 mimetype,否则会被存为 *.plain
This commit is contained in:
parent
0c789837eb
commit
58d90c17e8
@ -2,6 +2,7 @@ const multer = require('multer')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const crypto = require('crypto')
|
||||
const mime = require('mime') // mime@4 do not support require, only support import. Therefore use mime@2.
|
||||
const wo = global.wo
|
||||
|
||||
const my = {}
|
||||
@ -28,7 +29,8 @@ module.exports = {
|
||||
// 虽然可以把本方法写成 async 的,并且在这里调用 wo.ipfsStore.ipfs.add(file.stream) 来获取 cid,但是最后在本地存成的文件是 0字节的。因此还是用个随机数吧。
|
||||
// 或者,干脆利用这个缺陷,直接提交到 ipfs,在本地就留着0字节文件不要使用就好了。同时,在 api.receiveFile 里,就要相应的直接返回 IPFS 网址给前端。
|
||||
const filename =
|
||||
`${Date.now()}-${crypto.randomBytes(16).toString('hex')}` + (path.extname(file.originalname || '').toLowerCase() || `.${file?.mimetype?.split?.('/')?.[1]}`)
|
||||
`${Date.now()}-${crypto.randomBytes(16).toString('hex')}` +
|
||||
(path.extname(file.originalname || '').toLowerCase() || `.${mime.getExtension(file?.mimetype)}`) // 注意到这时的 originalname 形如 file-1732762348744,不知什么时机会变成 originalnameSystem,而 originalname 变回正确的前端的文件名
|
||||
//const _passtokenSource = webtoken.verifyToken(req.headers._passtoken) || {}
|
||||
//const filename = `${req.path.replace(/^\/api\d*/, '')}_${_passtokenSource.usid}_${Date.now()}${fileNameExtension}` // 如果最终 filename 含有 / (例如当 req.path 为 Who/todo),则必须已经存在该目录,否则在这里就出错,不会进入下面流程。
|
||||
cb(null, filename)
|
||||
@ -64,12 +66,11 @@ module.exports = {
|
||||
_file.cid = cid?.toString() // + path.extname(file.filename)
|
||||
_file.ipfsUrl = `${global.wo?.envar?.ipfsLens?.replace?.(/\/$/, '')}/${_file.cid}` // 1) 前端自己选用 cid 或 ipfsUrl。2) 在本地测试成功,但是发现第一次上传的文件,作为 ipfs url 图片在前端显示比较慢,不如作为传统服务器的快。第二次上传同样文件的ipfs前端显示就快了。
|
||||
// rename the file to the cid
|
||||
const newFileName = `${new Date().toJSON().replace(/[-:T]|\.\d\d\dZ$/g, '')}-${_file.cid}${path.extname(_file.filename)}`
|
||||
const newFileName =
|
||||
`${new Date().toJSON().replace(/[-:T]|\.\d\d\dZ$/g, '')}-${_file.cid}` +
|
||||
(path.extname(_file.originalname || '').toLowerCase() || `.${mime.getExtension(_file?.mimetype)}`)
|
||||
try {
|
||||
await fs.renameSync(
|
||||
path.resolve(_file.path),
|
||||
path.resolve(_file.destination, newFileName)
|
||||
)
|
||||
await fs.renameSync(path.resolve(_file.path), path.resolve(_file.destination, newFileName))
|
||||
// set all properties of _file containing the original file name to the new file name
|
||||
_file.path = _file.path?.replace?.(_file.filename, newFileName)
|
||||
_file.baseUrl = _file.baseUrl?.replace?.(_file.filename, newFileName)
|
||||
@ -80,5 +81,5 @@ module.exports = {
|
||||
}
|
||||
return { _state: 'SUCCESS', ..._file }
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"mime": "^2.6.0",
|
||||
"multer": "^1.4.3"
|
||||
},
|
||||
"devDependencies": {},
|
||||
|
Loading…
Reference in New Issue
Block a user