feat: Full upload functionality
This commit is contained in:
parent
724e473755
commit
188d3e8318
@ -4,9 +4,13 @@
|
|||||||
|
|
||||||
let files: FileList;
|
let files: FileList;
|
||||||
|
|
||||||
function submit() {
|
async function submit(e: SubmitEvent) {
|
||||||
|
e.preventDefault();
|
||||||
if (files.length === 0) return;
|
if (files.length === 0) return;
|
||||||
generateStream(files[0]);
|
const success = await generateStream(files[0]);
|
||||||
|
if (success) {
|
||||||
|
// success feedback
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
export async function generateStream(file: File): Promise<boolean> {
|
export async function generateStream(file: File): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
|
|
||||||
await fetch(`/api/upload/`, {
|
await fetch(`/api/upload/`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: file,
|
body: formData,
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -31,10 +31,16 @@ class MinioClient {
|
|||||||
console.log('Bucket "' + bucket + '" created in "us-east-1".');
|
console.log('Bucket "' + bucket + '" created in "us-east-1".');
|
||||||
}
|
}
|
||||||
|
|
||||||
const upload = await this.client.putObject(bucket, v4(), stream);
|
const objectId = v4();
|
||||||
console.log(upload);
|
const upload = await this.client.putObject(bucket, objectId, stream);
|
||||||
|
return {
|
||||||
|
bucket,
|
||||||
|
objectId,
|
||||||
|
etag: upload.etag,
|
||||||
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`Error uploading file: ${(e as Error).message}`);
|
console.error(`Error uploading file: ${(e as Error).message}`);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { error } from '@sveltejs/kit';
|
import { error, json } from '@sveltejs/kit';
|
||||||
import { auth } from '$lib/server/db/auth';
|
import { auth } from '$lib/server/db/auth';
|
||||||
|
import { fsClient } from '$lib/server/storage/minio-client';
|
||||||
|
import { Readable } from 'stream';
|
||||||
|
|
||||||
export const POST = async ({ request }) => {
|
export const POST = async ({ request }) => {
|
||||||
const session = await auth.api.getSession({
|
const session = await auth.api.getSession({
|
||||||
@ -10,5 +12,22 @@ export const POST = async ({ request }) => {
|
|||||||
return error(401, 'Not authorized. Please sign up at /sign-up');
|
return error(401, 'Not authorized. Please sign up at /sign-up');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(undefined, { status: 204 });
|
try {
|
||||||
|
const formData = await request.formData();
|
||||||
|
const file = formData.get('file');
|
||||||
|
|
||||||
|
if (!file || !(file instanceof File)) {
|
||||||
|
return error(400, 'No file provided');
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await file.arrayBuffer();
|
||||||
|
const stream = Readable.from(Buffer.from(buffer));
|
||||||
|
|
||||||
|
const uploadResponse = await fsClient?.uploadProfile(stream);
|
||||||
|
|
||||||
|
return json(uploadResponse);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
return error(500, 'Error uploading file');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user