Fix unnecessary download thread, fix load audiobooks having different uri

This commit is contained in:
advplyr 2021-10-18 19:40:05 -05:00
parent 5a025b3a03
commit cd3277a5f9
6 changed files with 37 additions and 19 deletions

View file

@ -28,8 +28,13 @@ class Server extends EventEmitter {
getServerUrl(url) {
if (!url) return null
try {
var urlObject = new URL(url)
return `${urlObject.protocol}//${urlObject.hostname}:${urlObject.port}`
} catch (error) {
console.error('Invalid URL', error)
return null
}
}
setUser(user) {
@ -82,6 +87,9 @@ class Server extends EventEmitter {
async check(url) {
var serverUrl = this.getServerUrl(url)
if (!serverUrl) {
return false
}
var res = await this.ping(serverUrl)
if (!res || !res.success) {
return false
@ -134,7 +142,7 @@ class Server extends EventEmitter {
ping(url) {
var pingUrl = url + '/ping'
console.log('[Server] Check server', pingUrl)
return axios.get(pingUrl).then((res) => {
return axios.get(pingUrl, { timeout: 1000 }).then((res) => {
return res.data
}).catch(error => {
console.error('Server check failed', error)

View file

@ -13,8 +13,8 @@ android {
applicationId "com.audiobookshelf.app"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 23
versionName "0.9.7-beta"
versionCode 24
versionName "0.9.8-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

@ -25,8 +25,6 @@ import com.getcapacitor.PluginMethod
import com.getcapacitor.annotation.CapacitorPlugin
import org.json.JSONObject
import java.io.File
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
@CapacitorPlugin(name = "AudioDownloader")
@ -222,17 +220,28 @@ class AudioDownloader : Plugin() {
if (audiobookFile == null) {
Log.e(tag, "Audiobook was not found $audiobookUrl")
} else {
Log.d(tag, "Audiobook File Found StorageId:${audiobookFile.getStorageId(context)} | AbsolutePath:${audiobookFile.getAbsolutePath(context)} | BasePath:${audiobookFile.getBasePath(context)}")
var _name = audiobookFile.name
if (_name == null) _name = ""
var _coverUrl = ""
if (coverFile != null) _coverUrl = coverFile.uri.toString()
var size = audiobookFile.length()
var abItem = AudiobookItem(audiobookFile.uri, _name, size, _coverUrl)
if (audiobookFile.uri.toString() !== audiobookUrl) {
Log.d(tag, "Audiobook URI ${audiobookFile.uri} is different from $audiobookUrl => using the latter")
}
// Use existing URI's - bug happening where new uri is different from initial
var abItem = AudiobookItem(Uri.parse(audiobookUrl), _name, size, coverUrl)
Log.d(tag, "Setting AB ITEM ${abItem.name} | ${abItem.size} | ${abItem.uri} | ${abItem.coverUrl}")
audiobookItems.add(abItem)
}
}
Log.d(tag, "Load Finished ${audiobookItems.size} found")
var audiobookObjs:List<JSObject> = audiobookItems.map{ it.toJSObject() }
var mediaItemNoticePayload = JSObject()
mediaItemNoticePayload.put("items", audiobookObjs)
@ -341,7 +350,7 @@ class AudioDownloader : Plugin() {
var simplePath = resultDocFile.getSimplePath(context)
var storageId = resultDocFile.getStorageId(context)
var size = resultDocFile.length()
Log.d(tag, "Finished Moving File, NAME: ${resultDocFile.name} | $storageId | SimplePath: $simplePath")
Log.d(tag, "Finished Moving File, NAME: ${resultDocFile.name} | URI:${resultDocFile.uri} | AbsolutePath:${resultDocFile.getAbsolutePath(context)} | $storageId | SimplePath: $simplePath")
var abFolder = folder.findFolder(title)
var jsobj = JSObject()
@ -370,15 +379,12 @@ class AudioDownloader : Plugin() {
}
// After file is downloaded, move the files into an audiobook directory inside the user selected folder
val executorService: ExecutorService = Executors.newFixedThreadPool(4)
executorService.execute {
if (id == coverDownloadId) {
docfile?.moveFileTo(context, folder, FileDescription(coverFilename, title, MimeType.IMAGE), callback)
} else if (id == audiobookDownloadId) {
docfile?.moveFileTo(context, folder, FileDescription(filename, title, MimeType.AUDIO), callback)
}
}
}
var progressUpdater = DownloadProgressUpdater(downloadManager, audiobookDownloadId, progressReceiver, doneReceiver)
progressUpdater.run()

View file

@ -173,7 +173,7 @@ export default {
var size = data.size || 0
var isCover = !!data.isCover
console.log('Download complete', filename, downloadId, contentUrl, 'AudiobookId:', audiobookId, 'Is Cover:', isCover)
console.log(`Download complete "${contentUrl}" | ${filename} | DlId: ${downloadId} | Is Cover? ${isCover}`)
var downloadObj = this.$store.getters['downloads/getDownload'](audiobookId)
if (!downloadObj) {
console.error('Failed to find download for audiobook', audiobookId)
@ -231,9 +231,9 @@ export default {
coverUrl: item.coverUrl || null
}
})
console.log('onMediaLoaded Items', JSON.stringify(jsitems))
var downloads = await this.$sqlStore.getAllDownloads()
for (let i = 0; i < downloads.length; i++) {
var download = downloads[i]
var jsitem = jsitems.find((item) => item.contentUrl === download.contentUrl)
@ -298,7 +298,6 @@ export default {
},
async initMediaStore() {
// Request and setup listeners for media files on native
console.log('Permissino SET LISTENER')
AudioDownloader.addListener('permission', (data) => {
this.onPermissionUpdate(data)
})
@ -349,7 +348,7 @@ export default {
}
var checkPermission = await AudioDownloader.checkStoragePermission()
console.log('Storage Permission is', checkPermission.value)
console.log('Storage Permission is' + checkPermission.value)
if (!checkPermission.value) {
console.log('Will require permissions')
} else {

View file

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

View file

@ -25,6 +25,8 @@
<ui-btn v-if="isUpdateAvailable" class="w-full my-4" color="success" @click="clickUpdate">Update is available</ui-btn>
<ui-btn v-if="!isUpdateAvailable || immediateUpdateAllowed" class="w-full my-4" color="primary" @click="openAppStore">Open app store</ui-btn>
<p>UA: {{ updateAvailability }} | Avail: {{ availableVersion }} | Curr: {{ currentVersion }} | ImmedAllowed: {{ immediateUpdateAllowed }}</p>
</div>
</template>
@ -51,6 +53,9 @@ export default {
availableVersion() {
return this.appUpdateInfo ? this.appUpdateInfo.availableVersion : null
},
currentVersion() {
return this.appUpdateInfo ? this.appUpdateInfo.currentVersion : null
},
immediateUpdateAllowed() {
return this.appUpdateInfo ? !!this.appUpdateInfo.immediateUpdateAllowed : false
},