diff --git a/fileloader.js b/fileloader.js index 756f2c0..9a04f51 100644 --- a/fileloader.js +++ b/fileloader.js @@ -37,14 +37,15 @@ module.exports = { api: { async receiveFile ({ _file = wo?._req?.file, useIpfs = true } = {}) { // req 被 multer 处理后,req.file 为 { destination, filename, originialname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径,但不包括起头的 '/',例如 '_filestore/xxx.png' - const ipfsProvider = my.ipfsProvider || wo?.ipfsProvider + const ipfsProvider = my.ipfsProvider || global.wo?.ipfsProvider if (_file?.path) { _file.path = _file.path.replace('\\', '/') - _file.baseUrl = `${wo.envar.servUrl}/${path}` // 传统的基于服务器的 url。在开发环境下,不一定符合前端实际,因为后台只知道预设的 servUrl,而前端会再根据 location.origin 来调整。 + _file.baseUrl = `${global.wo?.envar?.servUrl?.replace?.(/\/$/, '')}/${path}` // 传统的基于服务器的 url。在开发环境下,不一定符合前端实际,因为后台只知道预设的 servUrl,而前端会再根据 location.origin 来调整。 if (useIpfs && wo?.ipfsStore) { // 为了在这里使用 wo.ipfsStore.add, 需要提供 FileContent,不能直接用 req.file // 20230312: not working with nodejs above (not including) 18.2.1! https://github.com/nodejs/node/issues/46221 - const { cid } = await wo.ipfsStore.add(ipfsProvider.globSource(_file.path, ''), { + const { cid } = await global.wo?.ipfsStore?.add?.(ipfsProvider.globSource(_file.path, ''), { + // __dirname 是本文件所在目录,但 path.resolve(_file.path) 得到的是正确路径。 // 20230713 不知为何,今天在本机测试时,ipfs-core 报错 Pattern must be a string,之前没有过这个问题。跟踪后发现 ipfs-core 0.14.3 代码里 第二个参数 patter 必须是 string,于是添加第二个参数为 '',现在可以上传文件给本地的 ipfs-core 了。经测试,加不加这个参数都不影响 ipfs-http-client 运行。 cidVersion: 1, hashAlg: 'sha2-256', @@ -52,7 +53,7 @@ module.exports = { pin: false, // 用户第一次上传的,可能并不是最后想要的,不必须永存。 }) _file.cid = cid?.toString() // + path.extname(file.filename) - _file.ipfsUrl = wo.envar.ipfsGateway + _file.cid // 1) 前端自己选用 cid 或 ipfsUrl。2) 在本地测试成功,但是发现第一次上传的文件,作为 ipfs url 图片在前端显示比较慢,不如作为传统服务器的快。第二次上传同样文件的ipfs前端显示就快了。 + _file.ipfsUrl = `${global.wo?.envar?.ipfsGateway?.replace?.(/\/$/, '')}/${_file.cid}` // 1) 前端自己选用 cid 或 ipfsUrl。2) 在本地测试成功,但是发现第一次上传的文件,作为 ipfs url 图片在前端显示比较慢,不如作为传统服务器的快。第二次上传同样文件的ipfs前端显示就快了。 } return { _state: 'SUCCESS', ..._file } } else {