I want to build a file sharing site kinda like nextcloud. The Idea is that their are files on a server (currently they are local), the svelte backend parses through them and presents them. You than can click through the folders to find the files you want and when you click on a file you can download this file.
Now parsing through the directory is working just fine. But i can't seem to figure out how to create an endpoint that returns a file for download.
The files should not be part of sveltekit so they are not static files, so i can't just download them by pointing a url to them.
So I tried to create a blob according to the filetype but i can't seem to work out how this really works.
Thats what i have so far for the endpoint.
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import fs from "fs";
import { resolve } from 'path';
export const GET = (async (event) => {
const filePath = resolve("."+event.url.pathname);
console.log(filePath);
var file = fs.readFileSync(filePath);
return json({
status:200,
headers: {
"Content-type" : "application/pdf",
"Content-Disposition": `attachment; filename=${event.url.pathname.split("/").pop()}`
},
body: file
});
}) satisfies RequestHandler;