import { error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { getYouTubeMetadata, streamYouTube, ytdl } from '$lib/server/ytdlp'; import { contentTypeFromFormat, isURLValid, mimeTypeMap } from '$lib/server/helpers'; const validateRequest = (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) { throw error(400, 'Missing required query parameters: link, format, or source'); } if (source !== 'youtube') { throw error(400, 'Currently, only YouTube is supported'); } if (!isURLValid(link)) { throw error(400, 'URL not valid'); } if (!mimeTypeMap.get(format)) { throw error(400, 'format not valid'); } return { link, format, source, metadata } } export const GET: RequestHandler = async ({ url }) => { const { format, source, metadata, link } = validateRequest(url) let filename = `you-clicked-no-metadata-so-i-cant-put-a-correct-name.${format}`; if (metadata) { try { // Fetch metadata for filename const metadata = await getYouTubeMetadata(link); const { title, uploader } = metadata; const safeTitle = `${uploader} - ${title}`; filename = `${safeTitle}.${format}`; } catch (err) { console.error(err) console.error('Error fetching metadata:'); throw error(500, 'Failed to fetch video metadata'); } } try { // Stream video/audio return new Response(streamYouTube(link, format), { headers: { 'Content-Type': contentTypeFromFormat(format), 'Content-Disposition': `attachment; filename="${filename}"`, 'Set-Cookie': 'downloading=0' } }); } catch (err) { console.error(err) console.error('Filed to stream file'); throw error(500, 'Failed to stream file'); } };