fix some issues with downloads

This commit is contained in:
Bnyro 2022-11-11 18:39:56 +01:00
parent 7e9f71a529
commit b1f06d92b3
8 changed files with 45 additions and 91 deletions

View File

@ -1,7 +0,0 @@
package com.github.libretube.extensions
import java.io.File
fun File.createDir() = apply {
if (!this.exists()) this.mkdirs()
}

View File

@ -2,12 +2,10 @@ package com.github.libretube.obj
import android.graphics.Bitmap import android.graphics.Bitmap
import com.github.libretube.api.obj.Streams import com.github.libretube.api.obj.Streams
import com.github.libretube.enums.DownloadType
data class DownloadedFile( data class DownloadedFile(
val name: String, val name: String,
val size: Long, val size: Long,
val type: DownloadType,
var metadata: Streams? = null, var metadata: Streams? = null,
var thumbnail: Bitmap? = null var thumbnail: Bitmap? = null
) )

View File

@ -63,8 +63,8 @@ class DownloadService : Service() {
private fun downloadManager() { private fun downloadManager() {
// initialize and create the directories to download into // initialize and create the directories to download into
val videoDownloadDir = DownloadHelper.getVideoDir(this) val videoDownloadDir = DownloadHelper.getDownloadDir(this, DownloadHelper.VIDEO_DIR)
val audioDownloadDir = DownloadHelper.getAudioDir(this) val audioDownloadDir = DownloadHelper.getDownloadDir(this, DownloadHelper.AUDIO_DIR)
// start download // start download
try { try {

View File

@ -75,21 +75,20 @@ class OfflinePlayerActivity : BaseActivity() {
) )
} }
private fun File.toUri(): Uri? {
return if (this.exists()) Uri.fromFile(this) else null
}
private fun playVideo() { private fun playVideo() {
val videoDownloadDir = DownloadHelper.getVideoDir(this) val videoUri = File(
val videoFile = File( DownloadHelper.getDownloadDir(this, DownloadHelper.VIDEO_DIR),
videoDownloadDir,
fileName fileName
) ).toUri()
val audioDownloadDir = DownloadHelper.getAudioDir(this) val audioUri = File(
val audioFile = File( DownloadHelper.getDownloadDir(this, DownloadHelper.AUDIO_DIR),
audioDownloadDir,
fileName fileName
) ).toUri()
val videoUri = if (videoFile.exists()) Uri.fromFile(videoFile) else null
val audioUri = if (audioFile.exists()) Uri.fromFile(audioFile) else null
setMediaSource( setMediaSource(
videoUri, videoUri,

View File

@ -61,8 +61,8 @@ class DownloadsAdapter(
) { _, index -> ) { _, index ->
when (index) { when (index) {
0 -> { 0 -> {
val audioDir = DownloadHelper.getAudioDir(root.context) val audioDir = DownloadHelper.getDownloadDir(root.context, DownloadHelper.AUDIO_DIR)
val videoDir = DownloadHelper.getVideoDir(root.context) val videoDir = DownloadHelper.getDownloadDir(root.context, DownloadHelper.VIDEO_DIR)
listOf(audioDir, videoDir).forEach { listOf(audioDir, videoDir).forEach {
val f = File(it, file.name) val f = File(it, file.name)

View File

@ -2,12 +2,15 @@ package com.github.libretube.util
import android.content.Context import android.content.Context
import android.os.Build import android.os.Build
import com.github.libretube.enums.DownloadType
import com.github.libretube.extensions.createDir
import com.github.libretube.obj.DownloadedFile import com.github.libretube.obj.DownloadedFile
import java.io.File import java.io.File
object DownloadHelper { object DownloadHelper {
const val VIDEO_DIR = "video"
const val AUDIO_DIR = "audio"
const val METADATA_DIR = "metadata"
const val THUMBNAIL_DIR = "thumbnail"
private fun getOfflineStorageDir(context: Context): File { private fun getOfflineStorageDir(context: Context): File {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return context.filesDir if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return context.filesDir
@ -18,65 +21,35 @@ object DownloadHelper {
} }
} }
fun getVideoDir(context: Context): File { fun getDownloadDir(context: Context, path: String): File {
return File( return File(
getOfflineStorageDir(context), getOfflineStorageDir(context),
"video" path
).createDir() ).apply {
if (!this.exists()) this.mkdirs()
}
} }
fun getAudioDir(context: Context): File { private fun File.toDownloadedFile(): DownloadedFile {
return File( return DownloadedFile(
getOfflineStorageDir(context), name = this.name,
"audio" size = this.length()
).createDir() )
}
fun getMetadataDir(context: Context): File {
return File(
getOfflineStorageDir(context),
"metadata"
).createDir()
}
fun getThumbnailDir(context: Context): File {
return File(
getOfflineStorageDir(context),
"thumbnail"
).createDir()
} }
fun getDownloadedFiles(context: Context): MutableList<DownloadedFile> { fun getDownloadedFiles(context: Context): MutableList<DownloadedFile> {
val videoFiles = getVideoDir(context).listFiles() val videoFiles = getDownloadDir(context, VIDEO_DIR).listFiles().orEmpty()
val audioFiles = getAudioDir(context).listFiles()?.toMutableList() val audioFiles = getDownloadDir(context, AUDIO_DIR).listFiles().orEmpty().toMutableList()
val files = mutableListOf<DownloadedFile>() val files = mutableListOf<DownloadedFile>()
videoFiles?.forEach { videoFiles.forEach {
var type = DownloadType.VIDEO audioFiles.removeIf { audioFile -> audioFile.name == it.name }
audioFiles?.forEach { audioFile -> files.add(it.toDownloadedFile())
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 { audioFiles.forEach {
files.add( files.add(it.toDownloadedFile())
DownloadedFile(
name = it.name,
size = it.length(),
type = DownloadType.AUDIO
)
)
} }
return files return files

View File

@ -56,16 +56,11 @@ object ImageHelper {
.data(url) .data(url)
.target { result -> .target { result ->
val bitmap = (result as BitmapDrawable).bitmap val bitmap = (result as BitmapDrawable).bitmap
saveImage( val file = File(
context, DownloadHelper.getDownloadDir(context, DownloadHelper.THUMBNAIL_DIR),
bitmap, fileName
Uri.fromFile(
File(
DownloadHelper.getThumbnailDir(context),
fileName
)
)
) )
saveImage(context, bitmap, Uri.fromFile(file))
} }
.build() .build()
@ -73,15 +68,11 @@ object ImageHelper {
} }
fun getDownloadedImage(context: Context, fileName: String): Bitmap? { fun getDownloadedImage(context: Context, fileName: String): Bitmap? {
return getImage( val file = File(
context, DownloadHelper.getDownloadDir(context, DownloadHelper.THUMBNAIL_DIR),
Uri.fromFile( fileName
File(
DownloadHelper.getThumbnailDir(context),
fileName
)
)
) )
return getImage(context, Uri.fromFile(file))
} }
private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) { private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) {

View File

@ -11,7 +11,7 @@ class MetadataHelper(
private val context: Context private val context: Context
) { ) {
private val mapper = ObjectMapper() private val mapper = ObjectMapper()
private val metadataDir = DownloadHelper.getMetadataDir(context) private val metadataDir = DownloadHelper.getDownloadDir(context, DownloadHelper.METADATA_DIR)
fun createMetadata(fileName: String, streams: Streams) { fun createMetadata(fileName: String, streams: Streams) {
val targetFile = File(metadataDir, fileName) val targetFile = File(metadataDir, fileName)