和 unitool.js 和 dragFileUploader.vue 配合,传递 filenameOriginal 解决了拖拽上传的文件的 originalname 并非真实文件名的问题。
This commit is contained in:
parent
58d90c17e8
commit
1ae87f960f
@ -30,7 +30,7 @@ module.exports = {
|
||||
// 或者,干脆利用这个缺陷,直接提交到 ipfs,在本地就留着0字节文件不要使用就好了。同时,在 api.receiveFile 里,就要相应的直接返回 IPFS 网址给前端。
|
||||
const filename =
|
||||
`${Date.now()}-${crypto.randomBytes(16).toString('hex')}` +
|
||||
(path.extname(file.originalname || '').toLowerCase() || `.${mime.getExtension(file?.mimetype)}`) // 注意到这时的 originalname 形如 file-1732762348744,不知什么时机会变成 originalnameSystem,而 originalname 变回正确的前端的文件名
|
||||
(path.extname(file.originalname || '').toLowerCase() || `.${mime.getExtension(file?.mimetype) || ''}`) // 注意,如果是拖拽上传的文件,这里收到的 originalname 形如 file-1732762348744,不是真实的前端文件名,也不包含后缀名!因此用 mime 来默认一个后缀名。
|
||||
//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)
|
||||
@ -41,7 +41,7 @@ module.exports = {
|
||||
}).single('file'),
|
||||
|
||||
api: {
|
||||
async receiveFile ({ _file = wo?._req?.file, useIpfs = true } = {}) {
|
||||
async receiveFile ({ _file = wo?._req?.file, useIpfs = true, filenameOriginal, _passtokenSource } = {}) {
|
||||
// req 被 multer 处理后,req.file 为 { destination, filename, originialname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径,但不包括起头的 '/',例如 '_filestore/xxx.png'
|
||||
const ipfsProvider = my.ipfsProvider || global.wo?.ipfsProvider
|
||||
const ipfsStore = my.ipfsStore || global.wo?.ipfsStore
|
||||
@ -66,9 +66,10 @@ 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
|
||||
if (filenameOriginal) _file.originalname = filenameOriginal
|
||||
const newFileName =
|
||||
`${new Date().toJSON().replace(/[-:T]|\.\d\d\dZ$/g, '')}-${_file.cid}` +
|
||||
(path.extname(_file.originalname || '').toLowerCase() || `.${mime.getExtension(_file?.mimetype)}`)
|
||||
(path.extname(_file.originalname || '').toLowerCase() || `.${mime.getExtension(_file?.mimetype) || ''}`)
|
||||
try {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user