From b0ea14737f9d0304f90ab687ba092ff64b1ec691 Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Sat, 25 May 2024 18:26:24 -0400 Subject: [PATCH] added new import format --- .../migration/importFromLinkwarden.ts | 2 +- .../migration/importFromWallabag.ts | 115 ++++++++++++++++++ pages/api/v1/migration/index.ts | 6 +- pages/settings/account.tsx | 20 +++ types/global.ts | 1 + 5 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 lib/api/controllers/migration/importFromWallabag.ts diff --git a/lib/api/controllers/migration/importFromLinkwarden.ts b/lib/api/controllers/migration/importFromLinkwarden.ts index 3f91f19..fb486cb 100644 --- a/lib/api/controllers/migration/importFromLinkwarden.ts +++ b/lib/api/controllers/migration/importFromLinkwarden.ts @@ -54,7 +54,7 @@ export default async function importFromLinkwarden( // Import Links for (const link of e.links) { - const newLink = await prisma.link.create({ + await prisma.link.create({ data: { url: link.url, name: link.name, diff --git a/lib/api/controllers/migration/importFromWallabag.ts b/lib/api/controllers/migration/importFromWallabag.ts new file mode 100644 index 0000000..6f9f404 --- /dev/null +++ b/lib/api/controllers/migration/importFromWallabag.ts @@ -0,0 +1,115 @@ +import { prisma } from "@/lib/api/db"; +import { Backup } from "@/types/global"; +import createFolder from "@/lib/api/storage/createFolder"; + +const MAX_LINKS_PER_USER = Number(process.env.MAX_LINKS_PER_USER) || 30000; + +type WallabagBackup = { + is_archived: number; + is_starred: number; + tags: String[]; + is_public: boolean; + id: number; + title: string; + url: string; + content: string; + created_at: Date; + updated_at: Date; + published_by: string[]; + starred_at: Date; + annotations: any[]; + mimetype: string; + language: string; + reading_time: number; + domain_name: string; + preview_picture: string; + http_status: string; + headers: Record; +}[]; + +export default async function importFromWallabag( + userId: number, + rawData: string +) { + const data: WallabagBackup = JSON.parse(rawData); + + const backup = data.filter((e) => e.url); + + let totalImports = backup.length; + + const numberOfLinksTheUserHas = await prisma.link.count({ + where: { + collection: { + ownerId: userId, + }, + }, + }); + + if (totalImports + numberOfLinksTheUserHas > MAX_LINKS_PER_USER) + return { + response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`, + status: 400, + }; + + await prisma + .$transaction( + async () => { + const newCollection = await prisma.collection.create({ + data: { + owner: { + connect: { + id: userId, + }, + }, + name: "Imports", + }, + }); + + createFolder({ filePath: `archives/${newCollection.id}` }); + + for (const link of backup) { + await prisma.link.create({ + data: { + pinnedBy: link.is_starred + ? { connect: { id: userId } } + : undefined, + url: link.url, + name: link.title || "", + textContent: link.content || "", + importDate: link.created_at || null, + collection: { + connect: { + id: newCollection.id, + }, + }, + tags: + link.tags && link.tags[0] + ? { + connectOrCreate: link.tags.map((tag) => ({ + where: { + name_ownerId: { + name: tag.trim(), + ownerId: userId, + }, + }, + create: { + name: tag.trim(), + owner: { + connect: { + id: userId, + }, + }, + }, + })), + } + : undefined, + }, + }); + } + }, + { timeout: 30000 } + ) + .catch((err) => console.log(err)); + + return { response: "Success.", status: 200 }; +} diff --git a/pages/api/v1/migration/index.ts b/pages/api/v1/migration/index.ts index bfba453..0bf3d76 100644 --- a/pages/api/v1/migration/index.ts +++ b/pages/api/v1/migration/index.ts @@ -4,6 +4,7 @@ import importFromHTMLFile from "@/lib/api/controllers/migration/importFromHTMLFi import importFromLinkwarden from "@/lib/api/controllers/migration/importFromLinkwarden"; import { MigrationFormat, MigrationRequest } from "@/types/global"; import verifyUser from "@/lib/api/verifyUser"; +import importFromWallabag from "@/lib/api/controllers/migration/importFromWallabag"; export const config = { api: { @@ -32,9 +33,10 @@ export default async function users(req: NextApiRequest, res: NextApiResponse) { let data; if (request.format === MigrationFormat.htmlFile) data = await importFromHTMLFile(user.id, request.data); - - if (request.format === MigrationFormat.linkwarden) + else if (request.format === MigrationFormat.linkwarden) data = await importFromLinkwarden(user.id, request.data); + else if (request.format === MigrationFormat.wallabag) + data = await importFromWallabag(user.id, request.data); if (data) return res.status(data.status).json({ response: data.response }); } diff --git a/pages/settings/account.tsx b/pages/settings/account.tsx index 8318171..5020285 100644 --- a/pages/settings/account.tsx +++ b/pages/settings/account.tsx @@ -366,6 +366,26 @@ export default function Account() { /> +
  • + +
  • diff --git a/types/global.ts b/types/global.ts index 3e84223..fbdaea2 100644 --- a/types/global.ts +++ b/types/global.ts @@ -116,6 +116,7 @@ export type MigrationRequest = { export enum MigrationFormat { linkwarden, htmlFile, + wallabag, } export enum Plan {