62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { log } from '$lib/log';
|
|
import { TimelineReader } from '$lib/server/timeline';
|
|
import type { Handle, HandleServerError } from '@sveltejs/kit';
|
|
import { error } from '@sveltejs/kit';
|
|
import fs from 'fs/promises';
|
|
|
|
log.log('App startup');
|
|
TimelineReader.init();
|
|
|
|
export const handleError = (({ error }) => {
|
|
if (error instanceof Error) {
|
|
log.error('Something went wrong: ', error.name, error.message);
|
|
}
|
|
|
|
return {
|
|
message: `Something went wrong! ${error}`
|
|
};
|
|
}) satisfies HandleServerError;
|
|
|
|
export const handle = (async ({ event, resolve }) => {
|
|
// Reeder *insists* on checking /feed instead of /feed.xml
|
|
if (event.url.pathname === '/feed') {
|
|
return new Response('', { status: 301, headers: { Location: '/feed.xml' } });
|
|
}
|
|
if (event.url.pathname === '/feed.xml') {
|
|
const f = await fs.readFile('feed.xml', { encoding: 'utf8' });
|
|
return new Response(f, { headers: [['Content-Type', 'application/atom+xml']] });
|
|
}
|
|
|
|
// Ideally, this would be served by apache
|
|
if (event.url.pathname.startsWith('/avatars/')) {
|
|
const fileName = event.url.pathname.split('/').pop() ?? 'unknown.jpeg';
|
|
const suffix = fileName.split('.').pop() ?? 'jpeg';
|
|
try {
|
|
//This should work, but doesn't yet. See: https://github.com/nodejs/node/issues/45853
|
|
/*
|
|
const stat = await fs.stat('avatars/' + fileName);
|
|
const fd = await fs.open('avatars/' + fileName);
|
|
const readStream = fd
|
|
.readableWebStream()
|
|
.getReader({ mode: 'byob' }) as ReadableStream<Uint8Array>;
|
|
log.info('sending. size: ', stat.size);
|
|
return new Response(readStream, {
|
|
headers: [
|
|
['Content-Type', 'image/' + suffix],
|
|
['Content-Length', stat.size.toString()]
|
|
]
|
|
});
|
|
*/
|
|
|
|
const f = await fs.readFile('avatars/' + fileName);
|
|
return new Response(f, { headers: [['Content-Type', 'image/' + suffix]] });
|
|
} catch (e) {
|
|
log.error('no stream', e);
|
|
error(404);
|
|
}
|
|
}
|
|
|
|
const response = await resolve(event);
|
|
return response;
|
|
}) satisfies Handle;
|