Files
forkmessager/client-mobile/core/utils/DownloadUtils.kt
Халимов Рустам 58fdf1aca1 Чат, вложения
2026-04-14 21:53:44 +03:00

91 lines
3.6 KiB
Kotlin

package core.utils
import android.app.DownloadManager
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.widget.Toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.net.URL
object DownloadUtils {
suspend fun downloadMedia(context: Context, url: String, fileName: String?) {
withContext(Dispatchers.IO) {
try {
val name = fileName ?: url.substringAfterLast("/")
val extension = name.substringAfterLast(".", "jpg")
val isImage = extension in listOf("jpg", "jpeg", "png", "webp", "gif")
val isVideo = extension in listOf("mp4", "webm", "mkv", "mov")
if (isImage || isVideo) {
saveToGallery(context, url, name, isImage)
} else {
useDownloadManager(context, url, name)
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(context, "Ошибка скачивания: ${e.localizedMessage}", Toast.LENGTH_SHORT).show()
}
}
}
}
private fun useDownloadManager(context: Context, url: String, fileName: String) {
val request = DownloadManager.Request(Uri.parse(url))
.setTitle(fileName)
.setDescription("Downloading...")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
.setAllowedOverMetered(true)
.setAllowedOverRoaming(true)
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
// Show feedback
(context as? android.app.Activity)?.runOnUiThread {
Toast.makeText(context, "Загрузка началась: $fileName", Toast.LENGTH_SHORT).show()
}
}
private suspend fun saveToGallery(context: Context, url: String, fileName: String, isImage: Boolean) {
val resolver = context.contentResolver
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
put(MediaStore.MediaColumns.MIME_TYPE, if (isImage) "image/jpeg" else "video/mp4")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
put(MediaStore.MediaColumns.RELATIVE_PATH, if (isImage) Environment.DIRECTORY_PICTURES else Environment.DIRECTORY_MOVIES)
put(MediaStore.MediaColumns.IS_PENDING, 1)
}
}
val collection = if (isImage) {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
} else {
MediaStore.Video.Media.EXTERNAL_CONTENT_URI
}
val uri = resolver.insert(collection, contentValues)
uri?.let {
URL(url).openStream().use { input ->
resolver.openOutputStream(it).use { output ->
input.copyTo(output!!)
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
contentValues.clear()
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0)
resolver.update(it, contentValues, null, null)
}
withContext(Dispatchers.Main) {
Toast.makeText(context, "Сохранено в галерею", Toast.LENGTH_SHORT).show()
}
}
}
}