This commit is contained in:
Daniel 2023-08-06 00:58:18 -04:00
parent 02b7a90160
commit 159075b38b
4 changed files with 54 additions and 8 deletions

View File

@ -2,11 +2,7 @@ import { Page, chromium, devices } from "playwright";
import { prisma } from "@/lib/api/db"; import { prisma } from "@/lib/api/db";
import createFile from "@/lib/api/storage/createFile"; import createFile from "@/lib/api/storage/createFile";
export default async function archive( export default async function archive(linkId: number, url: string) {
url: string,
collectionId: number,
linkId: number
) {
const browser = await chromium.launch(); const browser = await chromium.launch();
const context = await browser.newContext(devices["Desktop Chrome"]); const context = await browser.newContext(devices["Desktop Chrome"]);
const page = await context.newPage(); const page = await context.newPage();
@ -35,12 +31,12 @@ export default async function archive(
createFile({ createFile({
data: screenshot, data: screenshot,
filePath: `archives/${collectionId}/${linkId}.png`, filePath: `archives/${linkExists.collectionId}/${linkId}.png`,
}); });
createFile({ createFile({
data: pdf, data: pdf,
filePath: `archives/${collectionId}/${linkId}.pdf`, filePath: `archives/${linkExists.collectionId}/${linkId}.pdf`,
}); });
} }

View File

@ -97,7 +97,7 @@ export default async function postLink(
createFolder({ filePath: `archives/${newLink.collectionId}` }); createFolder({ filePath: `archives/${newLink.collectionId}` });
archive(newLink.url, newLink.collectionId, newLink.id); archive(newLink.id, newLink.url);
return { response: newLink, status: 200 }; return { response: newLink, status: 200 };
} }

View File

@ -2,6 +2,7 @@ import { prisma } from "@/lib/api/db";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { Collection, Link, UsersAndCollections } from "@prisma/client"; import { Collection, Link, UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission"; import getPermission from "@/lib/api/getPermission";
import moveFile from "@/lib/api/storage/moveFile";
export default async function updateLink( export default async function updateLink(
link: LinkIncludingShortenedCollectionAndTags, 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 }; return { response: updatedLink, status: 200 };
} }
} }

View File

@ -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);
});
}
}