Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Use proper file names for downloaded files ( closes #34 )
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Aug 29, 2023
1 parent 32ab9a1 commit c98f967
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*******************************************************************************
Created By Suhas Dissanayake on 8/3/23, 12:11 PM
Copyright (c) 2023
https://github.com/SuhasDissa/
All Rights Reserved
******************************************************************************/

package app.suhasdissa.memerize.backend.viewmodels

import android.content.Context
Expand All @@ -23,17 +16,18 @@ import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.suhasdissa.memerize.BuildConfig
import app.suhasdissa.memerize.backend.database.entity.Meme
import app.suhasdissa.memerize.utils.SaveDirectoryKey
import app.suhasdissa.memerize.utils.preferences
import coil.ImageLoader
import coil.request.ImageRequest
import coil.request.SuccessResult
import java.io.File
import java.io.FileOutputStream
import java.util.UUID
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.util.UUID

class PhotoViewModel : ViewModel() {

Expand All @@ -52,12 +46,12 @@ class PhotoViewModel : ViewModel() {
return null
}

fun savePhotoToDisk(url: String, context: Context) {
fun savePhotoToDisk(meme: Meme, context: Context) {
viewModelScope.launch(Dispatchers.IO) {
withContext(Dispatchers.Main) {
downloadState = DownloadState.Loading
}
val bitmap = getBitmapFromUrl(url, context)
val bitmap = getBitmapFromUrl(meme.url, context)
val prefDir =
context.preferences.getString(SaveDirectoryKey, null)

Expand All @@ -72,10 +66,21 @@ class PhotoViewModel : ViewModel() {

else -> DocumentFile.fromTreeUri(context, Uri.parse(prefDir))!!
}
val outputFile = saveDir.createFile("image/jpg", "${UUID.randomUUID()}.jpg")
val outputFile =
saveDir.createFile(
"image/jpg",
"${meme.title}-${UUID.randomUUID().toString().take(8)}"
)
if (outputFile == null) {
withContext(Dispatchers.Main) {
downloadState = DownloadState.Error
}
Toast.makeText(context, "Failed to create file", Toast.LENGTH_LONG).show()
return@launch
}
if (bitmap != null) {
try {
val outputStream = context.contentResolver.openOutputStream(outputFile!!.uri)!!
val outputStream = context.contentResolver.openOutputStream(outputFile.uri)!!
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
outputStream.flush()
outputStream.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.media3.common.Player
import app.suhasdissa.memerize.backend.database.entity.Meme
import app.suhasdissa.memerize.utils.RedditVideoDownloader
import kotlinx.coroutines.launch
import java.util.UUID

class PlayerViewModel() : ViewModel() {
var downloadState: DownloadState by mutableStateOf(DownloadState.NotStarted)

var muted by mutableStateOf(false)

@RequiresApi(Build.VERSION_CODES.O)
fun downloadVideo(context: Context, url: String) {
fun downloadVideo(context: Context, meme: Meme) {
val fileName = "${meme.title}-${UUID.randomUUID().toString().take(8)}"
viewModelScope.launch {
downloadState = DownloadState.Loading
val downloader = RedditVideoDownloader()
val result = downloader.downloadRedditVideo(context.applicationContext, url)
val result =
downloader.downloadRedditVideo(context.applicationContext, meme.url, fileName)
downloadState = if (result) {
Toast.makeText(context, "Download Finished", Toast.LENGTH_LONG).show()
DownloadState.NotStarted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fun PhotoView(
}, actions = {
IconButton(onClick = {
view.playSoundEffect(SoundEffectConstants.CLICK)
photoViewModel.savePhotoToDisk(meme.url, context)
photoViewModel.savePhotoToDisk(meme, context)
}) {
Icon(
imageVector = when (photoViewModel.downloadState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fun VideoView(
PlayerController(player, onDownload = {
playerViewModel.downloadVideo(
context,
meme.url
meme
)
}, playerViewModel)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*******************************************************************************
Created By Suhas Dissanayake on 8/7/23, 6:30 PM
Copyright (c) 2023
https://github.com/SuhasDissa/
All Rights Reserved
******************************************************************************/

package app.suhasdissa.memerize.utils

import android.annotation.SuppressLint
Expand Down Expand Up @@ -48,7 +41,11 @@ class RedditVideoDownloader {

@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("Recycle")
suspend fun downloadRedditVideo(context: Context, url: String): Boolean {
suspend fun downloadRedditVideo(
context: Context,
url: String,
outputFileName: String
): Boolean {
val urlS = getRedditUrls(url) ?: return false
val redditUrl = Regex("https?://v\\.redd\\.it/\\S+/").find(url)?.value ?: return false

Expand All @@ -62,7 +59,7 @@ class RedditVideoDownloader {
val videofilePath = result.getOrNull(0)?.uri?.path ?: return@withContext false
val audioFIlePath = result.getOrNull(1)?.uri?.path

val outputFile = getOutputFile(context)
val outputFile = getOutputFile(outputFileName, context) ?: return@withContext false
val pfd = context.contentResolver.openFileDescriptor(outputFile.uri, "w")
muxVideoAndAudio(videofilePath, audioFIlePath, pfd!!)
}
Expand Down Expand Up @@ -201,7 +198,7 @@ class RedditVideoDownloader {
return selectedVideo to selectedAudio
}

private suspend fun getOutputFile(context: Context): DocumentFile {
private suspend fun getOutputFile(fileName: String, context: Context): DocumentFile? {
return withContext(Dispatchers.IO) {
val prefDir =
context.preferences.getString(SaveDirectoryKey, null)
Expand All @@ -218,7 +215,7 @@ class RedditVideoDownloader {
else -> DocumentFile.fromTreeUri(context, Uri.parse(prefDir))!!
}

saveDir.createFile("video/mp4", "${UUID.randomUUID()}.mp4")!!
saveDir.createFile("video/mp4", fileName)
}
}

Expand Down

0 comments on commit c98f967

Please sign in to comment.