Skip to content

Commit

Permalink
Add image handler to push route
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-m-2983 committed Jul 11, 2024
1 parent 2ed4f26 commit 6766522
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
48 changes: 42 additions & 6 deletions src/app/api/push/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ interface Entry {
const prismaClient = new PrismaClient();

export async function POST(request: NextRequest) {
const data: Entry[] = await request.json();
const contentType: string | null = request.headers.get("Content-Type");

if (contentType == null) return NextResponse.error();

if (contentType === "application/json") {
return handleDocuments(request);
} else if (contentType.startsWith("image")) {
return handleImage(request);
}
return NextResponse.error();
}

async function handleDocuments(request: NextRequest): Promise<Response> {
const data: Entry[] = await request.json();
const checksum = MD5(JSON.stringify(data)).toString();

const entryIds = (await prismaClient.entry.createManyAndReturn({
Expand All @@ -31,14 +43,32 @@ export async function POST(request: NextRequest) {

entryIds.forEach((id, index) => writeEntry(id, data[index]));

if (entryIds.length == data.length) return NextResponse.json({
hash: checksum,
entryIds: entryIds
});
if (entryIds.length == data.length) {
return NextResponse.json({
hash: checksum,
entryIds: entryIds
})
}

return NextResponse.error();
}

async function handleImage(request: NextRequest): Promise<Response> {
const imageContents: string = await request.text();
const { imagesDir } = useDataDir();

const id: string = (await prismaClient.image.create({ select: { id: true } })).id;

writeFileSync(`${imagesDir}/${id}.turbo.image`, imageContents);

const checksum = MD5(JSON.stringify(imageContents)).toString();

return NextResponse.json({
id: id,
checksum: checksum
});
}

function writeEntry(id: string, entry: Entry) {
const { documentsDir } = useDataDir();

Expand All @@ -58,5 +88,11 @@ function useDataDir() {
mkdirSync(documentsDir);
}

return { dataDir: dataDir, documentsDir: documentsDir };
const imagesDir = "./turbo-data/images";

if (!existsSync(imagesDir)) {
mkdirSync(imagesDir);
}

return { dataDir: dataDir, documentsDir: documentsDir, imagesDir: imagesDir };
}
18 changes: 0 additions & 18 deletions src/app/lib/images.tsx

This file was deleted.

0 comments on commit 6766522

Please sign in to comment.