empty skylink issue

This commit is contained in:
Karol Wypchlo 2021-09-29 19:20:15 +02:00
parent 16353ca634
commit c1b454dc10
No known key found for this signature in database
GPG Key ID: C92C016317A964D0
1 changed files with 12 additions and 37 deletions

View File

@ -3,23 +3,10 @@ import express, { Request, Response } from "express";
import { rmdirSync, unlinkSync } from "fs"; import { rmdirSync, unlinkSync } from "fs";
import { extension as toExtension } from "mime-types"; import { extension as toExtension } from "mime-types";
import { Collection } from "mongodb"; import { Collection } from "mongodb";
import { import { API_HOSTNAME, API_PORT, MONGO_CONNECTIONSTRING, MONGO_DBNAME, UPLOAD_PATH } from "./consts";
API_HOSTNAME,
API_PORT,
MONGO_CONNECTIONSTRING,
MONGO_DBNAME,
UPLOAD_PATH,
} from "./consts";
import { MongoDB } from "./mongodb"; import { MongoDB } from "./mongodb";
import { IRecord } from "./types"; import { IRecord } from "./types";
import { import { contentType, download, extractArchive, isDirectory, uploadDirectory, uploadFile } from "./utils";
contentType,
download,
extractArchive,
isDirectory,
uploadDirectory,
uploadFile,
} from "./utils";
require("dotenv").config(); require("dotenv").config();
@ -52,30 +39,21 @@ require("dotenv").config();
}); });
})(); })();
async function handleGetLink( async function handleGetLink(req: Request, res: Response, recordsDB: Collection<IRecord>) {
req: Request,
res: Response,
recordsDB: Collection<IRecord>
) {
try { try {
const { cid } = req.params; const { cid } = req.params;
const record = await recordsDB.findOne({ cid }); const record = await recordsDB.findOne({ cid });
if (record && record.skylink) { if (record) {
res.status(200).send({ skylink: record.skylink }); if (record.skylink) {
return; 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 // reupload the cid and return the skylink
const skylink = await reuploadFile(cid, recordsDB); const skylink = await reuploadFile(cid, recordsDB);
res.status(200).send({ skylink }); res.status(200).send({ skylink });
@ -86,10 +64,7 @@ async function handleGetLink(
} }
} }
async function reuploadFile( async function reuploadFile(cid: string, recordsDB: Collection<IRecord>): Promise<string> {
cid: string,
recordsDB: Collection<IRecord>
): Promise<string> {
// get the content type // get the content type
const ct = await contentType(cid); const ct = await contentType(cid);
const ext = toExtension(ct); const ext = toExtension(ct);