better code
All checks were successful
Bump deps (only minor versions) / ci (push) Successful in 16s
All checks were successful
Bump deps (only minor versions) / ci (push) Successful in 16s
This commit is contained in:
parent
b16b84e5a3
commit
6b8779f527
2 changed files with 37 additions and 7 deletions
|
@ -1,7 +1,15 @@
|
|||
import formats from '$lib/common/supportedFormats.json';
|
||||
|
||||
const formatMime = new Map(Object.entries(formats))
|
||||
export const isURLValid = (url: string) => {
|
||||
try {
|
||||
new URL(url)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
export const mimeTypeMap = formatMime;
|
||||
export const contentTypeFromFormat = (format: string): string => {
|
||||
const toReturn: string | undefined = formatMime.get(format)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { error } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getYouTubeMetadata, streamYouTube, ytdl } from '$lib/server/ytdlp';
|
||||
import { contentTypeFromFormat, mimeTypeMap } from '$lib/server/helpers';
|
||||
import { contentTypeFromFormat, isURLValid, mimeTypeMap } from '$lib/server/helpers';
|
||||
|
||||
export const GET: RequestHandler = async ({ url }) => {
|
||||
const validateRequest = (url) => {
|
||||
// Get query params
|
||||
const link = url.searchParams.get('link');
|
||||
const format = url.searchParams.get('format'); // mp3, mp4
|
||||
|
@ -19,16 +19,37 @@ export const GET: RequestHandler = async ({ url }) => {
|
|||
throw error(400, 'Currently, only YouTube is supported');
|
||||
}
|
||||
|
||||
try {
|
||||
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), {
|
||||
|
@ -38,9 +59,10 @@ export const GET: RequestHandler = async ({ url }) => {
|
|||
'Set-Cookie': 'downloading=0'
|
||||
}
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
console.error('Error fetching metadata:');
|
||||
throw error(500, 'Failed to fetch video metadata');
|
||||
console.error('Filed to stream file');
|
||||
throw error(500, 'Failed to stream file');
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue