feat: Actually make uploading profile image update it

This commit is contained in:
April Hall 2025-02-22 02:31:27 -05:00
parent 3d467e8219
commit 24257fea8c
Signed by: arithefirst
GPG Key ID: 4508A15C4DB91C5B
5 changed files with 54 additions and 2 deletions

View File

@ -3,7 +3,7 @@ export async function generateStream(file: File): Promise<boolean> {
const formData = new FormData(); const formData = new FormData();
formData.append('file', file); formData.append('file', file);
await fetch(`/api/upload/`, { await fetch(`/api/set-profile-photo/`, {
method: 'POST', method: 'POST',
body: formData, body: formData,
}); });

View File

@ -13,6 +13,14 @@ class AuthDb {
this.client.pragma('journal_mode = WAL'); this.client.pragma('journal_mode = WAL');
} }
setUserImage(userId: string, image: string) {
try {
this.client.prepare('UPDATE user SET image = ? WHERE id = ?').run(image, userId);
} catch (e) {
console.error(`Error setting user image: ${(e as Error).message}`);
}
}
getUser(userId: string): Profile { getUser(userId: string): Profile {
const row = this.client.prepare('SELECT username, image FROM user WHERE id = ?').get(userId); const row = this.client.prepare('SELECT username, image FROM user WHERE id = ?').get(userId);
return { return {

View File

@ -34,6 +34,17 @@ class MinioClient {
} }
} }
async fetchProfilePhoto(objectId: string) {
try {
const bucket = 'profile-photos';
const object = await this.client.getObject(bucket, objectId);
return object;
} catch (e) {
console.error(`Error fetching file: ${(e as Error).message}`);
throw e;
}
}
async uploadProfile(stream: Readable, mime: string) { async uploadProfile(stream: Readable, mime: string) {
try { try {
const bucket = 'profile-photos'; const bucket = 'profile-photos';

View File

@ -0,0 +1,30 @@
import { fsClient } from '$lib/server/storage/minio-client';
import { error } from '@sveltejs/kit';
export const GET = async ({ params }) => {
const { filename } = params;
try {
const stream = await fsClient?.fetchProfilePhoto(filename);
if (!stream) {
return error(404, { message: 'File not found' });
}
const readableStream = new ReadableStream({
start(controller) {
stream.on('data', (chunk) => controller.enqueue(chunk));
stream.on('end', () => controller.close());
stream.on('error', (err) => controller.error(err));
},
});
return new Response(readableStream, {
headers: {
'Content-Type': `image/${filename.split('.').pop()}`,
'Cache-Control': 'public, max-age=31536000',
},
});
} catch {
return error(404, { message: 'File not found' });
}
};

View File

@ -1,5 +1,6 @@
import { error, json } from '@sveltejs/kit'; import { error, json } from '@sveltejs/kit';
import { auth } from '$lib/server/db/auth'; import { auth } from '$lib/server/db/auth';
import { authdb } from '$lib/server/db/sqlite';
import { fsClient } from '$lib/server/storage/minio-client'; import { fsClient } from '$lib/server/storage/minio-client';
import { Readable } from 'stream'; import { Readable } from 'stream';
@ -23,11 +24,13 @@ export const POST = async ({ request }) => {
const buffer = await file.arrayBuffer(); const buffer = await file.arrayBuffer();
const stream = Readable.from(Buffer.from(buffer)); const stream = Readable.from(Buffer.from(buffer));
console.log('Uploading profile photo');
const uploadResponse = await fsClient?.uploadProfile(stream, file.type); const uploadResponse = await fsClient?.uploadProfile(stream, file.type);
authdb.setUserImage(session.user.id, `/api/images/${uploadResponse?.objectId}`);
return json(uploadResponse); return json(uploadResponse);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return error(500, 'Error uploading file'); return error(500, (e as Error).message);
} }
}; };