don't use wo._req.file, use _file passed from server.js
This commit is contained in:
		
							parent
							
								
									7cd99fe9cb
								
							
						
					
					
						commit
						90c71f66cf
					
				@ -34,26 +34,25 @@ module.exports = {
 | 
				
			|||||||
  }).single('file'),
 | 
					  }).single('file'),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  api: {
 | 
					  api: {
 | 
				
			||||||
    async receiveFile () {
 | 
					    async receiveFile ({ _file = wo?._req?.file } = {}) {
 | 
				
			||||||
      // req 被 multer 处理后,req.file 为 { destination, filename, originialname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径。
 | 
					      // req 被 multer 处理后,req.file 为 { destination, filename, originialname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径。
 | 
				
			||||||
      const file = wo._req?.file
 | 
					 | 
				
			||||||
      const ipfsProvider = my.ipfsProvider || wo?.ipfsProvider
 | 
					      const ipfsProvider = my.ipfsProvider || wo?.ipfsProvider
 | 
				
			||||||
      if (file?.path) {
 | 
					      if (_file?.path) {
 | 
				
			||||||
        file.path = file.path.replace('\\', '/')
 | 
					        _file.path = _file.path.replace('\\', '/')
 | 
				
			||||||
        if (wo?.ipfsStore) {
 | 
					        if (wo?.ipfsStore) {
 | 
				
			||||||
          // 为了在这里使用 wo.ipfsStore.add, 需要提供 FileContent,不能直接用 req.file
 | 
					          // 为了在这里使用 wo.ipfsStore.add, 需要提供 FileContent,不能直接用 req.file
 | 
				
			||||||
          // 20230312: not working with nodejs above (not including) 18.2.1! https://github.com/nodejs/node/issues/46221
 | 
					          // 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 wo.ipfsStore.add(ipfsProvider.globSource(_file.path), {
 | 
				
			||||||
            cidVersion: 1,
 | 
					            cidVersion: 1,
 | 
				
			||||||
            hashAlg: 'sha2-256',
 | 
					            hashAlg: 'sha2-256',
 | 
				
			||||||
            onlyHash: false, // 多个备份是好的,而且能加快下次添加同样文件的速度。
 | 
					            onlyHash: false, // 多个备份是好的,而且能加快下次添加同样文件的速度。
 | 
				
			||||||
            pin: false, // 用户第一次上传的,可能并不是最后想要的,不必须永存。
 | 
					            pin: false, // 用户第一次上传的,可能并不是最后想要的,不必须永存。
 | 
				
			||||||
          })
 | 
					          })
 | 
				
			||||||
          file.cid = cid?.toString() // + path.extname(file.filename)
 | 
					          _file.cid = cid?.toString() // + path.extname(file.filename)
 | 
				
			||||||
          file.ipfsUrl = wo.envar.ipfsGateway + file.cid
 | 
					          _file.ipfsUrl = wo.envar.ipfsGateway + _file.cid
 | 
				
			||||||
          // file.path = wo.envar.ipfsGateway + file.cid // 可选考虑直接返回 ipfs地址。在本地测试成功,但是发现第一次上传的文件,作为ipfs网关图片在前端显示比较慢,不如显示http的快。第二次上传同样文件的ipfs前端显示就快了。
 | 
					          // file.path = wo.envar.ipfsGateway + file.cid // 可选考虑直接返回 ipfs地址。在本地测试成功,但是发现第一次上传的文件,作为ipfs网关图片在前端显示比较慢,不如显示http的快。第二次上传同样文件的ipfs前端显示就快了。
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return { _state: 'SUCCESS', ...file }
 | 
					        return { _state: 'SUCCESS', ..._file }
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        return { _state: 'WOBASE_FAIL_FILE_NOT_RECEIVED' }
 | 
					        return { _state: 'WOBASE_FAIL_FILE_NOT_RECEIVED' }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user