From c6c8b6418c61163dbb6f9d30610f0a7b71bb6cfb Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 4 Feb 2024 20:35:22 -0800 Subject: [PATCH] Prototyping download REST API --- src/controllers/rest-api/ipfs/controller.js | 13 +++++++++++++ src/controllers/rest-api/ipfs/index.js | 1 + 2 files changed, 14 insertions(+) diff --git a/src/controllers/rest-api/ipfs/controller.js b/src/controllers/rest-api/ipfs/controller.js index 2294a02..f39b9a9 100644 --- a/src/controllers/rest-api/ipfs/controller.js +++ b/src/controllers/rest-api/ipfs/controller.js @@ -31,6 +31,7 @@ class IpfsRESTControllerLib { this.handleError = this.handleError.bind(this) this.connect = this.connect.bind(this) this.getThisNode = this.getThisNode.bind(this) + this.downloadFile = this.downloadFile.bind(this) } /** @@ -123,6 +124,18 @@ class IpfsRESTControllerLib { } } + async downloadFile(ctx) { + try { + const {cid} = ctx.params + + const file = await this.adapters.ipfs.ipfs.blockstore.get(cid) + return file + } catch(err) { + wlogger.error('Error in ipfs/controller.js/downloadFile(): ', err) + this.handleError(ctx, err) + } + } + // DRY error handler handleError (ctx, err) { // If an HTTP status is specified by the buisiness logic, use that. diff --git a/src/controllers/rest-api/ipfs/index.js b/src/controllers/rest-api/ipfs/index.js index d787f73..f0ef47d 100644 --- a/src/controllers/rest-api/ipfs/index.js +++ b/src/controllers/rest-api/ipfs/index.js @@ -56,6 +56,7 @@ class IpfsRouter { this.router.post('/relays', this.ipfsRESTController.getRelays) this.router.post('/connect', this.ipfsRESTController.connect) this.router.get('/node', this.ipfsRESTController.getThisNode) + this.router.get('/download/:cid', this.ipfsRESTController.downloadFile) // Attach the Controller routes to the Koa app. app.use(this.router.routes())