Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/lib/client/sync-file-system/sfs-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileSystemFile> {
Expand Down
22 changes: 22 additions & 0 deletions src/routes/fs/project/+server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Writable } from 'stream';

import { Exception } from '@utils/exception';

import { useRequestHandler } from '@utils-server/request-handler';
Expand All @@ -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 });
});
Loading