[READ-ONLY] Mirror of https://github.com/probablykasper/ferrum. Music library app for Mac, Linux and Windows ferrum.kasper.space
electron linux macos music music-library music-player napi windows
0

Configure Feed

Select the types of activity you want to include in your feed.

Fix trackimg 404/error responses

Whenever we returned an error response from trackimg, the renderere showed a "scheme is not supported" error for some reason. Luckily, it works if we use the same scheme for everything

Kasper (Oct 8, 2024, 11:18 PM +0200) 3faee7cb 26c23f3b

+41 -38
+1 -1
src/components/Cover.svelte
··· 5 5 6 6 export let track: Track 7 7 $: src = 8 - 'trackimg:?path=' + 8 + 'app://trackimg?path=' + 9 9 encodeURIComponent(join_paths(paths.tracksDir, track.file)) + 10 10 '&cache_db_path=' + 11 11 encodeURIComponent(paths.cacheDb) +
+1 -1
src/components/QueueItem.svelte
··· 9 9 $: $tracks_updated, (track = get_track(id)) 10 10 11 11 $: src = 12 - 'trackimg:?path=' + 12 + 'app://trackimg?path=' + 13 13 encodeURIComponent(join_paths(paths.tracksDir, track.file)) + 14 14 '&cache_db_path=' + 15 15 encodeURIComponent(paths.cacheDb) +
+39 -36
src/electron/main.ts
··· 86 86 callback(path) 87 87 }) 88 88 89 - protocol.handle('trackimg', (request) => { 90 - return new Promise((resolve) => { 91 - const url_raw = new URL(request.url) 92 - const track_path = decodeURIComponent(url_raw.searchParams.get('path') ?? '') 93 - const cache_db_path = decodeURIComponent(url_raw.searchParams.get('cache_db_path') ?? '') 94 - 95 - addon 96 - .read_small_cover_async(track_path, 0, cache_db_path) 97 - .then((buffer) => { 98 - if (buffer === null) { 99 - resolve(new Response(null, { status: 404 })) 100 - } else { 101 - resolve(new Response(Buffer.from(buffer))) 102 - } 103 - }) 104 - .catch((error) => { 105 - resolve(new Response(error, { status: 500 })) 106 - console.log(`Could not read cover "${track_path}":`, error) 107 - }) 108 - }) 109 - }) 110 - 111 89 session.defaultSession.webRequest.onHeadersReceived((details, callback) => { 112 90 callback({ 113 91 responseHeaders: { ··· 123 101 } 124 102 125 103 const web_folder = path.join(path.dirname(__dirname), 'web') 104 + const origin = 'app://app' 126 105 127 106 protocol.handle('app', (request) => { 128 - const accepts_html = 129 - request.headers 130 - .get('accept') 131 - ?.split(',') 132 - .map((mime_type) => mime_type.trim()) 133 - .includes('text/html') ?? false 107 + const req_url = new URL(request.url) 108 + if (req_url.hostname === 'trackimg') { 109 + return new Promise((resolve) => { 110 + const track_path = decodeURIComponent(req_url.searchParams.get('path') ?? '') 111 + const cache_db_path = decodeURIComponent(req_url.searchParams.get('cache_db_path') ?? '') 112 + 113 + addon 114 + .read_small_cover_async(track_path, 0, cache_db_path) 115 + .then((buffer) => { 116 + if (buffer === null) { 117 + resolve(new Response(null, { status: 404 })) 118 + } else { 119 + resolve(new Response(Buffer.from(buffer))) 120 + } 121 + }) 122 + .catch((error) => { 123 + resolve(new Response(error, { status: 500 })) 124 + console.log(`Could not read cover "${track_path}":`, error) 125 + }) 126 + }) 127 + } else if (req_url.hostname === 'app') { 128 + const accepts_html = 129 + request.headers 130 + .get('accept') 131 + ?.split(',') 132 + .map((mime_type) => mime_type.trim()) 133 + .includes('text/html') ?? false 134 + 135 + if (request.method === 'GET' && accepts_html) { 136 + const html_path = url.pathToFileURL(path.join(web_folder, 'index.html')).toString() 137 + return net.fetch(html_path) 138 + } 134 139 135 - if (request.method === 'GET' && accepts_html) { 136 - const html_path = url.pathToFileURL(path.join(web_folder, 'index.html')).toString() 137 - return net.fetch(html_path) 140 + const file_path = path.join(web_folder, req_url.pathname) 141 + const file_url = url.pathToFileURL(file_path).toString() 142 + return net.fetch(file_url) 143 + } else { 144 + return new Response('Unknown protocol', { status: 400 }) 138 145 } 139 - 140 - const file_path = request.url.slice('app:'.length) 141 - const file_url = url.pathToFileURL(path.join(web_folder, file_path)).toString() 142 - return net.fetch(file_url) 143 146 }) 144 147 145 148 if (is.dev) { 146 149 main_window.loadURL(new URL(vite_dev_server_url).origin + '/playlist/root') 147 150 } else { 148 - main_window.loadURL('app:/playlist/root') 151 + main_window.loadURL(`${origin}/playlist/root`) 149 152 } 150 153 151 154 if (is.dev) main_window.webContents.openDevTools()