|
@@ -0,0 +1,42 @@
|
|
|
|
|
+import { createReadStream } from 'node:fs';
|
|
|
|
|
+import { stat } from 'node:fs/promises';
|
|
|
|
|
+import { homedir } from 'node:os';
|
|
|
|
|
+import { join } from 'node:path';
|
|
|
|
|
+import { Readable } from 'node:stream';
|
|
|
|
|
+import { error } from '@sveltejs/kit';
|
|
|
|
|
+
|
|
|
|
|
+export const prerender = false;
|
|
|
|
|
+
|
|
|
|
|
+const ROOT = join(homedir(), 'srv/leemorgan_dev/currencyimages');
|
|
|
|
|
+
|
|
|
|
|
+const TYPES = {
|
|
|
|
|
+ jpg: 'image/jpeg',
|
|
|
|
|
+ jpeg: 'image/jpeg',
|
|
|
|
|
+ png: 'image/png',
|
|
|
|
|
+ webp: 'image/webp',
|
|
|
|
|
+ gif: 'image/gif'
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export async function GET({ params }) {
|
|
|
|
|
+ const file = params.file;
|
|
|
|
|
+ if (!/^[A-Za-z0-9._-]+$/.test(file)) error(400, 'Invalid file');
|
|
|
|
|
+
|
|
|
|
|
+ const path = join(ROOT, file);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ const info = await stat(path);
|
|
|
|
|
+ if (!info.isFile()) error(404, 'Not found');
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ error(404, 'Not found');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const ext = file.split('.').pop()?.toLowerCase() ?? '';
|
|
|
|
|
+ const body = Readable.toWeb(createReadStream(path));
|
|
|
|
|
+
|
|
|
|
|
+ return new Response(body, {
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'content-type': TYPES[ext] ?? 'application/octet-stream',
|
|
|
|
|
+ 'cache-control': 'public, max-age=31536000, immutable'
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+}
|