Skip to content

Commit

Permalink
Remove token from image requests & fix download raw cover image #1328
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Jan 18, 2025
1 parent d5fa36b commit 111e8d3
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ data class DownloadItemPart(
val destinationUri = Uri.fromFile(destinationFile)
val finalDestinationUri = Uri.fromFile(finalDestinationFile)

var downloadUrl = "${DeviceManager.serverAddress}${serverPath}?token=${DeviceManager.token}"
if (serverPath.endsWith("/cover")) downloadUrl += "&format=jpeg&raw=1" // For cover images force to jpeg
var downloadUrl = "${DeviceManager.serverAddress}${serverPath}"

downloadUrl += if (serverPath.endsWith("/cover")) "?raw=1" // Download raw cover image
else "?token=${DeviceManager.token}"

val downloadUri = Uri.parse(downloadUrl)
Log.d("DownloadItemPart", "Audio File Destination Uri: $destinationUri | Final Destination Uri: $finalDestinationUri | Download URI $downloadUri")
return DownloadItemPart(
Expand Down Expand Up @@ -77,7 +80,7 @@ data class DownloadItemPart(
val isInternalStorage get() = localFolderId.startsWith("internal-")

@get:JsonIgnore
val serverUrl get() = "${DeviceManager.serverAddress}${serverPath}?token=${DeviceManager.token}"
val serverUrl get() = uri.toString()

@JsonIgnore
fun getDownloadRequest(): DownloadManager.Request {
Expand Down
3 changes: 0 additions & 3 deletions components/cards/AuthorCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export default {
}
},
computed: {
userToken() {
return this.$store.getters['user/getToken']
},
_author() {
return this.author || {}
},
Expand Down
7 changes: 2 additions & 5 deletions components/covers/AuthorImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ export default {
}
},
computed: {
userToken() {
return this.$store.getters['user/getToken']
},
_author() {
return this.author || {}
},
Expand All @@ -61,9 +58,9 @@ export default {
if (!this.imagePath || !this.serverAddress) return null
if (process.env.NODE_ENV !== 'production' && this.serverAddress.startsWith('http://192.168')) {
// Testing
return `http://localhost:3333/api/authors/${this.authorId}/image?token=${this.userToken}&ts=${this.updatedAt}`
return `http://localhost:3333/api/authors/${this.authorId}/image?ts=${this.updatedAt}`
}
return `${this.serverAddress}/api/authors/${this.authorId}/image?token=${this.userToken}&ts=${this.updatedAt}`
return `${this.serverAddress}/api/authors/${this.authorId}/image?ts=${this.updatedAt}`
}
},
methods: {
Expand Down
3 changes: 0 additions & 3 deletions components/covers/BookCover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ export default {
},
authorBottom() {
return 0.75 * this.sizeMultiplier
},
userToken() {
return this.$store.getters['user/getToken']
}
},
methods: {
Expand Down
3 changes: 0 additions & 3 deletions components/tables/ebook/EbookFilesTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ export default {
libraryItemId() {
return this.libraryItem.id
},
userToken() {
return this.$store.getters['user/getToken']
},
ebookFiles() {
return (this.libraryItem.libraryFiles || []).filter((lf) => lf.fileType === 'ebook')
},
Expand Down
3 changes: 0 additions & 3 deletions components/tables/ebook/EbookFilesTableRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ export default {
return {}
},
computed: {
userToken() {
return this.$store.getters['user/getToken']
},
userCanUpdate() {
return this.$store.getters['user/getUserCanUpdate']
},
Expand Down
3 changes: 0 additions & 3 deletions pages/item/_id/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,6 @@ export default {
user() {
return this.$store.state.user.user
},
userToken() {
return this.$store.getters['user/getToken']
},
userItemProgress() {
if (this.isPodcast) return null
if (this.isLocal) return this.localItemProgress
Expand Down
121 changes: 65 additions & 56 deletions store/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,62 +44,71 @@ export const state = () => ({
})

export const getters = {
getDownloadItem: state => (libraryItemId, episodeId = null) => {
return state.itemDownloads.find(i => {
// if (episodeId && !i.episodes.some(e => e.id == episodeId)) return false
if (episodeId && i.episodeId !== episodeId) return false
return i.libraryItemId == libraryItemId
})
},
getLibraryItemCoverSrc: (state, getters, rootState, rootGetters) => (libraryItem, placeholder, raw = false) => {
if (!libraryItem) return placeholder
const media = libraryItem.media
if (!media || !media.coverPath || media.coverPath === placeholder) return placeholder
getDownloadItem:
(state) =>
(libraryItemId, episodeId = null) => {
return state.itemDownloads.find((i) => {
// if (episodeId && !i.episodes.some(e => e.id == episodeId)) return false
if (episodeId && i.episodeId !== episodeId) return false
return i.libraryItemId == libraryItemId
})
},
getLibraryItemCoverSrc:
(state, getters, rootState, rootGetters) =>
(libraryItem, placeholder, raw = false) => {
if (!libraryItem) return placeholder
const media = libraryItem.media
if (!media || !media.coverPath || media.coverPath === placeholder) return placeholder

// Absolute URL covers (should no longer be used)
if (media.coverPath.startsWith('http:') || media.coverPath.startsWith('https:')) return media.coverPath
// Absolute URL covers (should no longer be used)
if (media.coverPath.startsWith('http:') || media.coverPath.startsWith('https:')) return media.coverPath

const userToken = rootGetters['user/getToken']
const serverAddress = rootGetters['user/getServerAddress']
if (!userToken || !serverAddress) return placeholder
const serverAddress = rootGetters['user/getServerAddress']
if (!serverAddress) return placeholder

const lastUpdate = libraryItem.updatedAt || Date.now()
const lastUpdate = libraryItem.updatedAt || Date.now()

if (process.env.NODE_ENV !== 'production') { // Testing
// return `http://localhost:3333/api/items/${libraryItem.id}/cover?token=${userToken}&ts=${lastUpdate}`
}
if (process.env.NODE_ENV !== 'production') {
// Testing
// return `http://localhost:3333/api/items/${libraryItem.id}/cover?ts=${lastUpdate}`
}

const url = new URL(`${serverAddress}/api/items/${libraryItem.id}/cover`)
return `${url}?token=${userToken}&ts=${lastUpdate}${raw ? '&raw=1' : ''}`
},
getLibraryItemCoverSrcById: (state, getters, rootState, rootGetters) => (libraryItemId, placeholder = null) => {
if (!placeholder) placeholder = `${rootState.routerBasePath}/book_placeholder.jpg`
if (!libraryItemId) return placeholder
const userToken = rootGetters['user/getToken']
const serverAddress = rootGetters['user/getServerAddress']
if (!userToken || !serverAddress) return placeholder
const url = new URL(`${serverAddress}/api/items/${libraryItem.id}/cover`)
return `${url}?ts=${lastUpdate}${raw ? '&raw=1' : ''}`
},
getLibraryItemCoverSrcById:
(state, getters, rootState, rootGetters) =>
(libraryItemId, placeholder = null) => {
if (!placeholder) placeholder = `${rootState.routerBasePath}/book_placeholder.jpg`
if (!libraryItemId) return placeholder
const serverAddress = rootGetters['user/getServerAddress']
if (!serverAddress) return placeholder

const url = new URL(`${serverAddress}/api/items/${libraryItemId}/cover`)
return `${url}?token=${userToken}`
},
getLocalMediaProgressById: (state) => (localLibraryItemId, episodeId = null) => {
return state.localMediaProgress.find(lmp => {
if (episodeId != null && lmp.localEpisodeId != episodeId) return false
return lmp.localLibraryItemId == localLibraryItemId
})
},
getLocalMediaProgressByServerItemId: (state) => (libraryItemId, episodeId = null) => {
return state.localMediaProgress.find(lmp => {
if (episodeId != null && lmp.episodeId != episodeId) return false
return lmp.libraryItemId == libraryItemId
})
},
getJumpForwardIcon: state => (jumpForwardTime) => {
const item = state.jumpForwardItems.find(i => i.value == jumpForwardTime)
const url = new URL(`${serverAddress}/api/items/${libraryItemId}/cover`)
return url.toString()
},
getLocalMediaProgressById:
(state) =>
(localLibraryItemId, episodeId = null) => {
return state.localMediaProgress.find((lmp) => {
if (episodeId != null && lmp.localEpisodeId != episodeId) return false
return lmp.localLibraryItemId == localLibraryItemId
})
},
getLocalMediaProgressByServerItemId:
(state) =>
(libraryItemId, episodeId = null) => {
return state.localMediaProgress.find((lmp) => {
if (episodeId != null && lmp.episodeId != episodeId) return false
return lmp.libraryItemId == libraryItemId
})
},
getJumpForwardIcon: (state) => (jumpForwardTime) => {
const item = state.jumpForwardItems.find((i) => i.value == jumpForwardTime)
return item ? item.icon : 'forward_10'
},
getJumpBackwardsIcon: state => (jumpBackwardsTime) => {
const item = state.jumpBackwardsItems.find(i => i.value == jumpBackwardsTime)
getJumpBackwardsIcon: (state) => (jumpBackwardsTime) => {
const item = state.jumpBackwardsItems.find((i) => i.value == jumpBackwardsTime)
return item ? item.icon : 'replay_10'
}
}
Expand All @@ -116,23 +125,23 @@ export const mutations = {
state.isModalOpen = val
},
addUpdateItemDownload(state, downloadItem) {
var index = state.itemDownloads.findIndex(i => i.id == downloadItem.id)
var index = state.itemDownloads.findIndex((i) => i.id == downloadItem.id)
if (index >= 0) {
state.itemDownloads.splice(index, 1, downloadItem)
} else {
state.itemDownloads.push(downloadItem)
}
},
updateDownloadItemPart(state, downloadItemPart) {
const downloadItem = state.itemDownloads.find(i => i.id == downloadItemPart.downloadItemId)
const downloadItem = state.itemDownloads.find((i) => i.id == downloadItemPart.downloadItemId)
if (!downloadItem) {
console.error('updateDownloadItemPart: Download item not found for itemPart', JSON.stringify(downloadItemPart))
return
}

let totalBytes = 0
let totalBytesDownloaded = 0
downloadItem.downloadItemParts = downloadItem.downloadItemParts.map(dip => {
downloadItem.downloadItemParts = downloadItem.downloadItemParts.map((dip) => {
let newDip = dip.id == downloadItemPart.id ? downloadItemPart : dip

totalBytes += newDip.completed ? Number(newDip.bytesDownloaded) : Number(newDip.fileSize)
Expand All @@ -149,7 +158,7 @@ export const mutations = {
}
},
removeItemDownload(state, id) {
state.itemDownloads = state.itemDownloads.filter(i => i.id != id)
state.itemDownloads = state.itemDownloads.filter((i) => i.id != id)
},
setBookshelfListView(state, val) {
state.bookshelfListView = val
Expand All @@ -164,18 +173,18 @@ export const mutations = {
if (!prog || !prog.id) {
return
}
var index = state.localMediaProgress.findIndex(lmp => lmp.id == prog.id)
var index = state.localMediaProgress.findIndex((lmp) => lmp.id == prog.id)
if (index >= 0) {
state.localMediaProgress.splice(index, 1, prog)
} else {
state.localMediaProgress.push(prog)
}
},
removeLocalMediaProgress(state, id) {
state.localMediaProgress = state.localMediaProgress.filter(lmp => lmp.id != id)
state.localMediaProgress = state.localMediaProgress.filter((lmp) => lmp.id != id)
},
removeLocalMediaProgressForItem(state, llid) {
state.localMediaProgress = state.localMediaProgress.filter(lmp => lmp.localLibraryItemId !== llid)
state.localMediaProgress = state.localMediaProgress.filter((lmp) => lmp.localLibraryItemId !== llid)
},
setLastSearch(state, val) {
state.lastSearch = val
Expand Down Expand Up @@ -203,4 +212,4 @@ export const mutations = {
state.rssFeedEntity = entity
state.showRSSFeedOpenCloseModal = true
}
}
}

0 comments on commit 111e8d3

Please sign in to comment.