From c79ecbb92e79e26ad0ee055ae31a1369d100a32f Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 26 Jan 2025 14:22:35 -0600 Subject: [PATCH] Fix crash downloading with old server, check server version to append token on image requests #1450 --- .../audiobookshelf/app/models/DownloadItemPart.kt | 8 ++++---- components/covers/AuthorImage.vue | 8 ++++++-- store/globals.js | 11 ++++++++++- store/index.js | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/java/com/audiobookshelf/app/models/DownloadItemPart.kt b/android/app/src/main/java/com/audiobookshelf/app/models/DownloadItemPart.kt index f5a71b6c..814b8544 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/models/DownloadItemPart.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/models/DownloadItemPart.kt @@ -41,10 +41,10 @@ data class DownloadItemPart( val destinationUri = Uri.fromFile(destinationFile) val finalDestinationUri = Uri.fromFile(finalDestinationFile) - var downloadUrl = "${DeviceManager.serverAddress}${serverPath}" - - downloadUrl += if (serverPath.endsWith("/cover")) "?raw=1" // Download raw cover image - else "?token=${DeviceManager.token}" + var downloadUrl = "${DeviceManager.serverAddress}${serverPath}?token=${DeviceManager.token}" + if (serverPath.endsWith("/cover")) { + downloadUrl += "&raw=1" // Download raw cover image + } val downloadUri = Uri.parse(downloadUrl) Log.d("DownloadItemPart", "Audio File Destination Uri: $destinationUri | Final Destination Uri: $finalDestinationUri | Download URI $downloadUri") diff --git a/components/covers/AuthorImage.vue b/components/covers/AuthorImage.vue index 0d1fea6e..d1b0f057 100644 --- a/components/covers/AuthorImage.vue +++ b/components/covers/AuthorImage.vue @@ -56,11 +56,15 @@ export default { }, imgSrc() { if (!this.imagePath || !this.serverAddress) return null + const urlQuery = new URLSearchParams({ ts: this.updatedAt }) + if (this.$store.getters.getDoesServerImagesRequireToken) { + urlQuery.append('token', this.$store.getters['user/getToken']) + } if (process.env.NODE_ENV !== 'production' && this.serverAddress.startsWith('http://192.168')) { // Testing - return `http://localhost:3333/api/authors/${this.authorId}/image?ts=${this.updatedAt}` + return `http://localhost:3333/api/authors/${this.authorId}/image?${urlQuery.toString()}` } - return `${this.serverAddress}/api/authors/${this.authorId}/image?ts=${this.updatedAt}` + return `${this.serverAddress}/api/authors/${this.authorId}/image?${urlQuery.toString()}` } }, methods: { diff --git a/store/globals.js b/store/globals.js index e4fbb37a..d33a693a 100644 --- a/store/globals.js +++ b/store/globals.js @@ -74,7 +74,13 @@ export const getters = { } const url = new URL(`${serverAddress}/api/items/${libraryItem.id}/cover`) - return `${url}?ts=${lastUpdate}${raw ? '&raw=1' : ''}` + const urlQuery = new URLSearchParams() + urlQuery.append('ts', lastUpdate) + if (raw) urlQuery.append('raw', '1') + if (rootGetters.getDoesServerImagesRequireToken) { + urlQuery.append('token', rootGetters['user/getToken']) + } + return `${url}?${urlQuery}` }, getLibraryItemCoverSrcById: (state, getters, rootState, rootGetters) => @@ -85,6 +91,9 @@ export const getters = { if (!serverAddress) return placeholder const url = new URL(`${serverAddress}/api/items/${libraryItemId}/cover`) + if (rootGetters.getDoesServerImagesRequireToken) { + return `${url}?token=${rootGetters['user/getToken']}` + } return url.toString() }, getLocalMediaProgressById: diff --git a/store/index.js b/store/index.js index c641aa20..8844c0d8 100644 --- a/store/index.js +++ b/store/index.js @@ -85,6 +85,20 @@ export const getters = { getCanStreamingUsingCellular: (state) => { if (!state.deviceData?.deviceSettings?.streamingUsingCellular) return 'ALWAYS' return state.deviceData.deviceSettings.streamingUsingCellular || 'ALWAYS' + }, + /** + * Old server versions require a token for images + * + * @param {*} state + * @returns {boolean} True if server version is less than 2.17 + */ + getDoesServerImagesRequireToken: (state) => { + const serverVersion = state.serverSettings?.version + if (!serverVersion) return false + const versionParts = serverVersion.split('.') + const majorVersion = parseInt(versionParts[0]) + const minorVersion = parseInt(versionParts[1]) + return majorVersion < 2 || (majorVersion == 2 && minorVersion < 17) } }