2023-04-23 10:46:14 +00:00
|
|
|
import { log } from '$lib/log';
|
2023-04-02 15:41:33 +00:00
|
|
|
import { TimelineReader } from '$lib/server/timeline';
|
2023-04-02 15:37:55 +00:00
|
|
|
import type { HandleServerError } from '@sveltejs/kit';
|
2023-05-02 15:31:16 +00:00
|
|
|
import { error } from '@sveltejs/kit';
|
2023-04-05 14:21:43 +00:00
|
|
|
import fs from 'fs/promises';
|
2023-04-02 15:37:55 +00:00
|
|
|
|
2023-04-02 15:41:33 +00:00
|
|
|
TimelineReader.init();
|
|
|
|
|
2023-04-02 15:37:55 +00:00
|
|
|
export const handleError = (({ error }) => {
|
|
|
|
if (error instanceof Error) {
|
2023-04-23 10:46:14 +00:00
|
|
|
log.error('Something went wrong: ', error.name, error.message);
|
2023-04-02 15:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2023-04-22 07:28:42 +00:00
|
|
|
message: `Something went wrong! ${error}`
|
2023-04-02 15:37:55 +00:00
|
|
|
};
|
2023-04-05 14:21:43 +00:00
|
|
|
}) satisfies HandleServerError;
|
|
|
|
|
|
|
|
import type { Handle } from '@sveltejs/kit';
|
|
|
|
|
|
|
|
export const handle = (async ({ event, resolve }) => {
|
2023-04-07 15:06:27 +00:00
|
|
|
// Reeder *insists* on checking /feed instead of /feed.xml
|
|
|
|
if (event.url.pathname === '/feed') {
|
|
|
|
return new Response('', { status: 301, headers: { Location: '/feed.xml' } });
|
|
|
|
}
|
2023-04-05 14:57:25 +00:00
|
|
|
if (event.url.pathname === '/feed.xml') {
|
|
|
|
const f = await fs.readFile('feed.xml', { encoding: 'utf8' });
|
2023-04-11 14:02:54 +00:00
|
|
|
return new Response(f, { headers: [['Content-Type', 'application/atom+xml']] });
|
2023-04-05 14:21:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 15:31:16 +00:00
|
|
|
// 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);
|
2024-01-22 16:26:36 +00:00
|
|
|
error(404);
|
2023-05-02 15:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:21:43 +00:00
|
|
|
const response = await resolve(event);
|
|
|
|
return response;
|
2023-04-11 14:02:54 +00:00
|
|
|
}) satisfies Handle;
|