Add loader and new formats
Some checks failed
Bump deps (only minor versions) / ci (push) Failing after 11s

This commit is contained in:
0d0 2025-02-23 04:55:09 +01:00
parent 204bceeee3
commit dcb7cfec27
6 changed files with 148 additions and 40 deletions

View file

@ -1,12 +1,14 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getYouTubeMetadata, streamYouTube } from '$lib/server/ytdlp';
import { getYouTubeMetadata, streamYouTube, ytdl } from '$lib/server/ytdlp';
import { contentTypeFromFormat, mimeTypeMap } from '$lib/server/helpers';
export const GET: RequestHandler = async ({ url }) => {
// Get query params
const link = url.searchParams.get('link');
const format = url.searchParams.get('format'); // mp3, mp4
const source = url.searchParams.get('source'); // youtube or spotify
const metadata = url.searchParams.get('metadata');
// Validate input
if (!link || !format || !source) {
@ -18,22 +20,27 @@ export const GET: RequestHandler = async ({ url }) => {
}
try {
// Fetch metadata for filename
const metadata = await getYouTubeMetadata(link);
const { title, uploader } = metadata;
const safeTitle = `${uploader} - ${title}`;
const filename = `${safeTitle}.${format}`;
let filename = `you-clicked-no-metadata-so-i-cant-put-a-correct-name.${format}`;
if (metadata) {
// Fetch metadata for filename
const metadata = await getYouTubeMetadata(link);
const { title, uploader } = metadata;
const safeTitle = `${uploader} - ${title}`;
filename = `${safeTitle}.${format}`;
}
console.log(filename);
// Stream video/audio
return new Response(streamYouTube(link, format), {
headers: {
'Content-Type': format === 'mp3' ? 'audio/mpeg' : 'video/mp4',
'Content-Disposition': `attachment; filename="${filename}"`
'Content-Type': contentTypeFromFormat(format),
'Content-Disposition': `attachment; filename="${filename}"`,
'Set-Cookie': 'downloading=0'
}
});
} catch (err) {
console.error('Error fetching metadata:', err);
console.error(err)
console.error('Error fetching metadata:');
throw error(500, 'Failed to fetch video metadata');
}
};