瀏覽代碼

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

arkon 4 年之前
父節點
當前提交
93960315d9

+ 10 - 0
app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt

@@ -272,6 +272,13 @@ class Downloader(
     private fun downloadChapter(download: Download): Observable<Download> = 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
     }
 }

+ 12 - 0
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.
      */

+ 2 - 1
app/src/main/res/values/strings.xml

@@ -584,7 +584,8 @@
     <string name="copy">Copy</string>
 
     <!-- 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 -->
     <string name="notification_check_updates">Checking for new chapters</string>