feat: expose and display duplicates

This commit is contained in:
William Valentin
2026-02-04 23:38:24 -08:00
parent 1952fbaf30
commit 83f3ff1f69
4 changed files with 179 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
import { z } from "zod";
import { getDb } from "@tline/db";
const paramsSchema = z.object({ id: z.string().uuid() });
type DbLike = ReturnType<typeof getDb>;
export async function handleGetDupes(input: {
params: { id: string };
db?: DbLike;
}): Promise<{ status: number; body: unknown }> {
const paramsParsed = paramsSchema.safeParse(input.params);
if (!paramsParsed.success) {
return {
status: 400,
body: { error: "invalid_params", issues: paramsParsed.error.issues },
};
}
const db = (input.db ?? getDb()) as DbLike;
const hashRows = await db<
{
bucket: string;
sha256: string;
}[]
>`
select bucket, sha256
from asset_hashes
where asset_id = ${paramsParsed.data.id}
limit 1
`;
const hash = hashRows[0];
if (!hash) {
return { status: 200, body: { items: [] } };
}
const dupes = await db<
{
id: string;
media_type: "image" | "video";
status: "new" | "processing" | "ready" | "failed";
}[]
>`
select a.id, a.media_type, a.status
from assets a
join asset_hashes h on h.asset_id = a.id
where h.bucket = ${hash.bucket}
and h.sha256 = ${hash.sha256}
and a.id <> ${paramsParsed.data.id}
order by a.id asc
`;
return { status: 200, body: { items: dupes } };
}

View File

@@ -0,0 +1,12 @@
import { handleGetDupes } from "./handlers";
export const runtime = "nodejs";
export async function GET(
_request: Request,
context: { params: Promise<{ id: string }> },
): Promise<Response> {
const rawParams = await context.params;
const result = await handleGetDupes({ params: rawParams });
return Response.json(result.body, { status: result.status });
}