mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-17 00:59:29 -07:00
add client-side generation for playlist.json for static builds
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { json } from './utils/fetch.mjs';
|
||||
import { json, text } from './utils/fetch.mjs';
|
||||
import Setting from './utils/setting.mjs';
|
||||
|
||||
let playlist;
|
||||
@@ -19,18 +19,38 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
getMedia();
|
||||
});
|
||||
|
||||
const scanMusicDirectory = async () => {
|
||||
const parseDirectory = async (path, prefix = "") => {
|
||||
const listing = await text(path);
|
||||
const matches = [...listing.matchAll(/href="([^\"]+\.mp3)"/gi)];
|
||||
return matches.map((m) => `${prefix}${m[1]}`);
|
||||
};
|
||||
|
||||
try {
|
||||
let files = await parseDirectory("music/");
|
||||
if (files.length === 0) {
|
||||
files = await parseDirectory("music/default/", "default/");
|
||||
}
|
||||
return { availableFiles: files };
|
||||
} catch (e) {
|
||||
console.error("Unable to scan music directory");
|
||||
console.error(e);
|
||||
return { availableFiles: [] };
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const getMedia = async () => {
|
||||
try {
|
||||
// fetch the playlist
|
||||
const rawPlaylist = await json('playlist.json');
|
||||
// store the playlist
|
||||
playlist = rawPlaylist;
|
||||
// enable the media player
|
||||
enableMediaPlayer();
|
||||
} catch (e) {
|
||||
console.error("Couldn't get playlist");
|
||||
console.error(e);
|
||||
}
|
||||
try {
|
||||
// fetch the playlist
|
||||
const rawPlaylist = await json('playlist.json');
|
||||
playlist = rawPlaylist;
|
||||
} catch (e) {
|
||||
console.warn("Couldn't get playlist.json, falling back to directory scan");
|
||||
playlist = await scanMusicDirectory();
|
||||
}
|
||||
|
||||
enableMediaPlayer();
|
||||
};
|
||||
|
||||
const enableMediaPlayer = () => {
|
||||
|
||||
Reference in New Issue
Block a user