diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 235fe19203..bb8f3f1757 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -272,6 +272,13 @@ class Downloader( private fun downloadChapter(download: Download): Observable = Observable.defer { val chapterDirname = provider.getChapterDirName(download.chapter) 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 pageListObservable = if (download.pages == null) { @@ -489,5 +496,8 @@ class Downloader( companion object { const val TMP_DIR_SUFFIX = "_tmp" + + // Arbitrary minimum required space to start a download: 50 MB + const val MIN_DISK_SPACE = 50 * 1024 * 1024 } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt index d44a63b5d7..4cff37d196 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.os.Environment +import android.os.StatFs import androidx.core.content.ContextCompat import androidx.core.os.EnvironmentCompat import com.hippo.unifile.UniFile @@ -28,6 +29,17 @@ object DiskUtil { 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. */ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ed089c10f9..a283ef0c23 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -584,7 +584,8 @@ Copy - Could not download chapters. You can try again in the downloads section + Couldn\'t download chapters. You can try again in the downloads section + Couldn\'t download chapters due to low disk space Checking for new chapters