rename ipfsLib to ipfsProvider; add 'initMy' method
This commit is contained in:
		
							parent
							
								
									1d1326f6b0
								
							
						
					
					
						commit
						6879c34a13
					
				| @ -3,7 +3,13 @@ const path = require('path') | |||||||
| const crypto = require('crypto') | const crypto = require('crypto') | ||||||
| const wo = global.wo | const wo = global.wo | ||||||
| 
 | 
 | ||||||
|  | const my = {} | ||||||
|  | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|  |   initMy ({ ipfsProvider }) { | ||||||
|  |     my.ipfsProvider = ipfsProvider | ||||||
|  |   }, | ||||||
|  | 
 | ||||||
|   MulterStore: multer({ |   MulterStore: multer({ | ||||||
|     // dest:'./File/', // 这样,不能自定义文件名。
 |     // dest:'./File/', // 这样,不能自定义文件名。
 | ||||||
|     storage: multer.diskStorage({ |     storage: multer.diskStorage({ | ||||||
| @ -31,12 +37,13 @@ module.exports = { | |||||||
|     async receiveFile () { |     async receiveFile () { | ||||||
|       // 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 file = wo._req?.file | ||||||
|  |       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(wo.ipfsLib.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, // 多个备份是好的,而且能加快下次添加同样文件的速度。
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user