80 lines
1.5 KiB
TypeScript
80 lines
1.5 KiB
TypeScript
import { DOWNLOAD_PATH } from '$env/static/private';
|
|
import { redirect } from '@sveltejs/kit';
|
|
import youtubedl from 'youtube-dl-exec';
|
|
|
|
const isAudioFormat = (format: string) => {
|
|
let isAudio = false;
|
|
|
|
switch (format) {
|
|
case 'mp3':
|
|
isAudio = true;
|
|
break;
|
|
default:
|
|
isAudio = false;
|
|
break;
|
|
}
|
|
|
|
return isAudio;
|
|
};
|
|
|
|
const isVideoFormat = (format: string) => {
|
|
let isVideo = false;
|
|
|
|
switch (format) {
|
|
case 'mp4':
|
|
isVideo = true;
|
|
break;
|
|
default:
|
|
isVideo = false;
|
|
break;
|
|
}
|
|
|
|
return isVideo;
|
|
}
|
|
|
|
export const actions = {
|
|
download: async ({ request, cookies }) => {
|
|
const data = await request.formData();
|
|
|
|
const obj = {};
|
|
for (const element of data) {
|
|
obj[element[0]] = element[1];
|
|
}
|
|
|
|
const { format, source, link } = obj;
|
|
|
|
if (!(format && source && link)) {
|
|
throw redirect(307, '/');
|
|
}
|
|
|
|
console.info(`Asked ${source} download of ${link}`);
|
|
|
|
switch (source) {
|
|
case 'youtube':
|
|
const options = {
|
|
output: `${DOWNLOAD_PATH}/%(artist)s-%(title)s.%(ext)s`,
|
|
embedThumbnail: true
|
|
}
|
|
|
|
if (isAudioFormat(format)) {
|
|
options.extractAudio = true;
|
|
options.audioFormat = format;
|
|
}
|
|
|
|
isVideoFormat(format) ? options.format = format : ''
|
|
|
|
const output = await youtubedl(link);
|
|
|
|
console.log(output);
|
|
console.info(`Downloaded ${link} to ${output}`);
|
|
|
|
break;
|
|
case 'spotify':
|
|
break;
|
|
default:
|
|
console.error('ops');
|
|
}
|
|
|
|
return { success: true };
|
|
}
|
|
};
|