Prevent downloads when less than 50MB of disk space is available (closes #1018)

This commit is contained in:
arkon 2020-07-10 12:47:19 -04:00
parent 479eb1ba71
commit 93960315d9
3 changed files with 24 additions and 1 deletions

View file

@ -272,6 +272,13 @@ class Downloader(
private fun downloadChapter(download: Download): Observable<Download> = Observable.defer { private fun downloadChapter(download: Download): Observable<Download> = Observable.defer {
val chapterDirname = provider.getChapterDirName(download.chapter) val chapterDirname = provider.getChapterDirName(download.chapter)
val mangaDir = provider.getMangaDir(download.manga, download.source) val mangaDir = provider.getMangaDir(download.manga, download.source)
if (DiskUtil.getAvailableStorageSpace(mangaDir) < MIN_DISK_SPACE) {
download.status = Download.ERROR
notifier.onError(context.getString(R.string.download_insufficient_space), download.chapter.name)
return@defer Observable.just(download)
}
val tmpDir = mangaDir.createDirectory(chapterDirname + TMP_DIR_SUFFIX) val tmpDir = mangaDir.createDirectory(chapterDirname + TMP_DIR_SUFFIX)
val pageListObservable = if (download.pages == null) { val pageListObservable = if (download.pages == null) {
@ -489,5 +496,8 @@ class Downloader(
companion object { companion object {
const val TMP_DIR_SUFFIX = "_tmp" const val TMP_DIR_SUFFIX = "_tmp"
// Arbitrary minimum required space to start a download: 50 MB
const val MIN_DISK_SPACE = 50 * 1024 * 1024
} }
} }

View file

@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Environment import android.os.Environment
import android.os.StatFs
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.os.EnvironmentCompat import androidx.core.os.EnvironmentCompat
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
@ -28,6 +29,17 @@ object DiskUtil {
return size return size
} }
/**
* Gets the available space for the disk that a file path points to, in bytes.
*/
fun getAvailableStorageSpace(f: UniFile): Long {
val stat = StatFs(f.filePath)
val availBlocks = stat.availableBlocksLong
val blockSize = stat.blockSizeLong
return availBlocks * blockSize
}
/** /**
* Returns the root folders of all the available external storages. * Returns the root folders of all the available external storages.
*/ */

View file

@ -584,7 +584,8 @@
<string name="copy">Copy</string> <string name="copy">Copy</string>
<!-- Downloads activity and service --> <!-- Downloads activity and service -->
<string name="download_queue_error">Could not download chapters. You can try again in the downloads section</string> <string name="download_queue_error">Couldn\'t download chapters. You can try again in the downloads section</string>
<string name="download_insufficient_space">Couldn\'t download chapters due to low disk space</string>
<!-- Library update service notifications --> <!-- Library update service notifications -->
<string name="notification_check_updates">Checking for new chapters</string> <string name="notification_check_updates">Checking for new chapters</string>