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-11-06 14:41:19 +05:30
|
|
|
import com.github.libretube.enums.DownloadType
|
2022-10-10 20:28:26 +05:30
|
|
|
import com.github.libretube.extensions.createDir
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fun getVideoDir(context: Context): File {
|
|
|
|
return File(
|
|
|
|
getOfflineStorageDir(context),
|
|
|
|
"video"
|
2022-10-10 20:28:26 +05:30
|
|
|
).createDir()
|
2022-09-10 15:21:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fun getAudioDir(context: Context): File {
|
|
|
|
return File(
|
|
|
|
getOfflineStorageDir(context),
|
|
|
|
"audio"
|
2022-10-10 20:28:26 +05:30
|
|
|
).createDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getMetadataDir(context: Context): File {
|
|
|
|
return File(
|
|
|
|
getOfflineStorageDir(context),
|
|
|
|
"metadata"
|
|
|
|
).createDir()
|
2022-09-10 15:21:50 +05:30
|
|
|
}
|
2022-09-10 15:40:45 +05:30
|
|
|
|
2022-10-15 19:59:12 +05:30
|
|
|
fun getThumbnailDir(context: Context): File {
|
|
|
|
return File(
|
|
|
|
getOfflineStorageDir(context),
|
|
|
|
"thumbnail"
|
|
|
|
).createDir()
|
|
|
|
}
|
|
|
|
|
2022-09-10 15:40:45 +05:30
|
|
|
fun getDownloadedFiles(context: Context): MutableList<DownloadedFile> {
|
|
|
|
val videoFiles = getVideoDir(context).listFiles()
|
|
|
|
val audioFiles = getAudioDir(context).listFiles()?.toMutableList()
|
|
|
|
|
|
|
|
val files = mutableListOf<DownloadedFile>()
|
|
|
|
|
|
|
|
videoFiles?.forEach {
|
|
|
|
var type = DownloadType.VIDEO
|
|
|
|
audioFiles?.forEach { audioFile ->
|
|
|
|
if (audioFile.name == it.name) {
|
|
|
|
type = DownloadType.AUDIO_VIDEO
|
|
|
|
audioFiles.remove(audioFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files.add(
|
|
|
|
DownloadedFile(
|
|
|
|
name = it.name,
|
|
|
|
size = it.length(),
|
|
|
|
type = type
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
audioFiles?.forEach {
|
|
|
|
files.add(
|
|
|
|
DownloadedFile(
|
|
|
|
name = it.name,
|
|
|
|
size = it.length(),
|
|
|
|
type = DownloadType.AUDIO
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
2022-09-10 15:21:50 +05:30
|
|
|
}
|