From c1b454dc101f3a2f7c376dc852f006179fcbb27c Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Wed, 29 Sep 2021 19:20:15 +0200 Subject: [PATCH] empty skylink issue --- packages/ipfs-api/src/server.ts | 49 ++++++++------------------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/packages/ipfs-api/src/server.ts b/packages/ipfs-api/src/server.ts index 9523f63e..2864572c 100644 --- a/packages/ipfs-api/src/server.ts +++ b/packages/ipfs-api/src/server.ts @@ -3,23 +3,10 @@ import express, { Request, Response } from "express"; import { rmdirSync, unlinkSync } from "fs"; import { extension as toExtension } from "mime-types"; import { Collection } from "mongodb"; -import { - API_HOSTNAME, - API_PORT, - MONGO_CONNECTIONSTRING, - MONGO_DBNAME, - UPLOAD_PATH, -} from "./consts"; +import { API_HOSTNAME, API_PORT, MONGO_CONNECTIONSTRING, MONGO_DBNAME, UPLOAD_PATH } from "./consts"; import { MongoDB } from "./mongodb"; import { IRecord } from "./types"; -import { - contentType, - download, - extractArchive, - isDirectory, - uploadDirectory, - uploadFile, -} from "./utils"; +import { contentType, download, extractArchive, isDirectory, uploadDirectory, uploadFile } from "./utils"; require("dotenv").config(); @@ -52,30 +39,21 @@ require("dotenv").config(); }); })(); -async function handleGetLink( - req: Request, - res: Response, - recordsDB: Collection -) { +async function handleGetLink(req: Request, res: Response, recordsDB: Collection) { try { const { cid } = req.params; const record = await recordsDB.findOne({ cid }); - if (record && record.skylink) { - res.status(200).send({ skylink: record.skylink }); - return; + if (record) { + if (record.skylink) { + res.status(200).send({ skylink: record.skylink }); + return; + } + } else { + // insert an empty record for the cid + await recordsDB.insertOne({ cid, createdAt: new Date(), skylink: "" }); } - // if (record && !record.skylink) { - // // TODO: we can retry the IPFS download and skynet upload here if - // // time.Since(createdAt) exceeds some threshold - // res.status(200).send({ error: "processing" }); - // return; - // } - - // insert an empty record for the cid - await recordsDB.insertOne({ cid, createdAt: new Date(), skylink: "" }); - // reupload the cid and return the skylink const skylink = await reuploadFile(cid, recordsDB); res.status(200).send({ skylink }); @@ -86,10 +64,7 @@ async function handleGetLink( } } -async function reuploadFile( - cid: string, - recordsDB: Collection -): Promise { +async function reuploadFile(cid: string, recordsDB: Collection): Promise { // get the content type const ct = await contentType(cid); const ext = toExtension(ct);