it's cool that we have format
All checks were successful
Bump deps (only minor versions) / ci (push) Successful in 18s
All checks were successful
Bump deps (only minor versions) / ci (push) Successful in 18s
This commit is contained in:
parent
760812c692
commit
652208aa57
6 changed files with 69 additions and 60 deletions
|
@ -1,7 +1,7 @@
|
|||
import { logger } from "$lib/server/helpers";
|
||||
import { logger } from '$lib/server/helpers';
|
||||
|
||||
export async function handle({ event, resolve }) {
|
||||
logger.info(`Received ${event.request.method} request: ${event.url}`);
|
||||
|
||||
return await resolve(event);;
|
||||
}
|
||||
|
||||
return await resolve(event);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"mp3": "audio/mpeg",
|
||||
"opus": "audio/ogg",
|
||||
"wav": "audio/wav",
|
||||
"mp4": "video/mp4"
|
||||
}
|
||||
"mp3": "audio/mpeg",
|
||||
"opus": "audio/ogg",
|
||||
"wav": "audio/wav",
|
||||
"mp4": "video/mp4"
|
||||
}
|
||||
|
|
|
@ -2,18 +2,18 @@ import formats from '$lib/common/supportedFormats.json';
|
|||
import winston from 'winston';
|
||||
|
||||
export const logger = winston.createLogger({
|
||||
level: 'debug',
|
||||
format: winston.format.json(),
|
||||
transports: [new winston.transports.Console()],
|
||||
level: 'debug',
|
||||
format: winston.format.json(),
|
||||
transports: [new winston.transports.Console()]
|
||||
});
|
||||
const formatMime = new Map(Object.entries(formats))
|
||||
const formatMime = new Map(Object.entries(formats));
|
||||
export const isURLValid = (url: string) => {
|
||||
try {
|
||||
new URL(url)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
try {
|
||||
new URL(url);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
export const mimeTypeMap = formatMime;
|
||||
return true;
|
||||
};
|
||||
export const mimeTypeMap = formatMime;
|
||||
|
|
|
@ -25,45 +25,46 @@ export async function getYouTubeMetadata(link: string) {
|
|||
*/
|
||||
export function streamYouTube(link: string, format: string): ReadableStream<Uint8Array> {
|
||||
logger.debug(`Starting to stream: ${link}`);
|
||||
const mimeType: string | undefined = mimeTypeMap.get(format)
|
||||
const mimeType: string | undefined = mimeTypeMap.get(format);
|
||||
|
||||
if (!mimeType) {
|
||||
throw new Error("Unsupported format");
|
||||
throw new Error('Unsupported format');
|
||||
}
|
||||
logger.debug(`Given format is compatible: ${mimeType}`);
|
||||
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
const args = [
|
||||
'--no-write-thumbnail',
|
||||
'-o',
|
||||
'-',
|
||||
].filter(Boolean);
|
||||
const args = ['--no-write-thumbnail', '-o', '-'].filter(Boolean);
|
||||
|
||||
if (mimeType?.includes('audio')) {
|
||||
args.push(...['--extract-audio', '--embed-metadata', '--embed-thumbnail', '--audio-format', format])
|
||||
args.push(
|
||||
...['--extract-audio', '--embed-metadata', '--embed-thumbnail', '--audio-format', format]
|
||||
);
|
||||
} else if (mimeType.includes('video')) {
|
||||
args.push(...['--embed-metadata', '--embed-thumbnail', '--format', format])
|
||||
args.push(...['--embed-metadata', '--embed-thumbnail', '--format', format]);
|
||||
}
|
||||
|
||||
const cmd = `${YTDLP_PATH} ${args.join(' ')} ${link}`
|
||||
const cmd = `${YTDLP_PATH} ${args.join(' ')} ${link}`;
|
||||
logger.debug(`Running: ${cmd}`);
|
||||
|
||||
const process = spawn(YTDLP_PATH, [...args, link], { cwd: "/tmp", stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
const process = spawn(YTDLP_PATH, [...args, link], {
|
||||
cwd: '/tmp',
|
||||
stdio: ['ignore', 'pipe', 'pipe']
|
||||
});
|
||||
|
||||
process.stdout.on('data', (chunk) => {
|
||||
try {
|
||||
controller.enqueue(chunk)
|
||||
controller.enqueue(chunk);
|
||||
} catch (ex) {
|
||||
process.kill()
|
||||
process.kill();
|
||||
}
|
||||
});
|
||||
process.stderr.on('data', (chunk) => logger.debug(chunk.toString()));
|
||||
process.stdout.on('end', () => {
|
||||
try {
|
||||
controller.close()
|
||||
controller.close();
|
||||
} catch (ex) {
|
||||
logger.error(ex)
|
||||
logger.error(ex);
|
||||
}
|
||||
});
|
||||
process.stdout.on('error', (err) => {
|
||||
|
|
|
@ -24,21 +24,21 @@
|
|||
};
|
||||
|
||||
onMount(() => {
|
||||
document.cookie = 'downloading=0'
|
||||
document.cookie = 'downloading=0';
|
||||
});
|
||||
|
||||
const readLogs = () => {
|
||||
logId = setInterval(() => {
|
||||
logs += "We're downloading <br>"
|
||||
}, 2000)
|
||||
}
|
||||
logs += "We're downloading <br>";
|
||||
}, 2000);
|
||||
};
|
||||
const onClick = () => {
|
||||
let checkIterations = 0;
|
||||
link = '';
|
||||
downloading = true;
|
||||
document.cookie = 'downloading=1'
|
||||
|
||||
readLogs()
|
||||
document.cookie = 'downloading=1';
|
||||
|
||||
readLogs();
|
||||
|
||||
const id = setInterval(() => {
|
||||
if (document.cookie.includes('downloading=0') || checkIterations > 3) {
|
||||
|
@ -72,8 +72,7 @@
|
|||
searchParams.append('link', link);
|
||||
searchParams.append('format', format);
|
||||
|
||||
if (metadata)
|
||||
searchParams.append('metadata', "1");
|
||||
if (metadata) searchParams.append('metadata', '1');
|
||||
|
||||
href = `/download?${searchParams.toString()}`;
|
||||
};
|
||||
|
@ -88,12 +87,18 @@
|
|||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div
|
||||
id="wrapper"
|
||||
class="relative mx-auto rounded-lg bg-black p-6 text-[#00ff7f] shadow-lg sm:max-w-sm sm:border-4 sm:border-[#00ff7f] md:mt-10 md:max-w-md lg:max-w-lg 2xl:max-w-2xl"
|
||||
>
|
||||
|
||||
<div id="loader" class={["backdrop-blur-xs bg-black/20 absolute inset-0 z-10 flex items-center justify-center", { downloading }]}>
|
||||
<div
|
||||
id="loader"
|
||||
class={[
|
||||
'absolute inset-0 z-10 flex items-center justify-center bg-black/20 backdrop-blur-xs',
|
||||
{ downloading }
|
||||
]}
|
||||
>
|
||||
<Loader />
|
||||
{@html logs}
|
||||
</div>
|
||||
|
@ -182,7 +187,9 @@
|
|||
|
||||
<!-- Metadata -->
|
||||
<div>
|
||||
<label for="metadata" class="mb-2 block text-[#00e5ff]">Set filename (<span class='text-red-700'>SLOW</span>)</label>
|
||||
<label for="metadata" class="mb-2 block text-[#00e5ff]"
|
||||
>Set filename (<span class="text-red-700">SLOW</span>)</label
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="metadata"
|
||||
|
@ -218,7 +225,7 @@
|
|||
source, paste the link, and select a format to start downloading!
|
||||
</p>
|
||||
<span class="mt-10 block">
|
||||
<a class="underline text-[#00e5ff]" href="https://git.pweapon.org/odo/dl.emersa.it"
|
||||
<a class="text-[#00e5ff] underline" href="https://git.pweapon.org/odo/dl.emersa.it"
|
||||
>Click here for the source code</a
|
||||
>
|
||||
</span>
|
||||
|
@ -237,10 +244,10 @@
|
|||
font-family: 'Press Start 2P', cursive;
|
||||
}
|
||||
|
||||
@media (max-height: 1000px) {
|
||||
@media (max-height: 1000px) {
|
||||
* {
|
||||
font-size: 10px;
|
||||
}
|
||||
}
|
||||
}
|
||||
@media (min-width: 1024px) {
|
||||
* {
|
||||
|
@ -248,14 +255,13 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#loader {
|
||||
display: none;
|
||||
}
|
||||
#loader.downloading {
|
||||
display: grid;
|
||||
justify-items: center;
|
||||
align-items: center;
|
||||
align-items: center;
|
||||
}
|
||||
.not-available {
|
||||
text-decoration-line: line-through;
|
||||
|
|
|
@ -30,9 +30,12 @@ const validateRequest = (url: URL) => {
|
|||
logger.debug(`Request is valid`);
|
||||
|
||||
return {
|
||||
link, format, source, metadata
|
||||
}
|
||||
}
|
||||
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}`;
|
||||
|
@ -46,7 +49,7 @@ export const GET: RequestHandler = async ({ url }) => {
|
|||
const safeTitle = `${uploader} - ${title}`;
|
||||
filename = `${safeTitle}.${format}`;
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
logger.error(err);
|
||||
logger.error('Error fetching metadata:');
|
||||
throw error(500, 'Failed to fetch video metadata');
|
||||
}
|
||||
|
@ -56,14 +59,13 @@ export const GET: RequestHandler = async ({ url }) => {
|
|||
// Stream video/audio
|
||||
return new Response(streamYouTube(link, format), {
|
||||
headers: {
|
||||
'Content-Type': "text/event-stream",
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Content-Disposition': `attachment; filename="${filename}"`,
|
||||
'Set-Cookie': 'downloading=0'
|
||||
}
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
logger.error(err);
|
||||
logger.error('Filed to stream file');
|
||||
throw error(500, 'Failed to stream file');
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue