added support for multiple folder support in filename templates. Not working in OTG
This commit is contained in:
parent
04f5333ffc
commit
4061f3878a
4 changed files with 69 additions and 35 deletions
|
|
@ -319,6 +319,7 @@ class MainActivity : BaseActivity() {
|
||||||
permissions.add(Manifest.permission.READ_MEDIA_VIDEO)
|
permissions.add(Manifest.permission.READ_MEDIA_VIDEO)
|
||||||
}else{
|
}else{
|
||||||
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
|
@ -376,6 +377,8 @@ class MainActivity : BaseActivity() {
|
||||||
== PackageManager.PERMISSION_GRANTED)
|
== PackageManager.PERMISSION_GRANTED)
|
||||||
}else{
|
}else{
|
||||||
(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
== PackageManager.PERMISSION_GRANTED) &&
|
||||||
|
(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||||
== PackageManager.PERMISSION_GRANTED)
|
== PackageManager.PERMISSION_GRANTED)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,12 @@ import android.os.Environment
|
||||||
import android.provider.DocumentsContract
|
import android.provider.DocumentsContract
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.webkit.MimeTypeMap
|
import android.webkit.MimeTypeMap
|
||||||
|
import androidx.core.net.toUri
|
||||||
import com.deniscerri.ytdlnis.R
|
import com.deniscerri.ytdlnis.R
|
||||||
import com.deniscerri.ytdlnis.database.models.DownloadItem
|
import com.deniscerri.ytdlnis.database.models.DownloadItem
|
||||||
import okhttp3.internal.closeQuietly
|
import okhttp3.internal.closeQuietly
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
|
@ -66,27 +68,20 @@ object FileUtil {
|
||||||
return formattedPath.toString()
|
return formattedPath.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun scanMedia(files: List<File>, context: Context) : String {
|
|
||||||
|
|
||||||
try {
|
|
||||||
val paths = files.map { it.absolutePath }.toTypedArray()
|
|
||||||
MediaScannerConnection.scanFile(context, paths, null, null)
|
|
||||||
return files.reduce(Compare::max).absolutePath
|
|
||||||
}catch (e: Exception){
|
|
||||||
e.printStackTrace()
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.getString(R.string.unfound_file);
|
|
||||||
}
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun moveFile(originDir: File, context: Context, destDir: String, keepCache: Boolean, progress: (p: Int) -> Unit) : String {
|
fun moveFile(originDir: File, context: Context, destDir: String, keepCache: Boolean, progress: (p: Int) -> Unit) : String {
|
||||||
val fileList = mutableListOf<File>()
|
val fileList = mutableListOf<File>()
|
||||||
val dir = File(formatPath(destDir))
|
val dir = File(formatPath(destDir))
|
||||||
if (!dir.exists()) dir.mkdirs()
|
if (!dir.exists()) dir.mkdirs()
|
||||||
originDir.listFiles()?.forEach {
|
var currentDirectory = dir
|
||||||
Log.e("zz", it.name)
|
originDir.walk().forEach {
|
||||||
val destFile = File(dir.absolutePath + "/${it.name}")
|
var destFile = File(dir.absolutePath + "/${it.absolutePath.removePrefix(originDir.absolutePath)}")
|
||||||
|
if (it.isDirectory) {
|
||||||
|
destFile.mkdirs()
|
||||||
|
currentDirectory = destFile
|
||||||
|
return@forEach
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if (it.name.matches("(^config.*.\\.txt\$)|(rList)|(part-Frag)".toRegex())){
|
if (it.name.matches("(^config.*.\\.txt\$)|(rList)|(part-Frag)".toRegex())){
|
||||||
it.delete()
|
it.delete()
|
||||||
|
|
@ -95,31 +90,52 @@ object FileUtil {
|
||||||
|
|
||||||
if(it.name.contains(".part-Frag")) return@forEach
|
if(it.name.contains(".part-Frag")) return@forEach
|
||||||
|
|
||||||
val mimeType =
|
//sending to main or SD CARD
|
||||||
MimeTypeMap.getSingleton().getMimeTypeFromExtension(it.extension) ?: "*/*"
|
if (currentDirectory.exists()){
|
||||||
|
val inputStream = context.contentResolver.openInputStream(it.toUri())!!
|
||||||
|
val outputStream = FileOutputStream(destFile)
|
||||||
|
|
||||||
val dest = Uri.parse(destDir).run {
|
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||||
DocumentsContract.buildDocumentUriUsingTree(
|
var bytesRead: Int
|
||||||
this,
|
val totalBytes = it.length()
|
||||||
DocumentsContract.getTreeDocumentId(this)
|
|
||||||
)
|
// Transfer the file contents
|
||||||
|
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
|
||||||
|
progress((bytesRead / totalBytes).toInt() * 100)
|
||||||
|
outputStream.write(buffer, 0, bytesRead)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputStream.close()
|
||||||
|
outputStream.closeQuietly()
|
||||||
|
}else{
|
||||||
|
//sending to USB OTG
|
||||||
|
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(it.extension) ?: "*/*"
|
||||||
|
destFile = File(currentDirectory.absolutePath + "/${it.name}")
|
||||||
|
|
||||||
|
val dest = Uri.parse(destDir).run {
|
||||||
|
DocumentsContract.buildDocumentUriUsingTree(
|
||||||
|
this,
|
||||||
|
DocumentsContract.getTreeDocumentId(this)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val destUri = DocumentsContract.createDocument(
|
||||||
|
context.contentResolver,
|
||||||
|
dest,
|
||||||
|
mimeType,
|
||||||
|
it.name
|
||||||
|
) ?: return@forEach
|
||||||
|
|
||||||
|
val inputStream = it.inputStream()
|
||||||
|
val outputStream =
|
||||||
|
context.contentResolver.openOutputStream(destUri) ?: return@forEach
|
||||||
|
inputStream.copyTo(outputStream)
|
||||||
|
inputStream.closeQuietly()
|
||||||
|
outputStream.closeQuietly()
|
||||||
}
|
}
|
||||||
val destUri = DocumentsContract.createDocument(
|
|
||||||
context.contentResolver,
|
|
||||||
dest,
|
|
||||||
mimeType,
|
|
||||||
it.name
|
|
||||||
) ?: return@forEach
|
|
||||||
|
|
||||||
val inputStream = it.inputStream()
|
|
||||||
val outputStream =
|
|
||||||
context.contentResolver.openOutputStream(destUri) ?: return@forEach
|
|
||||||
inputStream.copyTo(outputStream)
|
|
||||||
inputStream.closeQuietly()
|
|
||||||
outputStream.closeQuietly()
|
|
||||||
|
|
||||||
fileList.add(destFile)
|
fileList.add(destFile)
|
||||||
}catch (e: java.lang.Exception) {
|
}catch (e: java.lang.Exception) {
|
||||||
|
Log.e("error", e.message.toString())
|
||||||
|
|
||||||
if (destFile.absolutePath.contains("/storage/emulated/0/Download")
|
if (destFile.absolutePath.contains("/storage/emulated/0/Download")
|
||||||
|| destFile.absolutePath.contains("/storage/emulated/0/Documents")
|
|| destFile.absolutePath.contains("/storage/emulated/0/Documents")
|
||||||
|
|
@ -133,6 +149,7 @@ object FileUtil {
|
||||||
}
|
}
|
||||||
return@forEach
|
return@forEach
|
||||||
}catch(e : Exception){
|
}catch(e : Exception){
|
||||||
|
Log.e("error", e.message.toString())
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,6 +159,18 @@ object FileUtil {
|
||||||
}
|
}
|
||||||
return scanMedia(fileList, context)
|
return scanMedia(fileList, context)
|
||||||
}
|
}
|
||||||
|
private fun scanMedia(files: List<File>, context: Context) : String {
|
||||||
|
|
||||||
|
try {
|
||||||
|
val paths = files.map { it.absolutePath }.toTypedArray()
|
||||||
|
MediaScannerConnection.scanFile(context, paths, null, null)
|
||||||
|
return files.reduce(Compare::max).absolutePath
|
||||||
|
}catch (e: Exception){
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.getString(R.string.unfound_file);
|
||||||
|
}
|
||||||
|
|
||||||
fun getLogFile(context: Context, item: DownloadItem) : File {
|
fun getLogFile(context: Context, item: DownloadItem) : File {
|
||||||
val titleRegex = Regex("[^A-Za-z\\d ]")
|
val titleRegex = Regex("[^A-Za-z\\d ]")
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,7 @@
|
||||||
<item>in</item>
|
<item>in</item>
|
||||||
<item>it</item>
|
<item>it</item>
|
||||||
<item>ja</item>
|
<item>ja</item>
|
||||||
|
<item>ko</item>
|
||||||
<item>lv</item>
|
<item>lv</item>
|
||||||
<item>nb</item>
|
<item>nb</item>
|
||||||
<item>pl</item>
|
<item>pl</item>
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
<locale android:name="in"/>
|
<locale android:name="in"/>
|
||||||
<locale android:name="it"/>
|
<locale android:name="it"/>
|
||||||
<locale android:name="ja"/>
|
<locale android:name="ja"/>
|
||||||
|
<locale android:name="ko"/>
|
||||||
<locale android:name="lv"/>
|
<locale android:name="lv"/>
|
||||||
<locale android:name="nb-NO"/>
|
<locale android:name="nb-NO"/>
|
||||||
<locale android:name="pl"/>
|
<locale android:name="pl"/>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue