From 1ae87f960f9d33adb8017fddbf8e85a94e63ea5a Mon Sep 17 00:00:00 2001 From: luk Date: Thu, 28 Nov 2024 21:33:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=92=8C=20unitool.js=20=E5=92=8C=20dragFileUp?= =?UTF-8?q?loader.vue=20=E9=85=8D=E5=90=88=EF=BC=8C=E4=BC=A0=E9=80=92=20fi?= =?UTF-8?q?lenameOriginal=20=E8=A7=A3=E5=86=B3=E4=BA=86=E6=8B=96=E6=8B=BD?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=9A=84=E6=96=87=E4=BB=B6=E7=9A=84=20origin?= =?UTF-8?q?alname=20=E5=B9=B6=E9=9D=9E=E7=9C=9F=E5=AE=9E=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fileloader.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fileloader.js b/fileloader.js index d22a7bc..3a00320 100644 --- a/fileloader.js +++ b/fileloader.js @@ -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