Add logging

This commit is contained in:
PJ 2022-02-16 11:24:13 +01:00
parent 2b6a738405
commit 37109a35b3
No known key found for this signature in database
GPG Key ID: F345964979FA8971
2 changed files with 18 additions and 2 deletions

View File

@ -85,12 +85,16 @@ async function handleGetLink(req: Request, res: Response, recordsDB: Collection<
} }
async function reuploadFile(cid: string, recordsDB: Collection<IRecord>): Promise<string> { async function reuploadFile(cid: string, recordsDB: Collection<IRecord>): Promise<string> {
console.log('reuploading file with cid', cid)
// 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);
console.log('extension found', ext)
// find out whether it's a directory // find out whether it's a directory
const isDir = await isDirectory(cid); const isDir = await isDirectory(cid);
console.log('is directory', isDir)
// upload directory // upload directory
if (isDir) { if (isDir) {
@ -118,10 +122,21 @@ async function reuploadFile(cid: string, recordsDB: Collection<IRecord>): Promis
// download cid as file // download cid as file
const filePath = `${UPLOAD_PATH}/${cid}.${ext}`; const filePath = `${UPLOAD_PATH}/${cid}.${ext}`;
await download(cid, filePath, isDir); try {
await download(cid, filePath, isDir);
} catch (error) {
console.log('download error: ', error)
throw error;
}
// upload the file // upload the file
const skylink = await uploadFile(filePath); let skylink;
try {
skylink = await uploadFile(filePath);
} catch (error) {
console.log('upload error: ', error)
throw error;
}
// cleanup files // cleanup files
fs.unlinkSync(filePath); fs.unlinkSync(filePath);

View File

@ -20,6 +20,7 @@ export async function isDirectory(cid: string): Promise<boolean> {
export async function download(cid: string, destination: string, directory: boolean): Promise<boolean> { export async function download(cid: string, destination: string, directory: boolean): Promise<boolean> {
const url = directory ? `${IPFS_INFURA_API}/api/v0/get?arg=${cid}&archive=true` : `${IPFS_GATEWAY}/${cid}`; const url = directory ? `${IPFS_INFURA_API}/api/v0/get?arg=${cid}&archive=true` : `${IPFS_GATEWAY}/${cid}`;
console.log('downloading from url', url)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const downloadStream = got.stream(url); const downloadStream = got.stream(url);