feat: add extensive test suite
This commit is contained in:
+10
-26
@@ -1,39 +1,23 @@
|
||||
"use server";
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import type { PgDatabase } from "drizzle-orm/pg-core";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { actionLog, cdnify } from "@/lib/api";
|
||||
import { adminCheck } from "@/lib/auth";
|
||||
import { db } from "@/lib/db";
|
||||
import { cdn } from "@/lib/db/schema";
|
||||
import { fetchToCdnService } from "./service";
|
||||
|
||||
export async function fetchToCdn(urls: string[]) {
|
||||
if (!(await adminCheck())) throw "Unauthorized";
|
||||
|
||||
async function toFile(res: Response) {
|
||||
if (!res.ok) throw "File failed to download";
|
||||
return new File(
|
||||
[await res.blob()],
|
||||
res.headers
|
||||
.get("Content-Disposition")
|
||||
?.match(/^attachment;.*filename="([a-zA-Z0-9-_ .]+)"/)?.[1] ||
|
||||
decodeURIComponent(new URL(res.url).pathname).match(
|
||||
/\/([a-zA-Z0-9-_ .]+)$/,
|
||||
)?.[1] ||
|
||||
`file.${res.headers.get("Content-Type")?.split("/")[1] || "dat"}`,
|
||||
);
|
||||
}
|
||||
return db
|
||||
.transaction(async (tx) =>
|
||||
Promise.all(
|
||||
urls.map((url) =>
|
||||
fetch(url, { redirect: "follow" })
|
||||
.then(toFile)
|
||||
.then((f) => cdnify(f, { tx })),
|
||||
),
|
||||
),
|
||||
)
|
||||
.finally(() => revalidatePath("/admin/cdn"));
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Drizzle transaction type parameters
|
||||
return fetchToCdnService<PgDatabase<any, any, any>, string>(urls, {
|
||||
adminCheck,
|
||||
fetch: (url) => fetch(url, { redirect: "follow" }),
|
||||
transaction: (callback) => db.transaction(callback),
|
||||
importFile: (file, tx) => cdnify(file, { tx }),
|
||||
afterImport: () => revalidatePath("/admin/cdn"),
|
||||
});
|
||||
}
|
||||
|
||||
export async function rename(id: string, name: string) {
|
||||
|
||||
Reference in New Issue
Block a user