Skip to content

Commit

Permalink
chore(read): add read endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemon committed Jun 13, 2023
1 parent 63c077d commit 286a84b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ipfsConfig = {
};

exports.create = create;
exports.get = get;
exports.resolve = resolve;
exports.publish = publish;
exports.pin = pin;
Expand All @@ -33,6 +34,10 @@ async function create(content) {
return data;
}

async function get(addr) {
return getContent(ipfsConfig.gateway + '/ipfs/' + addr)
}

async function pin(addr) {
if (!addr.includes('ipfs')) {
addr = '/ipfs/' + addr;
Expand Down
13 changes: 13 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ router.post("/upload", upload.single("file"), async function (req, res, next) {
}
});

router.get("/:cid", async function (req, res, next) {
const file = await ipfsModel.findOne({ path: req.params.cid });
if (file === undefined) {
return next(createError(404, 'File not found.'));
}
const content = await ipfs.get(file.path);
if (content === undefined) {
return next(createError(500, 'Unable to fetch file from IPFS.'));
}
res.set('Content-Type', file.type);
res.status(200).send(Buffer.from(content, 'base64'));
});

async function handleIpfs(bitmap, options) {
let uploaded = await ipfs.create(bitmap);

Expand Down

0 comments on commit 286a84b

Please sign in to comment.