diff --git a/jam-ui/src/store/features/mediaSlice.js b/jam-ui/src/store/features/mediaSlice.js index 42835b20e..87b3acaab 100644 --- a/jam-ui/src/store/features/mediaSlice.js +++ b/jam-ui/src/store/features/mediaSlice.js @@ -87,31 +87,45 @@ export const downloadJamTrack = createAsyncThunk( throw new Error(`Mixdown "${mixdown.name}" has no packages available`); } - // Debug: Log package structure to understand field names - console.log('[JamTrack] Available packages for mixdown:', mixdown.name); - console.log('[JamTrack] Looking for: file_type=ogg, encrypt_type=jkz, sample_rate=', sampleRate); - mixdown.packages.forEach((pkg, idx) => { - console.log(`[JamTrack] Package ${idx}:`, { - id: pkg.id, - file_type: pkg.file_type, - fileType: pkg.fileType, - encrypt_type: pkg.encrypt_type, - encryptType: pkg.encryptType, - sample_rate: pkg.sample_rate, - sampleRate: pkg.sampleRate - }); - }); + // pickMyPackage logic with fallback strategy + // Preference order: + // 1. ogg + jkz encryption + matching sample rate (ideal) + // 2. ogg + any encryption + matching sample rate + // 3. any format + matching sample rate + // 4. any package (last resort) - // pickMyPackage logic: find compatible package (ogg, jkz, matching sample rate) - // Try both snake_case and camelCase field names - const compatiblePackage = mixdown.packages.find(pkg => - (pkg.file_type === 'ogg' || pkg.fileType === 'ogg') && - (pkg.encrypt_type === 'jkz' || pkg.encryptType === 'jkz') && - (pkg.sample_rate === sampleRate || pkg.sampleRate === sampleRate) + let compatiblePackage = null; + + // Try 1: Ideal package (ogg, jkz, matching sample rate) + compatiblePackage = mixdown.packages.find(pkg => + pkg.file_type === 'ogg' && + pkg.encrypt_type === 'jkz' && + pkg.sample_rate === sampleRate ); + // Try 2: ogg with any encryption, matching sample rate if (!compatiblePackage) { - throw new Error(`No compatible package found for sample rate ${sampleRate}kHz (mixdown: ${mixdown.name})`); + compatiblePackage = mixdown.packages.find(pkg => + pkg.file_type === 'ogg' && + pkg.sample_rate === sampleRate + ); + } + + // Try 3: Any format with matching sample rate + if (!compatiblePackage) { + compatiblePackage = mixdown.packages.find(pkg => + pkg.sample_rate === sampleRate + ); + } + + // Try 4: Just use first available package + if (!compatiblePackage) { + compatiblePackage = mixdown.packages[0]; + console.warn(`[JamTrack] No package matches sample rate ${sampleRate}kHz, using first available package (${compatiblePackage.file_type}, ${compatiblePackage.sample_rate}kHz)`); + } + + if (!compatiblePackage) { + throw new Error(`No packages available for mixdown "${mixdown.name}"`); } const packageId = compatiblePackage.id;