From ff9c0a694107571c1707f6e2ea6b8aee366c5050 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Tue, 30 Jul 2024 08:38:58 +0200 Subject: [PATCH] Endpoint for name autocomplete --- src/lib/index.ts | 1 + src/routes/+page.server.ts | 4 +--- src/routes/names/+server.ts | 11 +++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 src/routes/names/+server.ts diff --git a/src/lib/index.ts b/src/lib/index.ts index b555291..c609a73 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,5 +1,6 @@ // place files you want to import through the `$lib` alias in this folder. const safePath = (input: string) => input.replace(/\W/g, ''); +export const storagePath: string = './uploads'; export default safePath; diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 793df56..0ad06d6 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,12 +1,10 @@ import { writeFileSync, mkdirSync, existsSync } from 'fs'; import { fail } from '@sveltejs/kit'; import type { RequestEvent } from './$types'; -import safePath from '$lib'; +import safePath, { storagePath } from '$lib'; import { hash } from 'crypto'; import path from 'path'; -const storagePath: string = './uploads'; - const mkdirIfNotExists = (path: string) => { if (!existsSync(path)) { mkdirSync(path); diff --git a/src/routes/names/+server.ts b/src/routes/names/+server.ts new file mode 100644 index 0000000..c69f717 --- /dev/null +++ b/src/routes/names/+server.ts @@ -0,0 +1,11 @@ +import { storagePath } from '$lib'; +import { json } from '@sveltejs/kit'; +import { readdirSync, statSync } from 'fs'; + +export function GET() { + const names = readdirSync(storagePath).filter((f) => + statSync(`${storagePath}/${f}`).isDirectory() + ); + + return json(names); +}