a lot of work, but i'd guess mostly api in the works
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { db } from '$lib/server/db';
|
||||
import { filesystem } from '$lib/server/db/schema';
|
||||
import { MD5 } from '$lib/termos/util/hash';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
export async function GET({ request }): Promise<Response> {
|
||||
const row = await db
|
||||
.select({
|
||||
data: filesystem.data,
|
||||
checksum: filesystem.checksum
|
||||
})
|
||||
.from(filesystem)
|
||||
.where(eq(filesystem.id, 1))
|
||||
.limit(1);
|
||||
|
||||
if (!row.length) {
|
||||
return new Response('Filesystem is not initialized on the database', { status: 500 });
|
||||
}
|
||||
|
||||
const { data, checksum } = row[0];
|
||||
const clientETag = request.headers.get('if-none-match');
|
||||
|
||||
if (clientETag === `"${checksum}"`) {
|
||||
return new Response(null, {
|
||||
status: 304,
|
||||
headers: { ETag: `"${checksum}"` }
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(data, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
ETag: `"${checksum}"`,
|
||||
'Cache-Control': 'no-cache'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST({ request }) {
|
||||
const newData = await request.text();
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(newData);
|
||||
//TODO: Schema validation
|
||||
} catch {
|
||||
//TODO: Change that to a proper http headers error
|
||||
return new Response(null, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
const checksum: string = MD5.hash(newData);
|
||||
|
||||
await db.update(filesystem).set({ data: newData, checksum }).where(eq(filesystem.id, 1));
|
||||
|
||||
return new Response(JSON.stringify({ success: true }), { status: 200 });
|
||||
}
|
||||
Reference in New Issue
Block a user