diff --git a/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt b/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt index 211455a86..96dc278a6 100644 --- a/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt @@ -9,7 +9,6 @@ import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.obj.DownloadItem import com.github.libretube.services.DownloadService import java.nio.file.Path -import kotlin.io.path.createDirectories object DownloadHelper { const val VIDEO_DIR = "video" @@ -35,7 +34,11 @@ object DownloadHelper { } fun getDownloadDir(context: Context, path: String): Path { - return getOfflineStorageDir(context).resolve(path).createDirectories() + // TODO: Use createDirectories() when https://issuetracker.google.com/issues/279034662 is + // fixed. + return getOfflineStorageDir(context).resolve(path).apply { + toFile().mkdirs() + } } fun getMaxConcurrentDownloads(): Int { diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 5f946bd4c..99727c223 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -45,6 +45,7 @@ import java.nio.file.StandardOpenOption import java.util.concurrent.Executors import kotlin.io.path.absolute import kotlin.io.path.createFile +import kotlin.io.path.deleteIfExists import kotlin.io.path.fileSize import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers @@ -103,7 +104,6 @@ class DownloadService : LifecycleService() { } val thumbnailTargetPath = getDownloadPath(DownloadHelper.THUMBNAIL_DIR, fileName) - .absolute() val download = Download( videoId, @@ -147,7 +147,7 @@ class DownloadService : LifecycleService() { FileType.AUDIO -> getDownloadPath(DownloadHelper.AUDIO_DIR, item.fileName) FileType.VIDEO -> getDownloadPath(DownloadHelper.VIDEO_DIR, item.fileName) FileType.SUBTITLE -> getDownloadPath(DownloadHelper.SUBTITLE_DIR, item.fileName) - }.createFile().absolute() + }.apply { deleteIfExists() }.createFile() lifecycleScope.launch(coroutineContext) { item.id = Database.downloadDao().insertDownloadItem(item).toInt() @@ -440,7 +440,7 @@ class DownloadService : LifecycleService() { * Get a [File] from the corresponding download directory and the file name */ private fun getDownloadPath(directory: String, fileName: String): Path { - return DownloadHelper.getDownloadDir(this, directory).resolve(fileName) + return DownloadHelper.getDownloadDir(this, directory).resolve(fileName).absolute() } override fun onDestroy() {