Spaces:
Sleeping
Sleeping
File size: 18,404 Bytes
3c56493 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | import { losslessAPI } from '$lib/api';
import type { Album, Track, AudioQuality } from '$lib/types';
import type { DownloadMode } from '$lib/stores/downloadPreferences';
import { formatArtists } from '$lib/utils';
import JSZip from 'jszip';
function detectImageFormat(data: Uint8Array): { extension: string; mimeType: string } | null {
if (!data || data.length < 4) {
return null;
}
// Check for JPEG magic bytes (FF D8 FF)
if (data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) {
return { extension: 'jpg', mimeType: 'image/jpeg' };
}
// Check for PNG magic bytes (89 50 4E 47)
if (data[0] === 0x89 && data[1] === 0x50 && data[2] === 0x4e && data[3] === 0x47) {
return { extension: 'png', mimeType: 'image/png' };
}
// Check for WebP magic bytes (52 49 46 46 ... 57 45 42 50)
if (data.length >= 12 &&
data[0] === 0x52 && data[1] === 0x49 && data[2] === 0x46 && data[3] === 0x46 &&
data[8] === 0x57 && data[9] === 0x45 && data[10] === 0x42 && data[11] === 0x50) {
return { extension: 'webp', mimeType: 'image/webp' };
}
return null;
}
export function sanitizeForFilename(value: string | null | undefined): string {
if (!value) return 'Unknown';
return value
.replace(/[\\/:*?"<>|]/g, '_')
.replace(/\s+/g, ' ')
.trim();
}
export function getExtensionForQuality(quality: AudioQuality, convertAacToMp3 = false): string {
switch (quality) {
case 'LOW':
case 'HIGH':
return convertAacToMp3 ? 'mp3' : 'm4a';
default:
return 'flac';
}
}
export function buildTrackFilename(
album: Album,
track: Track,
quality: AudioQuality,
artistName?: string,
convertAacToMp3 = false
): string {
const extension = getExtensionForQuality(quality, convertAacToMp3);
const volumeNumber = Number(track.volumeNumber);
const trackNumber = Number(track.trackNumber);
// Check if this is a multi-volume album by checking:
// 1. numberOfVolumes > 1, or
// 2. volumeNumber is set and finite (indicating multi-volume structure)
const isMultiVolume = (album.numberOfVolumes && album.numberOfVolumes > 1) ||
Number.isFinite(volumeNumber);
let trackPart: string;
if (isMultiVolume) {
const volumePadded = Number.isFinite(volumeNumber) && volumeNumber > 0 ? `${volumeNumber}`.padStart(2, '0') : '01';
const trackPadded = Number.isFinite(trackNumber) && trackNumber > 0 ? `${trackNumber}`.padStart(2, '0') : '00';
trackPart = `${volumePadded}-${trackPadded}`;
} else {
const trackPadded = Number.isFinite(trackNumber) && trackNumber > 0 ? `${trackNumber}`.padStart(2, '0') : '00';
trackPart = trackPadded;
}
let title = track.title;
if (track.version) {
title = `${title} (${track.version})`;
}
const parts = [
sanitizeForFilename(artistName ?? formatArtists(track.artists)),
sanitizeForFilename(album.title ?? 'Unknown Album'),
`${trackPart} ${sanitizeForFilename(title)}`
];
return `${parts.join(' - ')}.${extension}`;
}
export interface AlbumDownloadCallbacks {
onTotalResolved?(total: number): void;
onTrackDownloaded?(completed: number, total: number, track: Track): void;
onTrackFailed?(track: Track, error: Error, attempt: number): void;
}
function escapeCsvValue(value: string): string {
const normalized = value.replace(/\r?\n|\r/g, ' ');
if (/[",]/.test(normalized)) {
return `"${normalized.replace(/"/g, '""')}"`;
}
return normalized;
}
export async function buildTrackLinksCsv(tracks: Track[], quality: AudioQuality): Promise<string> {
const header = ['Index', 'Title', 'Artist', 'Album', 'Duration', 'FLAC URL'];
const rows: string[][] = [];
for (const [index, track] of tracks.entries()) {
const streamUrl = await losslessAPI.getTrackStreamUrl(track.id, quality);
rows.push([
`${index + 1}`,
track.title ?? '',
formatArtists(track.artists),
track.album?.title ?? '',
losslessAPI.formatDuration(track.duration ?? 0),
streamUrl
]);
}
return [header, ...rows]
.map((row) => row.map((value) => escapeCsvValue(String(value ?? ''))).join(','))
.join('\n');
}
interface DownloadTrackResult {
success: boolean;
blob?: Blob;
error?: Error;
}
async function downloadTrackWithRetry(
trackId: number,
quality: AudioQuality,
filename: string,
track: Track,
callbacks?: AlbumDownloadCallbacks,
options?: { convertAacToMp3?: boolean; downloadCoverSeperately?: boolean }
): Promise<DownloadTrackResult> {
const maxAttempts = 3;
const baseDelay = 1000; // 1 second
const trackTitle = track.title ?? 'Unknown Track';
const artistName = formatArtists(track.artists);
console.log(`[Track Download] Starting download: "${trackTitle}" by ${artistName} (ID: ${trackId}, Quality: ${quality})`);
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
if (attempt > 1) {
console.log(`[Track Download] Retry attempt ${attempt}/${maxAttempts} for "${trackTitle}"`);
}
const { blob } = await losslessAPI.fetchTrackBlob(trackId, quality, filename, {
ffmpegAutoTriggered: false,
convertAacToMp3: options?.convertAacToMp3
});
console.log(`[Track Download] β Success: "${trackTitle}" (${(blob.size / 1024 / 1024).toFixed(2)} MB)${attempt > 1 ? ` - succeeded on attempt ${attempt}` : ''}`);
return { success: true, blob };
} catch (error) {
const errorObj = error instanceof Error ? error : new Error(String(error));
console.warn(
`[Track Download] β Attempt ${attempt}/${maxAttempts} failed for "${trackTitle}": ${errorObj.message}`
);
callbacks?.onTrackFailed?.(track, errorObj, attempt);
if (attempt < maxAttempts) {
// Exponential backoff: 1s, 2s, 4s
const delay = baseDelay * Math.pow(2, attempt - 1);
console.log(`[Track Download] Waiting ${delay}ms before retry...`);
await new Promise((resolve) => setTimeout(resolve, delay));
} else {
console.error(
`[Track Download] βββ All ${maxAttempts} attempts failed for "${trackTitle}" - giving up`
);
return { success: false, error: errorObj };
}
}
}
return { success: false, error: new Error('Download failed after all retry attempts') };
}
function triggerFileDownload(blob: Blob, filename: string): void {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
export async function downloadAlbum(
album: Album,
quality: AudioQuality,
callbacks?: AlbumDownloadCallbacks,
preferredArtistName?: string,
options?: { mode?: DownloadMode; convertAacToMp3?: boolean; downloadCoverSeperately?: boolean }
): Promise<void> {
const { album: fetchedAlbum, tracks } = await losslessAPI.getAlbum(album.id);
const canonicalAlbum = fetchedAlbum ?? album;
const total = tracks.length;
callbacks?.onTotalResolved?.(total);
const mode = options?.mode ?? 'individual';
const shouldZip = mode === 'zip' && total > 1;
const useCsv = mode === 'csv';
const convertAacToMp3 = options?.convertAacToMp3 ?? false;
const downloadCoverSeperately = options?.downloadCoverSeperately ?? false;
const artistName = sanitizeForFilename(
preferredArtistName ?? canonicalAlbum.artist?.name ?? 'Unknown Artist'
);
const albumTitle = sanitizeForFilename(canonicalAlbum.title ?? 'Unknown Album');
console.log(`[Album Download] ββββββββββββββββββββββββββββββββββββββββββββββββ`);
console.log(`[Album Download] Starting: "${albumTitle}" by ${artistName}`);
console.log(`[Album Download] Tracks: ${total} | Quality: ${quality} | Mode: ${mode}`);
console.log(`[Album Download] ββββββββββββββββββββββββββββββββββββββββββββββββ`);
if (useCsv) {
let completed = 0;
for (const track of tracks) {
completed += 1;
callbacks?.onTrackDownloaded?.(completed, total, track);
}
const csvContent = await buildTrackLinksCsv(tracks, quality);
const csvBlob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
triggerFileDownload(csvBlob, `${artistName} - ${albumTitle}.csv`);
return;
}
if (shouldZip) {
const zip = new JSZip();
let completed = 0;
const failedTracks: Array<{ track: Track; error: Error }> = [];
// Download cover separately for ZIP if requested
if (downloadCoverSeperately && canonicalAlbum.cover) {
try {
console.log('[ZIP Cover Download] Fetching cover for album...');
// Try multiple sizes as fallback
const coverSizes: Array<'1280' | '640' | '320'> = ['1280', '640', '320'];
let coverDownloadSuccess = false;
for (const size of coverSizes) {
if (coverDownloadSuccess) break;
const coverUrl = losslessAPI.getCoverUrl(canonicalAlbum.cover, size);
console.log(`[ZIP Cover Download] Attempting size ${size}:`, coverUrl);
// Try two fetch strategies: with headers, then without
const fetchStrategies = [
{
name: 'with-headers',
options: {
method: 'GET' as const,
headers: {
'Accept': 'image/jpeg,image/jpg,image/png,image/*',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
},
signal: AbortSignal.timeout(10000)
}
},
{
name: 'simple',
options: {
method: 'GET' as const,
signal: AbortSignal.timeout(10000)
}
}
];
for (const strategy of fetchStrategies) {
if (coverDownloadSuccess) break;
console.log(`[ZIP Cover Download] Trying strategy: ${strategy.name}`);
try {
const coverResponse = await fetch(coverUrl, strategy.options);
console.log(`[ZIP Cover Download] Response status: ${coverResponse.status}, Content-Length: ${coverResponse.headers.get('Content-Length')}`);
if (!coverResponse.ok) {
console.warn(`[ZIP Cover Download] Failed with status ${coverResponse.status} for size ${size}`);
continue;
}
const contentType = coverResponse.headers.get('Content-Type');
const contentLength = coverResponse.headers.get('Content-Length');
if (contentLength && parseInt(contentLength, 10) === 0) {
console.warn(`[ZIP Cover Download] Content-Length is 0 for size ${size}`);
continue;
}
if (contentType && !contentType.startsWith('image/')) {
console.warn(`[ZIP Cover Download] Invalid content type: ${contentType}`);
continue;
}
// Use arrayBuffer directly for more reliable data retrieval
const arrayBuffer = await coverResponse.arrayBuffer();
if (!arrayBuffer || arrayBuffer.byteLength === 0) {
console.warn(`[ZIP Cover Download] Empty array buffer for size ${size}`);
continue;
}
const uint8Array = new Uint8Array(arrayBuffer);
console.log(`[ZIP Cover Download] Received ${uint8Array.length} bytes`);
// Detect image format
const imageFormat = detectImageFormat(uint8Array);
if (!imageFormat) {
console.warn(`[ZIP Cover Download] Unknown image format for size ${size}`);
continue;
}
// Add cover to ZIP with appropriate filename
const coverFilename = `cover.${imageFormat.extension}`;
zip.file(coverFilename, uint8Array, {
binary: true,
compression: 'DEFLATE',
compressionOptions: { level: 6 }
});
coverDownloadSuccess = true;
console.log(`[ZIP Cover Download] Successfully added cover to ZIP (${size}x${size}, format: ${imageFormat.extension}, strategy: ${strategy.name})`);
break;
} catch (sizeError) {
console.warn(`[ZIP Cover Download] Failed at size ${size} with strategy ${strategy.name}:`, sizeError);
}
} // End strategy loop
} // End size loop
if (!coverDownloadSuccess) {
console.warn('[ZIP Cover Download] All attempts failed');
}
} catch (coverError) {
console.warn('Failed to download cover for ZIP:', coverError);
}
}
for (const track of tracks) {
const filename = buildTrackFilename(
canonicalAlbum,
track,
quality,
preferredArtistName,
convertAacToMp3
);
const result = await downloadTrackWithRetry(
track.id,
quality,
filename,
track,
callbacks,
{ convertAacToMp3 }
);
if (result.success && result.blob) {
zip.file(filename, result.blob);
} else {
console.error(`[ZIP Download] Track failed: ${track.title}`, result.error);
failedTracks.push({ track, error: result.error ?? new Error('Unknown error') });
}
completed += 1;
callbacks?.onTrackDownloaded?.(completed, total, track);
}
// Add error report file if there were failures
if (failedTracks.length > 0) {
let errorReport = 'DOWNLOAD ERRORS\n';
errorReport += '===============\n\n';
errorReport += 'The following tracks failed to download after 3 attempts:\n\n';
failedTracks.forEach((item, index) => {
const { track, error } = item;
const trackTitle = track.title ?? 'Unknown Track';
const artistName = formatArtists(track.artists);
errorReport += `${index + 1}. ${trackTitle} - ${artistName}\n`;
errorReport += ` Error: ${error.message}\n\n`;
});
zip.file('_DOWNLOAD_ERRORS.txt', errorReport);
console.log(`[ZIP Download] Added error report with ${failedTracks.length} failed track(s)`);
}
const zipBlob = await zip.generateAsync({
type: 'blob',
compression: 'DEFLATE',
compressionOptions: { level: 6 }
});
const successCount = completed - failedTracks.length;
console.log(`[Album Download] ββββββββββββββββββββββββββββββββββββββββββββββββ`);
console.log(`[Album Download] ZIP Complete: "${albumTitle}"`);
console.log(`[Album Download] β Success: ${successCount}/${total} tracks | ZIP size: ${(zipBlob.size / 1024 / 1024).toFixed(2)} MB`);
if (failedTracks.length > 0) {
console.log(`[Album Download] β Failed: ${failedTracks.length} track(s) - see _DOWNLOAD_ERRORS.txt in ZIP`);
}
console.log(`[Album Download] ββββββββββββββββββββββββββββββββββββββββββββββββ`);
triggerFileDownload(zipBlob, `${artistName} - ${albumTitle}.zip`);
return;
}
let completed = 0;
let failedCount = 0;
for (const track of tracks) {
const filename = buildTrackFilename(
canonicalAlbum,
track,
quality,
preferredArtistName,
convertAacToMp3
);
const result = await downloadTrackWithRetry(
track.id,
quality,
filename,
track,
callbacks,
{ convertAacToMp3, downloadCoverSeperately }
);
if (result.success && result.blob) {
// Trigger individual download
const url = URL.createObjectURL(result.blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
// Download cover separately if enabled
if (downloadCoverSeperately && track.album?.cover) {
try {
const coverId = track.album.cover;
const coverSizes: Array<'1280' | '640' | '320'> = ['1280', '640', '320'];
let coverDownloadSuccess = false;
for (const size of coverSizes) {
if (coverDownloadSuccess) break;
const coverUrl = losslessAPI.getCoverUrl(coverId, size);
const fetchStrategies = [
{
name: 'with-headers',
options: {
method: 'GET' as const,
headers: {
'Accept': 'image/jpeg,image/jpg,image/png,image/*',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
},
signal: AbortSignal.timeout(10000)
}
},
{
name: 'simple',
options: {
method: 'GET' as const,
signal: AbortSignal.timeout(10000)
}
}
];
for (const strategy of fetchStrategies) {
if (coverDownloadSuccess) break;
try {
const coverResponse = await fetch(coverUrl, strategy.options);
if (!coverResponse.ok) continue;
const contentType = coverResponse.headers.get('Content-Type');
const contentLength = coverResponse.headers.get('Content-Length');
if (contentLength && parseInt(contentLength, 10) === 0) continue;
if (contentType && !contentType.startsWith('image/')) continue;
const arrayBuffer = await coverResponse.arrayBuffer();
if (!arrayBuffer || arrayBuffer.byteLength === 0) continue;
const uint8Array = new Uint8Array(arrayBuffer);
const imageFormat = detectImageFormat(uint8Array);
if (!imageFormat) continue;
const coverBlob = new Blob([uint8Array], { type: imageFormat.mimeType });
const coverObjectUrl = URL.createObjectURL(coverBlob);
const coverLink = document.createElement('a');
coverLink.href = coverObjectUrl;
coverLink.download = `cover.${imageFormat.extension}`;
document.body.appendChild(coverLink);
coverLink.click();
document.body.removeChild(coverLink);
URL.revokeObjectURL(coverObjectUrl);
coverDownloadSuccess = true;
break;
} catch {
// Continue to next strategy
}
}
}
} catch (coverError) {
console.warn('Failed to download cover separately:', coverError);
}
}
} else {
console.error(`[Individual Download] Track failed: ${track.title}`, result.error);
failedCount++;
}
completed += 1;
callbacks?.onTrackDownloaded?.(completed, total, track);
}
// Summary logging
const successCount = total - failedCount;
console.log(`[Album Download] ββββββββββββββββββββββββββββββββββββββββββββββββ`);
console.log(`[Album Download] Individual Downloads Complete: "${albumTitle}"`);
console.log(`[Album Download] β Success: ${successCount}/${total} tracks`);
if (failedCount > 0) {
console.log(`[Album Download] β Failed: ${failedCount} track(s)`);
}
console.log(`[Album Download] ββββββββββββββββββββββββββββββββββββββββββββββββ`);
}
|