This commit is contained in:
Luk 2024-09-15 11:19:26 +08:00
parent 51e2ff7f2f
commit d6740c4c20

View File

@ -43,41 +43,42 @@ module.exports = {
// req 被 multer 处理后req.file 为 { destination, filename, originialname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径,但不包括起头的 '/',例如 '_filestore/xxx.png' // req 被 multer 处理后req.file 为 { destination, filename, originialname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径,但不包括起头的 '/',例如 '_filestore/xxx.png'
const ipfsProvider = my.ipfsProvider || global.wo?.ipfsProvider const ipfsProvider = my.ipfsProvider || global.wo?.ipfsProvider
const ipfsStore = my.ipfsStore || global.wo?.ipfsStore const ipfsStore = my.ipfsStore || global.wo?.ipfsStore
if (_file?.path) {
_file.path = _file.path.replace('\\', '/') if (!_file?.path) {
_file.baseUrl = `${global.wo?.envar?.servUrl?.replace?.(/\/$/, '')}/${_file.path}` // 传统的基于服务器的 url。在开发环境下不一定符合前端实际因为后台只知道预设的 servUrl而前端会再根据 location.origin 来调整。 return { _state: 'WOBASE_FAIL_FILE_NOT_RECEIVED' }
if (useIpfs && 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 _file.path = _file.path.replace('\\', '/')
const { cid } = await ipfsStore?.add?.(ipfsProvider.globSource(_file.path, ''), { _file.baseUrl = `${global.wo?.envar?.servUrl?.replace?.(/\/$/, '')}/${_file.path}` // 传统的基于服务器的 url。在开发环境下不一定符合前端实际因为后台只知道预设的 servUrl而前端会再根据 location.origin 来调整。
// __dirname 是本文件所在目录,但 path.resolve(_file.path) 得到的是正确路径。 if (useIpfs && ipfsStore && ipfsProvider) {
// 20230713 不知为何今天在本机测试时ipfs-core 报错 Pattern must be a string之前没有过这个问题。跟踪后发现 ipfs-core 0.14.3 代码里 第二个参数 patter 必须是 string于是添加第二个参数为 '',现在可以上传文件给本地的 ipfs-core 了。经测试,加不加这个参数都不影响 ipfs-http-client 运行。 // 为了在这里使用 wo.ipfsStore.add, 需要提供 FileContent不能直接用 req.file
cidVersion: 1, // 20230312: not working with nodejs above (not including) 18.2.1! https://github.com/nodejs/node/issues/46221
hashAlg: 'sha2-256', const { cid } = await ipfsStore?.add?.(ipfsProvider.globSource(_file.path, ''), {
onlyHash: false, // 多个备份是好的,而且能加快下次添加同样文件的速度 // __dirname 是本文件所在目录,但 path.resolve(_file.path) 得到的是正确路径
pin: false, // 用户第一次上传的,可能并不是最后想要的,不必须永存 // 20230713 不知为何今天在本机测试时ipfs-core 报错 Pattern must be a string之前没有过这个问题。跟踪后发现 ipfs-core 0.14.3 代码里 第二个参数 patter 必须是 string于是添加第二个参数为 '',现在可以上传文件给本地的 ipfs-core 了。经测试,加不加这个参数都不影响 ipfs-http-client 运行
}) cidVersion: 1,
_file.cid = cid?.toString() // + path.extname(file.filename) hashAlg: 'sha2-256',
_file.ipfsUrl = `${global.wo?.envar?.ipfsLens?.replace?.(/\/$/, '')}/${_file.cid}` // 1) 前端自己选用 cid 或 ipfsUrl。2) 在本地测试成功,但是发现第一次上传的文件,作为 ipfs url 图片在前端显示比较慢不如作为传统服务器的快。第二次上传同样文件的ipfs前端显示就快了 onlyHash: false, // 多个备份是好的,而且能加快下次添加同样文件的速度
// rename the file to the cid pin: false, // 用户第一次上传的,可能并不是最后想要的,不必须永存。
const newFileName = `${new Date().toJSON().replace(/[-:T]|\.\d\d\dZ$/g, '')}-${_file.cid}${path.extname(_file.filename)}` })
try { _file.cid = cid?.toString() // + path.extname(file.filename)
await fs.renameSync( _file.ipfsUrl = `${global.wo?.envar?.ipfsLens?.replace?.(/\/$/, '')}/${_file.cid}` // 1) 前端自己选用 cid 或 ipfsUrl。2) 在本地测试成功,但是发现第一次上传的文件,作为 ipfs url 图片在前端显示比较慢不如作为传统服务器的快。第二次上传同样文件的ipfs前端显示就快了。
path.resolve(_file.path), // rename the file to the cid
path.resolve(_file.destination, newFileName) const newFileName = `${new Date().toJSON().replace(/[-:T]|\.\d\d\dZ$/g, '')}-${_file.cid}${path.extname(_file.filename)}`
) try {
// set all properties of _file containing the original file name to the new file name await fs.renameSync(
_file.path = _file.path?.replace?.(_file.filename, newFileName) path.resolve(_file.path),
_file.baseUrl = _file.baseUrl?.replace?.(_file.filename, newFileName) path.resolve(_file.destination, newFileName)
_file.filename = newFileName )
} catch (e) { // set all properties of _file containing the original file name to the new file name
console.log('FileLoader: rename failed: ', path.resolve(_file.filename), e) _file.path = _file.path?.replace?.(_file.filename, newFileName)
} _file.baseUrl = _file.baseUrl?.replace?.(_file.filename, newFileName)
return { _state: 'SUCCESS', ..._file } _file.filename = newFileName
} else { } catch (e) {
return { _state: 'WOBASE_FAIL_FILE_NOT_RECEIVED' } console.log('FileLoader: rename failed: ', path.resolve(_file.filename), e)
} }
} }
return { _state: 'SUCCESS', ..._file }
}, },
} }
} }