diff --git a/lib/api/archive.ts b/lib/api/archive.ts index 24c3ff6..80aa655 100644 --- a/lib/api/archive.ts +++ b/lib/api/archive.ts @@ -2,11 +2,7 @@ import { Page, chromium, devices } from "playwright"; import { prisma } from "@/lib/api/db"; import createFile from "@/lib/api/storage/createFile"; -export default async function archive( - url: string, - collectionId: number, - linkId: number -) { +export default async function archive(linkId: number, url: string) { const browser = await chromium.launch(); const context = await browser.newContext(devices["Desktop Chrome"]); const page = await context.newPage(); @@ -35,12 +31,12 @@ export default async function archive( createFile({ data: screenshot, - filePath: `archives/${collectionId}/${linkId}.png`, + filePath: `archives/${linkExists.collectionId}/${linkId}.png`, }); createFile({ data: pdf, - filePath: `archives/${collectionId}/${linkId}.pdf`, + filePath: `archives/${linkExists.collectionId}/${linkId}.pdf`, }); } diff --git a/lib/api/controllers/links/postLink.ts b/lib/api/controllers/links/postLink.ts index 3653eb9..f81a59f 100644 --- a/lib/api/controllers/links/postLink.ts +++ b/lib/api/controllers/links/postLink.ts @@ -97,7 +97,7 @@ export default async function postLink( createFolder({ filePath: `archives/${newLink.collectionId}` }); - archive(newLink.url, newLink.collectionId, newLink.id); + archive(newLink.id, newLink.url); return { response: newLink, status: 200 }; } diff --git a/lib/api/controllers/links/updateLink.ts b/lib/api/controllers/links/updateLink.ts index cdd0f67..24836c1 100644 --- a/lib/api/controllers/links/updateLink.ts +++ b/lib/api/controllers/links/updateLink.ts @@ -2,6 +2,7 @@ import { prisma } from "@/lib/api/db"; import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; import { Collection, Link, UsersAndCollections } from "@prisma/client"; import getPermission from "@/lib/api/getPermission"; +import moveFile from "@/lib/api/storage/moveFile"; export default async function updateLink( link: LinkIncludingShortenedCollectionAndTags, @@ -98,6 +99,18 @@ export default async function updateLink( }, }); + if (targetLink.collection.id !== link.collection.id) { + await moveFile( + `archives/${targetLink.collection.id}/${link.id}.pdf`, + `archives/${link.collection.id}/${link.id}.pdf` + ); + + await moveFile( + `archives/${targetLink.collection.id}/${link.id}.png`, + `archives/${link.collection.id}/${link.id}.png` + ); + } + return { response: updatedLink, status: 200 }; } } diff --git a/lib/api/storage/moveFile.ts b/lib/api/storage/moveFile.ts new file mode 100644 index 0000000..bbd8887 --- /dev/null +++ b/lib/api/storage/moveFile.ts @@ -0,0 +1,37 @@ +import fs from "fs"; +import path from "path"; +import s3Client from "./s3Client"; +import removeFile from "./removeFile"; + +export default async function moveFile(from: string, to: string) { + if (s3Client) { + const Bucket = process.env.BUCKET_NAME; + + const copyParams = { + Bucket: Bucket, + CopySource: `/${Bucket}/${from}`, + Key: to, + }; + + try { + s3Client.copyObject(copyParams, async (err: any) => { + if (err) { + console.error("Error copying the object:", err); + } else { + await removeFile({ filePath: from }); + } + }); + } catch (err) { + console.log("Error:", err); + } + } else { + const storagePath = process.env.STORAGE_FOLDER || "data"; + + const directory = (file: string) => + path.join(process.cwd(), storagePath + "/" + file); + + fs.rename(directory(from), directory(to), (err) => { + if (err) console.log("Error copying file:", err); + }); + } +}