mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
fix some issues with downloads
This commit is contained in:
parent
7e9f71a529
commit
b1f06d92b3
@ -1,7 +0,0 @@
|
||||
package com.github.libretube.extensions
|
||||
|
||||
import java.io.File
|
||||
|
||||
fun File.createDir() = apply {
|
||||
if (!this.exists()) this.mkdirs()
|
||||
}
|
@ -2,12 +2,10 @@ package com.github.libretube.obj
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import com.github.libretube.api.obj.Streams
|
||||
import com.github.libretube.enums.DownloadType
|
||||
|
||||
data class DownloadedFile(
|
||||
val name: String,
|
||||
val size: Long,
|
||||
val type: DownloadType,
|
||||
var metadata: Streams? = null,
|
||||
var thumbnail: Bitmap? = null
|
||||
)
|
||||
|
@ -63,8 +63,8 @@ class DownloadService : Service() {
|
||||
private fun downloadManager() {
|
||||
// initialize and create the directories to download into
|
||||
|
||||
val videoDownloadDir = DownloadHelper.getVideoDir(this)
|
||||
val audioDownloadDir = DownloadHelper.getAudioDir(this)
|
||||
val videoDownloadDir = DownloadHelper.getDownloadDir(this, DownloadHelper.VIDEO_DIR)
|
||||
val audioDownloadDir = DownloadHelper.getDownloadDir(this, DownloadHelper.AUDIO_DIR)
|
||||
|
||||
// start download
|
||||
try {
|
||||
|
@ -75,21 +75,20 @@ class OfflinePlayerActivity : BaseActivity() {
|
||||
)
|
||||
}
|
||||
|
||||
private fun File.toUri(): Uri? {
|
||||
return if (this.exists()) Uri.fromFile(this) else null
|
||||
}
|
||||
|
||||
private fun playVideo() {
|
||||
val videoDownloadDir = DownloadHelper.getVideoDir(this)
|
||||
val videoFile = File(
|
||||
videoDownloadDir,
|
||||
val videoUri = File(
|
||||
DownloadHelper.getDownloadDir(this, DownloadHelper.VIDEO_DIR),
|
||||
fileName
|
||||
)
|
||||
).toUri()
|
||||
|
||||
val audioDownloadDir = DownloadHelper.getAudioDir(this)
|
||||
val audioFile = File(
|
||||
audioDownloadDir,
|
||||
val audioUri = File(
|
||||
DownloadHelper.getDownloadDir(this, DownloadHelper.AUDIO_DIR),
|
||||
fileName
|
||||
)
|
||||
|
||||
val videoUri = if (videoFile.exists()) Uri.fromFile(videoFile) else null
|
||||
val audioUri = if (audioFile.exists()) Uri.fromFile(audioFile) else null
|
||||
).toUri()
|
||||
|
||||
setMediaSource(
|
||||
videoUri,
|
||||
|
@ -61,8 +61,8 @@ class DownloadsAdapter(
|
||||
) { _, index ->
|
||||
when (index) {
|
||||
0 -> {
|
||||
val audioDir = DownloadHelper.getAudioDir(root.context)
|
||||
val videoDir = DownloadHelper.getVideoDir(root.context)
|
||||
val audioDir = DownloadHelper.getDownloadDir(root.context, DownloadHelper.AUDIO_DIR)
|
||||
val videoDir = DownloadHelper.getDownloadDir(root.context, DownloadHelper.VIDEO_DIR)
|
||||
|
||||
listOf(audioDir, videoDir).forEach {
|
||||
val f = File(it, file.name)
|
||||
|
@ -2,12 +2,15 @@ package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import com.github.libretube.enums.DownloadType
|
||||
import com.github.libretube.extensions.createDir
|
||||
import com.github.libretube.obj.DownloadedFile
|
||||
import java.io.File
|
||||
|
||||
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 {
|
||||
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(
|
||||
getOfflineStorageDir(context),
|
||||
"video"
|
||||
).createDir()
|
||||
path
|
||||
).apply {
|
||||
if (!this.exists()) this.mkdirs()
|
||||
}
|
||||
}
|
||||
|
||||
fun getAudioDir(context: Context): File {
|
||||
return File(
|
||||
getOfflineStorageDir(context),
|
||||
"audio"
|
||||
).createDir()
|
||||
}
|
||||
|
||||
fun getMetadataDir(context: Context): File {
|
||||
return File(
|
||||
getOfflineStorageDir(context),
|
||||
"metadata"
|
||||
).createDir()
|
||||
}
|
||||
|
||||
fun getThumbnailDir(context: Context): File {
|
||||
return File(
|
||||
getOfflineStorageDir(context),
|
||||
"thumbnail"
|
||||
).createDir()
|
||||
private fun File.toDownloadedFile(): DownloadedFile {
|
||||
return DownloadedFile(
|
||||
name = this.name,
|
||||
size = this.length()
|
||||
)
|
||||
}
|
||||
|
||||
fun getDownloadedFiles(context: Context): MutableList<DownloadedFile> {
|
||||
val videoFiles = getVideoDir(context).listFiles()
|
||||
val audioFiles = getAudioDir(context).listFiles()?.toMutableList()
|
||||
val videoFiles = getDownloadDir(context, VIDEO_DIR).listFiles().orEmpty()
|
||||
val audioFiles = getDownloadDir(context, AUDIO_DIR).listFiles().orEmpty().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
|
||||
)
|
||||
)
|
||||
videoFiles.forEach {
|
||||
audioFiles.removeIf { audioFile -> audioFile.name == it.name }
|
||||
files.add(it.toDownloadedFile())
|
||||
}
|
||||
|
||||
audioFiles?.forEach {
|
||||
files.add(
|
||||
DownloadedFile(
|
||||
name = it.name,
|
||||
size = it.length(),
|
||||
type = DownloadType.AUDIO
|
||||
)
|
||||
)
|
||||
audioFiles.forEach {
|
||||
files.add(it.toDownloadedFile())
|
||||
}
|
||||
|
||||
return files
|
||||
|
@ -56,16 +56,11 @@ object ImageHelper {
|
||||
.data(url)
|
||||
.target { result ->
|
||||
val bitmap = (result as BitmapDrawable).bitmap
|
||||
saveImage(
|
||||
context,
|
||||
bitmap,
|
||||
Uri.fromFile(
|
||||
File(
|
||||
DownloadHelper.getThumbnailDir(context),
|
||||
fileName
|
||||
)
|
||||
)
|
||||
val file = File(
|
||||
DownloadHelper.getDownloadDir(context, DownloadHelper.THUMBNAIL_DIR),
|
||||
fileName
|
||||
)
|
||||
saveImage(context, bitmap, Uri.fromFile(file))
|
||||
}
|
||||
.build()
|
||||
|
||||
@ -73,15 +68,11 @@ object ImageHelper {
|
||||
}
|
||||
|
||||
fun getDownloadedImage(context: Context, fileName: String): Bitmap? {
|
||||
return getImage(
|
||||
context,
|
||||
Uri.fromFile(
|
||||
File(
|
||||
DownloadHelper.getThumbnailDir(context),
|
||||
fileName
|
||||
)
|
||||
)
|
||||
val file = File(
|
||||
DownloadHelper.getDownloadDir(context, DownloadHelper.THUMBNAIL_DIR),
|
||||
fileName
|
||||
)
|
||||
return getImage(context, Uri.fromFile(file))
|
||||
}
|
||||
|
||||
private fun saveImage(context: Context, bitmapImage: Bitmap, imagePath: Uri) {
|
||||
|
@ -11,7 +11,7 @@ class MetadataHelper(
|
||||
private val context: Context
|
||||
) {
|
||||
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) {
|
||||
val targetFile = File(metadataDir, fileName)
|
||||
|
Loading…
Reference in New Issue
Block a user