Big updates
This commit is contained in:
parent
ea0e18f9d7
commit
8223487c0b
8 changed files with 70 additions and 77 deletions
|
@ -6,17 +6,16 @@ import { json, error } from '@sveltejs/kit';
|
|||
import type { RequestHandler } from './$types';
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
|
||||
/**
|
||||
* Fetch YouTube metadata (title, uploader/artist)
|
||||
*/
|
||||
async function getYouTubeMetadata(link: string) {
|
||||
return await ytdl(link, {
|
||||
dumpSingleJson: true,
|
||||
noCheckCertificates: true,
|
||||
noWarnings: true,
|
||||
preferFreeFormats: true
|
||||
});
|
||||
return await ytdl(link, {
|
||||
dumpSingleJson: true,
|
||||
noCheckCertificates: true,
|
||||
noWarnings: true,
|
||||
preferFreeFormats: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,41 +50,41 @@ function streamYouTube(link: string, format: string): ReadableStream<Uint8Array>
|
|||
* Sanitize filename by removing unsafe characters
|
||||
*/
|
||||
function sanitizeFilename(name: string): string {
|
||||
return name.replace(/[\/:*?"<>|]/g, '').trim();
|
||||
return name.replace(/[\/:*?"<>|]/g, '').trim();
|
||||
}
|
||||
|
||||
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
|
||||
// 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
|
||||
|
||||
// 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');
|
||||
}
|
||||
// Validate input
|
||||
if (!link || !format || !source) {
|
||||
throw error(400, 'Missing required query parameters: link, format, or source');
|
||||
}
|
||||
|
||||
try {
|
||||
// Fetch metadata for filename
|
||||
const metadata = await getYouTubeMetadata(link);
|
||||
const { title, uploader } = metadata;
|
||||
const safeTitle = sanitizeFilename(`${uploader} - ${title}`);
|
||||
const filename = `${safeTitle}.${format}`;
|
||||
if (source !== 'youtube') {
|
||||
throw error(400, 'Currently, only YouTube is supported');
|
||||
}
|
||||
|
||||
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}"`
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error fetching metadata:', err);
|
||||
throw error(500, 'Failed to fetch video metadata');
|
||||
}
|
||||
try {
|
||||
// Fetch metadata for filename
|
||||
const metadata = await getYouTubeMetadata(link);
|
||||
const { title, uploader } = metadata;
|
||||
const safeTitle = sanitizeFilename(`${uploader} - ${title}`);
|
||||
const 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}"`
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error fetching metadata:', err);
|
||||
throw error(500, 'Failed to fetch video metadata');
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue