rename ipfsLib to ipfsProvider; add 'initMy' method

This commit is contained in:
陆柯 2023-03-20 13:14:46 +08:00
parent 1d1326f6b0
commit 6879c34a13

View File

@ -3,7 +3,13 @@ const path = require('path')
const crypto = require('crypto')
const wo = global.wo
const my = {}
module.exports = {
initMy ({ ipfsProvider }) {
my.ipfsProvider = ipfsProvider
},
MulterStore: multer({
// dest:'./File/', // 这样,不能自定义文件名。
storage: multer.diskStorage({
@ -31,12 +37,13 @@ module.exports = {
async receiveFile () {
// req 被 multer 处理后req.file 为 { destination, filename, originialname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径。
const file = wo._req?.file
const ipfsProvider = my.ipfsProvider || wo?.ipfsProvider
if (file?.path) {
file.path = file.path.replace('\\', '/')
if (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(wo.ipfsLib.globSource(file.path), {
const { cid } = await wo.ipfsStore.add(ipfsProvider.globSource(file.path), {
cidVersion: 1,
hashAlg: 'sha2-256',
onlyHash: false, // 多个备份是好的,而且能加快下次添加同样文件的速度。