chore: less jank image path

This commit is contained in:
2025-04-05 14:54:44 +02:00
parent 87d7e5752b
commit ee1ef27be5
3 changed files with 39 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
import { validateRequest } from '@/lib/auth/validate';
import fsP from 'fs/promises';
import fs from 'fs';
export async function GET(request: Request, { params }: { params: Promise<{ username: string }> }) {
const { username } = await params;
const { user } = await validateRequest();
if (!user) {
return new Response("Unauthorized", { status: 401 });
}
if (username.includes('..')) {
return new Response("nuh uh", { status: 403 });
}
const basePath = '/dev/shm/hctv-thumb';
const filePath = `${basePath}/${username}.webp`;
if (!fs.existsSync(filePath)) {
return new Response("Not Found", { status: 404 });
}
const fileContent = await fsP.readFile(filePath);
return new Response(fileContent, {
headers: {
'Content-Type': 'image/webp',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
},
});
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

View File

@@ -1,7 +1,9 @@
import { Worker } from 'bullmq';
import { getRedisConnection } from '@/lib/services/redis';
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import { existsSync } from 'node:fs';
const pExec = promisify(exec);
const globalForWorker = global as unknown as {
thumbnailWorker: Worker | null;
@@ -26,10 +28,16 @@ export async function registerThumbnailWorker(): Promise<void> {
// this is totally unnecessary, but i'll keep it for security purposes.
const name = job.data.name.replace(/[^a-zA-Z0-9]/g, '_');
const m3u8location = `/dev/shm/hls/${name}.m3u8`;
if (!existsSync(m3u8location)) return;
if (!existsSync('/dev/shm/hctv-thumb')) {
await pExec('mkdir -p /dev/shm/hctv-thumb');
}
// unnecessary for development, but maybe docker volumes mess with permissions in prod
await pExec('chown -R $USER /dev/shm/hctv-thumb');
exec(
`/usr/bin/ffmpeg -i ${m3u8location} -vframes 1 -an -y -f image2 /home/srizan/Documents/Development/hclive/apps/web/src/lib/${name}.jpg`,
`/usr/bin/ffmpeg -i ${m3u8location} -vframes 1 -an -y -f image2 /dev/shm/hctv-thumb/${name}.webp`,
(error) => {
if (error) {
console.error(`Error: ${error.message}`);