From 58d90c17e8ffa55ae551eddb76f921eda0723ae7 Mon Sep 17 00:00:00 2001 From: luk Date: Thu, 28 Nov 2024 14:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E6=84=8F=E5=88=B0=E5=88=9A=E6=94=B6?= =?UTF-8?q?=E5=88=B0=20file=20=E6=97=B6=E7=9A=84=20originalname=20?= =?UTF-8?q?=E5=BD=A2=E5=A6=82=20file-1732762348744=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E7=9F=A5=E4=BB=80=E4=B9=88=E6=97=B6=E6=9C=BA=E4=BC=9A=E5=8F=98?= =?UTF-8?q?=E6=88=90=20originalnameSystem=EF=BC=8C=E8=80=8C=20originalname?= =?UTF-8?q?=20=E5=8F=98=E5=9B=9E=E6=AD=A3=E7=A1=AE=E7=9A=84=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D=E3=80=82=E4=B8=BA?= =?UTF-8?q?=E6=AD=A4=E6=94=B9=E6=AD=A3=E5=91=BD=E5=90=8D=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E6=9C=BA=E3=80=82=E5=B9=B6=E4=B8=94=E4=BD=BF=E7=94=A8=20mime?= =?UTF-8?q?=20=E5=BA=93=E6=9D=A5=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86=20tex?= =?UTF-8?q?t/plain=20=E7=AD=89=20mimetype=EF=BC=8C=E5=90=A6=E5=88=99?= =?UTF-8?q?=E4=BC=9A=E8=A2=AB=E5=AD=98=E4=B8=BA=20*.plain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fileloader.js | 15 ++++++++------- package.json | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/fileloader.js b/fileloader.js index ae67eff..d22a7bc 100644 --- a/fileloader.js +++ b/fileloader.js @@ -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 } }, - } + }, } diff --git a/package.json b/package.json index e04036a..57e17d3 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "0.1.0", "private": true, "dependencies": { + "mime": "^2.6.0", "multer": "^1.4.3" }, "devDependencies": {},