2022-09-10 15:21:50 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
|
|
|
import android.content.Context
|
2022-10-07 23:18:55 +05:30
|
|
|
import android.os.Build
|
2022-09-10 15:40:45 +05:30
|
|
|
import com.github.libretube.obj.DownloadedFile
|
2022-09-10 15:21:50 +05:30
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
object DownloadHelper {
|
2022-11-11 23:09:56 +05:30
|
|
|
const val VIDEO_DIR = "video"
|
|
|
|
const val AUDIO_DIR = "audio"
|
|
|
|
const val METADATA_DIR = "metadata"
|
|
|
|
const val THUMBNAIL_DIR = "thumbnail"
|
|
|
|
|
2022-09-10 15:21:50 +05:30
|
|
|
private fun getOfflineStorageDir(context: Context): File {
|
2022-10-07 23:18:55 +05:30
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return context.filesDir
|
|
|
|
|
|
|
|
return try {
|
|
|
|
context.getExternalFilesDir(null)!!
|
|
|
|
} catch (e: Exception) {
|
|
|
|
context.filesDir
|
|
|
|
}
|
2022-09-10 15:21:50 +05:30
|
|
|
}
|
|
|
|
|
2022-11-11 23:09:56 +05:30
|
|
|
fun getDownloadDir(context: Context, path: String): File {
|
2022-10-10 20:28:26 +05:30
|
|
|
return File(
|
|
|
|
getOfflineStorageDir(context),
|
2022-11-11 23:09:56 +05:30
|
|
|
path
|
|
|
|
).apply {
|
|
|
|
if (!this.exists()) this.mkdirs()
|
|
|
|
}
|
2022-09-10 15:21:50 +05:30
|
|
|
}
|
2022-09-10 15:40:45 +05:30
|
|
|
|
2022-11-11 23:09:56 +05:30
|
|
|
private fun File.toDownloadedFile(): DownloadedFile {
|
|
|
|
return DownloadedFile(
|
|
|
|
name = this.name,
|
|
|
|
size = this.length()
|
|
|
|
)
|
2022-10-15 19:59:12 +05:30
|
|
|
}
|
|
|
|
|
2022-09-10 15:40:45 +05:30
|
|
|
fun getDownloadedFiles(context: Context): MutableList<DownloadedFile> {
|
2022-11-11 23:09:56 +05:30
|
|
|
val videoFiles = getDownloadDir(context, VIDEO_DIR).listFiles().orEmpty()
|
|
|
|
val audioFiles = getDownloadDir(context, AUDIO_DIR).listFiles().orEmpty().toMutableList()
|
2022-09-10 15:40:45 +05:30
|
|
|
|
|
|
|
val files = mutableListOf<DownloadedFile>()
|
|
|
|
|
2022-11-11 23:09:56 +05:30
|
|
|
videoFiles.forEach {
|
|
|
|
audioFiles.removeIf { audioFile -> audioFile.name == it.name }
|
|
|
|
files.add(it.toDownloadedFile())
|
2022-09-10 15:40:45 +05:30
|
|
|
}
|
|
|
|
|
2022-11-11 23:09:56 +05:30
|
|
|
audioFiles.forEach {
|
|
|
|
files.add(it.toDownloadedFile())
|
2022-09-10 15:40:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
2022-09-10 15:21:50 +05:30
|
|
|
}
|