mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-service.git
synced 2026-09-21 16:52:03 -07:00
Prototyped /ipfs/download
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
REST API Controller library for the /ipfs route
|
||||
*/
|
||||
|
||||
// Global npm libraries
|
||||
import { exporter } from 'ipfs-unixfs-exporter'
|
||||
import fs from 'fs'
|
||||
|
||||
// Local libraries
|
||||
import wlogger from '../../../adapters/wlogger.js'
|
||||
|
||||
class IpfsRESTControllerLib {
|
||||
@@ -23,6 +28,7 @@ class IpfsRESTControllerLib {
|
||||
// Encapsulate dependencies
|
||||
// this.UserModel = this.adapters.localdb.Users
|
||||
// this.userUseCases = this.useCases.user
|
||||
this.fs = fs
|
||||
|
||||
// Bind 'this' object to all subfunctions
|
||||
this.getStatus = this.getStatus.bind(this)
|
||||
@@ -125,10 +131,69 @@ class IpfsRESTControllerLib {
|
||||
|
||||
async downloadFile (ctx) {
|
||||
try {
|
||||
const { cid } = ctx.params
|
||||
// const { cid } = ctx.params
|
||||
|
||||
const file = await this.adapters.ipfs.ipfs.blockstore.get(cid)
|
||||
return file
|
||||
// const multiaddr = ctx.request.body.multiaddr
|
||||
// const getDetails = ctx.request.body.getDetails
|
||||
const { cid, fileName, path } = ctx.request.body
|
||||
|
||||
console.log(`downloadFile() retrieving this CID: ${cid}, with fileName: ${fileName}, and path: ${path}`)
|
||||
|
||||
// const file = await this.adapters.ipfs.ipfs.blockstore.get(cid)
|
||||
// console.log('file: ', file)
|
||||
// return file
|
||||
|
||||
// const ipfsFs = this.adapters.ipfs.ipfs.fs
|
||||
// console.log('ipfsFs: ', ipfsFs)
|
||||
//
|
||||
// const buf = []
|
||||
// for await (const chunk of ipfsFs.cat(cid)) {
|
||||
// // text += decoder.decode(chunk, {
|
||||
// // stream: true
|
||||
// // })
|
||||
//
|
||||
// buf.push(chunk)
|
||||
// }
|
||||
// console.log(`buf: `, buf)
|
||||
|
||||
const blockstore = this.adapters.ipfs.ipfs.blockstore
|
||||
const entry = await exporter(cid, blockstore)
|
||||
|
||||
console.info(entry.cid) // Qmqux
|
||||
console.info(entry.path) // Qmbaz/foo/bar.txt
|
||||
console.info(entry.name) // bar.txt
|
||||
console.log('entry: ', entry)
|
||||
// console.info(entry.unixfs.fileSize()) // 4
|
||||
|
||||
// stream content from unixfs node
|
||||
// const bytes = new Uint8Array(Number(entry.size))
|
||||
// let offset = 0
|
||||
|
||||
const filePath = `${path}/${fileName}`
|
||||
const writableStream = fs.createWriteStream(filePath)
|
||||
|
||||
writableStream.on('error', (error) => {
|
||||
console.log(`An error occured while writing to the file. Error: ${error.message}`)
|
||||
})
|
||||
|
||||
writableStream.on('finish', () => {
|
||||
console.log(`CID ${cid} downloaded to ${filePath}`)
|
||||
})
|
||||
|
||||
for await (const buf of entry.content()) {
|
||||
// bytes.set(buf, offset)
|
||||
// offset += buf.length
|
||||
|
||||
writableStream.write(buf)
|
||||
}
|
||||
|
||||
writableStream.end()
|
||||
|
||||
// console.info(bytes) // 0, 1, 2, 3
|
||||
|
||||
ctx.body = {
|
||||
success: true
|
||||
}
|
||||
} catch (err) {
|
||||
wlogger.error('Error in ipfs/controller.js/downloadFile(): ', err)
|
||||
this.handleError(ctx, err)
|
||||
|
||||
Reference in New Issue
Block a user