Avoid crashing if storage directory can't be read

e.g. when first launching and there's no storage permissions yet.
This commit is contained in:
arkon 2023-11-25 12:40:09 -05:00
parent 75314c78e0
commit a5c9469698
3 changed files with 16 additions and 29 deletions

View File

@ -87,8 +87,8 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
if (packageName != process) WebView.setDataDirectorySuffix(process) if (packageName != process) WebView.setDataDirectorySuffix(process)
} }
Injekt.importModule(AppModule(this))
Injekt.importModule(PreferenceModule(this)) Injekt.importModule(PreferenceModule(this))
Injekt.importModule(AppModule(this))
Injekt.importModule(DomainModule()) Injekt.importModule(DomainModule())
setupAcra() setupAcra()

View File

@ -297,8 +297,11 @@ class DownloadCache(
* Returns the downloads directory from the user's preferences. * Returns the downloads directory from the user's preferences.
*/ */
private fun getDirectoryFromPreference(): UniFile { private fun getDirectoryFromPreference(): UniFile {
return UniFile.fromUri(context, storagePreferences.baseStorageDirectory().get().toUri()) return storagePreferences.baseStorageDirectory().get().let {
.createDirectory(StoragePreferences.DOWNLOADS_DIR) UniFile.fromUri(context, it.toUri()).also {
it?.createDirectory(StoragePreferences.DOWNLOADS_DIR)
}
}
} }
/** /**

View File

@ -5,9 +5,6 @@ import androidx.core.net.toUri
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.DiskUtil
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import logcat.LogPriority import logcat.LogPriority
import tachiyomi.core.i18n.stringResource import tachiyomi.core.i18n.stringResource
import tachiyomi.core.util.system.logcat import tachiyomi.core.util.system.logcat
@ -29,27 +26,14 @@ class DownloadProvider(
private val storagePreferences: StoragePreferences = Injekt.get(), private val storagePreferences: StoragePreferences = Injekt.get(),
) { ) {
private val scope = MainScope() private val downloadsDir: UniFile?
get() = storagePreferences.baseStorageDirectory().get().let {
/** UniFile.fromUri(context, it.toUri())
* The root directory for downloads. ?.createDirectory(StoragePreferences.DOWNLOADS_DIR)
*/ ?.also { dir ->
private var downloadsDir = setDownloadsLocation() DiskUtil.createNoMediaFile(dir, context)
}
init {
storagePreferences.baseStorageDirectory().changes()
.onEach { downloadsDir = setDownloadsLocation() }
.launchIn(scope)
}
private fun setDownloadsLocation(): UniFile {
return storagePreferences.baseStorageDirectory().get().let {
val dir = UniFile.fromUri(context, it.toUri())
.createDirectory(StoragePreferences.DOWNLOADS_DIR)
DiskUtil.createNoMediaFile(dir, context)
dir
} }
}
/** /**
* Returns the download directory for a manga. For internal use only. * Returns the download directory for a manga. For internal use only.
@ -59,12 +43,12 @@ class DownloadProvider(
*/ */
internal fun getMangaDir(mangaTitle: String, source: Source): UniFile { internal fun getMangaDir(mangaTitle: String, source: Source): UniFile {
try { try {
return downloadsDir return downloadsDir!!
.createDirectory(getSourceDirName(source)) .createDirectory(getSourceDirName(source))
.createDirectory(getMangaDirName(mangaTitle)) .createDirectory(getMangaDirName(mangaTitle))
} catch (e: Throwable) { } catch (e: Throwable) {
logcat(LogPriority.ERROR, e) { "Invalid download directory" } logcat(LogPriority.ERROR, e) { "Invalid download directory" }
throw Exception(context.stringResource(MR.strings.invalid_location, downloadsDir)) throw Exception(context.stringResource(MR.strings.invalid_location, downloadsDir ?: ""))
} }
} }
@ -74,7 +58,7 @@ class DownloadProvider(
* @param source the source to query. * @param source the source to query.
*/ */
fun findSourceDir(source: Source): UniFile? { fun findSourceDir(source: Source): UniFile? {
return downloadsDir.findFile(getSourceDirName(source), true) return downloadsDir?.findFile(getSourceDirName(source), true)
} }
/** /**