diff --git a/src/lib/client/sync-file-system/sfs-file.ts b/src/lib/client/sync-file-system/sfs-file.ts index 0bc3f16..1699728 100644 --- a/src/lib/client/sync-file-system/sfs-file.ts +++ b/src/lib/client/sync-file-system/sfs-file.ts @@ -63,11 +63,14 @@ export class SfsFile { if (!this._cache) return; const file = await this._cache.getFile(); const res = await fetch(this._route, { - body: file.stream(), + body: file, method: 'POST', - headers: { [SESSION_PROJECT_HEADER]: this._handler.project.id }, + headers: { + [SESSION_PROJECT_HEADER]: this._handler.project.id, + 'Content-Type': 'application/octet-stream', + }, }); - if (!res.ok) throw new Error('Failed to sync file'); + if (!res.ok) throw new Error(`Failed to sync file: ${await res.text()}`); } async getFile(): Promise { diff --git a/src/routes/fs/project/+server.ts b/src/routes/fs/project/+server.ts index 0856a26..5238e51 100644 --- a/src/routes/fs/project/+server.ts +++ b/src/routes/fs/project/+server.ts @@ -1,3 +1,5 @@ +import { Writable } from 'stream'; + import { Exception } from '@utils/exception'; import { useRequestHandler } from '@utils-server/request-handler'; @@ -18,3 +20,23 @@ export const GET = useRequestHandler(({ event, project }) => { return new Response(stream); }); + +/** + * To post a project file, use the following URL: + * /fs/project?path=path/to/file + * The path must be relative to /client + * Only the client is handled for now + */ +export const POST = useRequestHandler(async ({ event, project }) => { + const path = event.url.searchParams.get('path'); + if (!path) throw new Exception('Bad Request', 'Missing path query param', 400); + + const file = project.client.fs.getFile(decodeURIComponent(path).replace(/\?url$/, '')); + + if (!event.request.body) throw new Exception('Bad Request', 'Missing body', 400); + + const stream = file.getWriteStream(); + await event.request.body.pipeTo(Writable.toWeb(stream)); + + return Response.json({ success: true }); +});