Support server v1.4.0, multiple libraries, new static request routes

This commit is contained in:
advplyr 2021-10-06 07:24:15 -05:00
parent a520a1fd8e
commit 993ff5341d
12 changed files with 355 additions and 44 deletions

View file

@ -13,8 +13,8 @@ android {
applicationId "com.audiobookshelf.app"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 19
versionName "0.9.3-beta"
versionCode 20
versionName "0.9.4-beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.

View file

@ -8,7 +8,13 @@
<span class="material-icons text-3xl text-white">arrow_back</span>
</a>
<div>
<p class="text-lg font-book leading-4">AudioBookshelf</p>
<div class="px-4 py-2 bg-bg bg-opacity-30 rounded-md flex items-center" @click="clickShowLibraryModal">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4" />
</svg>
<p class="text-lg font-book leading-4 ml-2">{{ currentLibraryName }}</p>
</div>
<!-- <p class="text-lg font-book leading-4">AudioBookshelf</p> -->
</div>
<div class="flex-grow" />
<!-- <ui-menu :label="username" :items="menuItems" @action="menuAction" class="ml-5" /> -->
@ -47,6 +53,12 @@ export default {
}
},
computed: {
currentLibrary() {
return this.$store.getters['libraries/getCurrentLibrary']
},
currentLibraryName() {
return this.currentLibrary ? this.currentLibrary.name : 'Main'
},
showBack() {
return this.$route.name !== 'index'
},
@ -65,6 +77,9 @@ export default {
}
},
methods: {
clickShowLibraryModal() {
this.$store.commit('libraries/setShowModal', true)
},
back() {
if (this.$route.name === 'audiobook-id-edit') {
this.$router.push(`/audiobook/${this.$route.params.id}`)

View file

@ -12,6 +12,9 @@
<div class="py-4">No Audiobooks</div>
<ui-btn v-if="hasFilters" @click="clearFilter">Clear Filter</ui-btn>
</div>
<div v-show="isLoading" class="absolute top-0 left-0 w-full h-full flex items-center justify-center bg-black bg-opacity-70 z-20">
<div class="py-4">Loading...</div>
</div>
</div>
</template>
@ -25,6 +28,9 @@ export default {
}
},
computed: {
isLoading() {
return this.$store.state.audiobooks.isLoading
},
cardWidth() {
return 140
},
@ -81,10 +87,17 @@ export default {
this.calcShelves()
}
},
async loadAudiobooks() {
var currentLibrary = await this.$localStore.getCurrentLibrary()
if (currentLibrary) {
this.$store.commit('libraries/setCurrentLibrary', currentLibrary.id)
}
this.$store.dispatch('audiobooks/load')
},
socketConnected(isConnected) {
if (isConnected) {
console.log('Connected - Load from server')
this.$store.dispatch('audiobooks/load')
this.loadAudiobooks()
} else {
console.log('Disconnected - Reset to local storage')
this.$store.commit('audiobooks/reset')
@ -106,7 +119,7 @@ export default {
this.$server.on('connected', this.socketConnected)
if (this.$server.connected) {
this.$store.dispatch('audiobooks/load')
this.loadAudiobooks()
} else {
console.log('Bookshelf - Server not connected using downloaded')
}

View file

@ -112,14 +112,16 @@ export default {
return `${this.$store.state.serverUrl}/Logo.png`
}
if (this.cover.startsWith('http')) return this.cover
var _clean = this.cover.replace(/\\/g, '/')
if (_clean.startsWith('/local')) {
var _cover = process.env.NODE_ENV !== 'production' && process.env.PROD !== '1' ? _clean.replace('/local', '') : _clean
return `${this.$store.state.serverUrl}${_cover}?token=${this.userToken}&ts=${Date.now()}`
} else if (_clean.startsWith('/metadata')) {
return `${this.$store.state.serverUrl}${_clean}?token=${this.userToken}&ts=${Date.now()}`
}
return _clean
var coverSrc = this.$store.getters['audiobooks/getBookCoverSrc'](this.audiobook)
return coverSrc
// var _clean = this.cover.replace(/\\/g, '/')
// if (_clean.startsWith('/local')) {
// var _cover = process.env.NODE_ENV !== 'production' && process.env.PROD !== '1' ? _clean.replace('/local', '') : _clean
// return `${this.$store.state.serverUrl}${_cover}?token=${this.userToken}&ts=${Date.now()}`
// } else if (_clean.startsWith('/metadata')) {
// return `${this.$store.state.serverUrl}${_clean}?token=${this.userToken}&ts=${Date.now()}`
// }
// return _clean
}
},
methods: {

View file

@ -97,16 +97,16 @@ export default {
fullCoverUrl() {
if (this.downloadCover) return this.downloadCover
else if (!this.networkConnected) return this.placeholderUrl
if (this.cover.startsWith('http')) return this.cover
var _clean = this.cover.replace(/\\/g, '/')
if (_clean.startsWith('/local')) {
var _cover = process.env.NODE_ENV !== 'production' && process.env.PROD !== '1' ? _clean.replace('/local', '') : _clean
return `${this.$store.state.serverUrl}${_cover}?token=${this.userToken}&ts=${Date.now()}`
} else if (_clean.startsWith('/metadata')) {
return `${this.$store.state.serverUrl}${_clean}?token=${this.userToken}&ts=${Date.now()}`
}
return _clean
return this.$store.getters['audiobooks/getBookCoverSrc'](this.audiobook)
// if (this.cover.startsWith('http')) return this.cover
// var _clean = this.cover.replace(/\\/g, '/')
// if (_clean.startsWith('/local')) {
// var _cover = process.env.NODE_ENV !== 'production' && process.env.PROD !== '1' ? _clean.replace('/local', '') : _clean
// return `${this.$store.state.serverUrl}${_cover}?token=${this.userToken}&ts=${Date.now()}`
// } else if (_clean.startsWith('/metadata')) {
// return `${this.$store.state.serverUrl}${_clean}?token=${this.userToken}&ts=${Date.now()}`
// }
// return _clean
},
cover() {
return this.book.cover || this.placeholderUrl

View file

@ -0,0 +1,65 @@
<template>
<modals-modal v-model="show" :width="300" :processing="processing" height="100%">
<template #outer>
<div class="absolute top-4 left-4 z-40" style="max-width: 80%">
<p class="text-white text-2xl truncate">Libraries</p>
</div>
</template>
<div class="w-full h-full overflow-hidden absolute top-0 left-0 flex items-center justify-center" @click="show = false">
<div class="w-full overflow-x-hidden overflow-y-auto bg-primary rounded-lg border border-white border-opacity-20" style="max-height: 75%" @click.stop>
<ul class="h-full w-full" role="listbox" aria-labelledby="listbox-label">
<template v-for="library in libraries">
<li :key="library.id" class="text-gray-50 select-none relative py-3 cursor-pointer hover:bg-black-400" :class="currentLibraryId === library.id ? 'bg-bg bg-opacity-80' : ''" role="option" @click="clickedOption(library)">
<div v-show="currentLibraryId === library.id" class="absolute top-0 left-0 w-0.5 bg-warning h-full" />
<div class="flex items-center px-3">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4" />
</svg>
<span class="font-normal block truncate text-lg ml-4">{{ library.name }}</span>
</div>
</li>
</template>
</ul>
</div>
</div>
</modals-modal>
</template>
<script>
export default {
data() {
return {
processing: false
}
},
computed: {
show: {
get() {
return this.$store.state.libraries.showModal
},
set(val) {
this.$store.commit('libraries/setShowModal', val)
}
},
currentLibraryId() {
return this.$store.state.libraries.currentLibraryId
},
libraries() {
return this.$store.state.libraries.libraries
}
},
methods: {
async clickedOption(lib) {
this.show = false
this.$store.commit('libraries/setCurrentLibrary', lib.id)
await this.$store.dispatch('audiobooks/load')
this.$localStore.setCurrentLibrary(lib)
}
},
mounted() {}
}
</script>

View file

@ -6,6 +6,7 @@
</div>
<app-stream-container ref="streamContainer" />
<modals-downloads-modal ref="downloadsModal" @selectDownload="selectDownload" @deleteDownload="deleteDownload" />
<modals-libraries-modal />
</div>
</template>
@ -29,13 +30,16 @@ export default {
}
},
methods: {
connected(isConnected) {
async connected(isConnected) {
if (this.$route.name === 'connect') {
if (isConnected) {
this.$router.push('/')
}
}
this.syncUserProgress()
// Load libraries
this.$store.dispatch('libraries/load')
},
updateAudiobookProgressOnServer(audiobookProgress) {
if (this.$server.socket) {
@ -363,8 +367,6 @@ export default {
mounted() {
if (!this.$server) return console.error('No Server')
console.log('Default Mounted')
this.$server.on('connected', this.connected)
this.$server.on('initialStream', this.initialStream)

View file

@ -1,6 +1,6 @@
{
"name": "audiobookshelf-app",
"version": "v0.9.3-beta",
"version": "v0.9.4-beta",
"author": "advplyr",
"scripts": {
"dev": "nuxt --hostname localhost --port 1337",

View file

@ -272,7 +272,9 @@ export default {
console.log('Single track, start download no prep needed')
var track = audiobook.tracks[0]
var fileext = track.ext
var url = `${this.$store.state.serverUrl}/local/${track.path}`
var relTrackPath = track.path.replace('\\', '/').replace(this.audiobook.path.replace('\\', '/'), '')
var url = `${this.$store.state.serverUrl}/s/book/${this.audiobookId}/${relTrackPath}`
this.startDownload(url, fileext, downloadObject)
} else {
// Multi-track merge
@ -291,14 +293,16 @@ export default {
var cover = this.book.cover
if (cover.startsWith('http')) return cover
var _clean = cover.replace(/\\/g, '/')
if (_clean.startsWith('/local')) {
var _cover = process.env.NODE_ENV !== 'production' && process.env.PROD !== '1' ? _clean.replace('/local', '') : _clean
return `${this.$store.state.serverUrl}${_cover}?token=${this.userToken}&ts=${Date.now()}`
} else if (_clean.startsWith('/metadata')) {
return `${this.$store.state.serverUrl}${_clean}?token=${this.userToken}&ts=${Date.now()}`
}
return _clean
var coverSrc = this.$store.getters['audiobooks/getBookCoverSrc'](this.audiobook)
return coverSrc
// var _clean = cover.replace(/\\/g, '/')
// if (_clean.startsWith('/local')) {
// var _cover = process.env.NODE_ENV !== 'production' && process.env.PROD !== '1' ? _clean.replace('/local', '') : _clean
// return `${this.$store.state.serverUrl}${_cover}?token=${this.userToken}&ts=${Date.now()}`
// } else if (_clean.startsWith('/metadata')) {
// return `${this.$store.state.serverUrl}${_clean}?token=${this.userToken}&ts=${Date.now()}`
// }
// return _clean
},
async startDownload(url, fileext, download) {
this.$toast.update(download.toastId, { content: `Downloading "${download.audiobook.book.title}"...` })
@ -306,7 +310,9 @@ export default {
var coverDownloadUrl = this.getCoverUrlForDownload()
var coverFilename = null
if (coverDownloadUrl) {
var coverExt = Path.extname(coverDownloadUrl) || '.jpg'
var coverNoQueryString = coverDownloadUrl.split('?')[0]
var coverExt = Path.extname(coverNoQueryString) || '.jpg'
coverFilename = `cover-${download.id}${coverExt}`
}
@ -337,7 +343,7 @@ export default {
var download = this.$store.getters['downloads/getDownload'](prepareDownload.audiobookId)
if (download) {
var fileext = prepareDownload.ext
var url = `${this.$store.state.serverUrl}/downloads/${prepareDownload.id}/${prepareDownload.filename}`
var url = `${this.$store.state.serverUrl}/downloads/${prepareDownload.id}/${prepareDownload.filename}?token=${this.userToken}`
this.startDownload(url, fileext, download)
} else {
console.error('Prepare download killed but download not found', prepareDownload)

View file

@ -503,6 +503,29 @@ class LocalStorage {
}
}
async setCurrentLibrary(library) {
try {
if (library) {
await Storage.set({ key: 'library', value: JSON.stringify(library) })
} else {
await Storage.remove({ key: 'library' })
}
} catch (error) {
console.error('[LocalStorage] Failed to set library', error)
}
}
async getCurrentLibrary() {
try {
var _value = (await Storage.get({ key: 'library' }) || {}).value || null
if (!_value) return null
return JSON.parse(_value)
} catch (error) {
console.error('[LocalStorage] Failed to get current library', error)
return null
}
}
async setDownloadFolder(folderObj) {
try {
if (folderObj) {

View file

@ -9,7 +9,10 @@ export const state = () => ({
listeners: [],
genres: [...STANDARD_GENRES],
tags: [],
series: []
series: [],
loadedLibraryId: 'main',
lastLoad: 0,
isLoading: false
})
export const getters = {
@ -63,21 +66,73 @@ export const getters = {
var _genres = []
state.audiobooks.filter(ab => !!(ab.book && ab.book.genres)).forEach(ab => _genres = _genres.concat(ab.book.genres))
return [...new Set(_genres)].sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1)
},
getBookCoverSrc: (state, getters, rootState, rootGetters) => (bookItem, placeholder = '/book_placeholder.jpg') => {
var book = bookItem.book
if (!book || !book.cover || book.cover === placeholder) return placeholder
var cover = book.cover
// Absolute URL covers (should no longer be used)
if (cover.startsWith('http:') || cover.startsWith('https:')) return cover
// Server hosted covers
try {
// Ensure cover is refreshed if cached
var bookLastUpdate = book.lastUpdate || Date.now()
var userToken = rootGetters['user/getToken']
// Map old covers to new format /s/book/{bookid}/*
if (cover.substr(1).startsWith('local')) {
cover = cover.replace('local', `s/book/${bookItem.id}`)
if (cover.includes(bookItem.path)) { // Remove book path
cover = cover.replace(bookItem.path, '').replace('//', '/').replace('\\\\', '/')
}
}
var url = new URL(cover, rootState.serverUrl)
return url + `?token=${userToken}&ts=${bookLastUpdate}`
} catch (err) {
console.error(err)
return placeholder
}
}
}
export const actions = {
load({ commit, dispatch }) {
return this.$axios
.$get(`/api/audiobooks`)
load({ state, commit, rootState }) {
if (!rootState.user || !rootState.user.user) {
console.error('audiobooks/load - User not set')
return false
}
var currentLibraryId = rootState.libraries.currentLibraryId
if (currentLibraryId === state.loadedLibraryId) {
// Don't load again if already loaded in the last 5 minutes
var lastLoadDiff = Date.now() - state.lastLoad
if (lastLoadDiff < 5 * 60 * 1000) {
// Already up to date
return false
}
} else {
commit('reset')
commit('setLoading', true)
}
commit('setLoadedLibrary', currentLibraryId)
this.$axios
.$get(`/api/library/${currentLibraryId}/audiobooks`)
.then((data) => {
console.log('Audiobooks request data', data)
commit('set', data)
dispatch('setNativeAudiobooks')
commit('setLastLoad')
commit('setLoading', false)
})
.catch((error) => {
console.error('Failed', error)
commit('set', [])
commit('setLoading', false)
})
return true
},
useDownloaded({ commit, rootGetters }) {
commit('set', rootGetters['downloads/getAudiobooks'])
@ -100,6 +155,15 @@ export const actions = {
}
export const mutations = {
setLoadedLibrary(state, val) {
state.loadedLibraryId = val
},
setLoading(state, val) {
state.isLoading = val
},
setLastLoad(state, val) {
state.lastLoad = val
},
reset(state) {
state.audiobooks = []
state.genres = [...STANDARD_GENRES]

121
store/libraries.js Normal file
View file

@ -0,0 +1,121 @@
export const state = () => ({
libraries: [],
lastLoad: 0,
listeners: [],
currentLibraryId: 'main',
showModal: false,
folders: [],
folderLastUpdate: 0
})
export const getters = {
getCurrentLibrary: state => {
return state.libraries.find(lib => lib.id === state.currentLibraryId)
}
}
export const actions = {
fetch({ state, commit, rootState }, libraryId) {
if (!rootState.user || !rootState.user.user) {
console.error('libraries/fetch - User not set')
return false
}
var library = state.libraries.find(lib => lib.id === libraryId)
if (library) {
commit('setCurrentLibrary', libraryId)
return library
}
return this.$axios
.$get(`/api/library/${libraryId}`)
.then((data) => {
commit('addUpdate', data)
commit('setCurrentLibrary', libraryId)
return data
})
.catch((error) => {
console.error('Failed', error)
return false
})
},
// Return true if calling load
load({ state, commit, rootState }) {
if (!rootState.user || !rootState.user.user) {
console.error('libraries/load - User not set')
return false
}
// Don't load again if already loaded in the last 5 minutes
var lastLoadDiff = Date.now() - state.lastLoad
if (lastLoadDiff < 5 * 60 * 1000) {
// Already up to date
return false
}
this.$axios
.$get(`/api/libraries`)
.then((data) => {
commit('set', data)
commit('setLastLoad')
})
.catch((error) => {
console.error('Failed', error)
commit('set', [])
})
return true
},
}
export const mutations = {
setFolders(state, folders) {
state.folders = folders
},
setFoldersLastUpdate(state) {
state.folderLastUpdate = Date.now()
},
setShowModal(state, val) {
state.showModal = val
},
setLastLoad(state) {
state.lastLoad = Date.now()
},
setCurrentLibrary(state, val) {
state.currentLibraryId = val
},
set(state, libraries) {
console.log('set libraries', libraries)
state.libraries = libraries
state.listeners.forEach((listener) => {
listener.meth()
})
},
addUpdate(state, library) {
var index = state.libraries.findIndex(a => a.id === library.id)
if (index >= 0) {
state.libraries.splice(index, 1, library)
} else {
state.libraries.push(library)
}
state.listeners.forEach((listener) => {
listener.meth()
})
},
remove(state, library) {
state.libraries = state.libraries.filter(a => a.id !== library.id)
state.listeners.forEach((listener) => {
listener.meth()
})
},
addListener(state, listener) {
var index = state.listeners.findIndex(l => l.id === listener.id)
if (index >= 0) state.listeners.splice(index, 1, listener)
else state.listeners.push(listener)
},
removeListener(state, listenerId) {
state.listeners = state.listeners.filter(l => l.id !== listenerId)
}
}