Cache music as mp3 files on startup
This commit is contained in:
1 parent
c819436481
commit
33c9daa58b
11 files changed
+158
-15
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { getPlaylistTracks } from '#lib/server/music';
|
||||
import { isSongCached } from '#lib/server/musicCache';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const GET: RequestHandler = async () => {
|
||||
const tracks = await getPlaylistTracks();
|
||||
const tracks = (await getPlaylistTracks()).map((track) => ({ ...track, cached: isSongCached(track.id) }));
|
||||
return json(tracks, { headers: { 'Cache-Control': 'no-store' } });
|
||||
};
|
||||
@@ -1,10 +1,19 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { isPlaylistVideo, streamTrackAsMp3 } from '#lib/server/music';
|
||||
import { isSongCached, serveCachedSong } from '#lib/server/musicCache';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const GET: RequestHandler = ({ params, url }) => {
|
||||
export const GET: RequestHandler = async ({ params, url, request }) => {
|
||||
if (!isPlaylistVideo(params.id)) error(404, 'Song not found');
|
||||
|
||||
if (isSongCached(params.id)) {
|
||||
try {
|
||||
return await serveCachedSong(params.id, request.headers.get('range'));
|
||||
} catch (cacheError) {
|
||||
console.error(`Cached song unavailable for ${params.id}:`, cacheError);
|
||||
}
|
||||
}
|
||||
|
||||
const requestedStart = Number(url.searchParams.get('start') ?? 0);
|
||||
const startSeconds = Number.isFinite(requestedStart) && requestedStart > 0 ? Math.floor(requestedStart) : 0;
|
||||
|
||||
|
||||
Reference in new issue
Block a user