fixed creating dir bug

This commit is contained in:
Blatzar 2021-11-04 16:11:28 +01:00
parent a1985a10b1
commit 8d772b8ad0
2 changed files with 26 additions and 6 deletions

View file

@ -5,6 +5,7 @@ import android.content.Context
import androidx.work.CoroutineWorker import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.removeKey import com.lagradost.cloudstream3.utils.DataStore.removeKey
@ -33,7 +34,7 @@ class DownloadFileWorkManager(val context: Context, private val workerParams: Wo
val info = applicationContext.getKey<VideoDownloadManager.DownloadInfo>(WORK_KEY_INFO, key) val info = applicationContext.getKey<VideoDownloadManager.DownloadInfo>(WORK_KEY_INFO, key)
val pkg = val pkg =
applicationContext.getKey<VideoDownloadManager.DownloadResumePackage>(WORK_KEY_PACKAGE, key) applicationContext.getKey<VideoDownloadManager.DownloadResumePackage>(WORK_KEY_PACKAGE, key)
println("INFO $info PKG ::: $pkg")
if (info != null) { if (info != null) {
downloadEpisode( downloadEpisode(
applicationContext, applicationContext,
@ -44,6 +45,7 @@ class DownloadFileWorkManager(val context: Context, private val workerParams: Wo
::handleNotification ::handleNotification
) )
awaitDownload(info.ep.id) awaitDownload(info.ep.id)
} else if (pkg != null) { } else if (pkg != null) {
downloadFromResume(applicationContext, pkg, ::handleNotification) downloadFromResume(applicationContext, pkg, ::handleNotification)
awaitDownload(pkg.item.ep.id) awaitDownload(pkg.item.ep.id)
@ -52,6 +54,7 @@ class DownloadFileWorkManager(val context: Context, private val workerParams: Wo
} }
return Result.success() return Result.success()
} catch (e: Exception) { } catch (e: Exception) {
logError(e)
if (key != null) { if (key != null) {
removeKeys(key) removeKeys(key)
} }
@ -79,6 +82,7 @@ class DownloadFileWorkManager(val context: Context, private val workerParams: Wo
} }
downloadStatusEvent += listener downloadStatusEvent += listener
while (!isDone) { while (!isDone) {
println("AWAITING $id")
delay(1000) delay(1000)
} }
downloadStatusEvent -= listener downloadStatusEvent -= listener

View file

@ -217,6 +217,7 @@ object VideoDownloadManager {
} }
return null return null
} catch (e: Exception) { } catch (e: Exception) {
logError(e)
return null return null
} }
} }
@ -440,6 +441,7 @@ object VideoDownloadManager {
} }
return list return list
} catch (e: Exception) { } catch (e: Exception) {
logError(e)
return null return null
} }
} }
@ -502,6 +504,7 @@ object VideoDownloadManager {
} }
return null return null
} catch (e: Exception) { } catch (e: Exception) {
logError(e)
return null return null
} }
} }
@ -512,6 +515,7 @@ object VideoDownloadManager {
this.openFileDescriptor(fileUri, "r") this.openFileDescriptor(fileUri, "r")
.use { it?.statSize ?: 0 } .use { it?.statSize ?: 0 }
} catch (e: Exception) { } catch (e: Exception) {
logError(e)
null null
} }
} }
@ -600,16 +604,17 @@ object VideoDownloadManager {
} else { } else {
val subDir = baseFile.first?.gotoDir(folder) val subDir = baseFile.first?.gotoDir(folder)
val rFile = subDir?.findFile(displayName) val rFile = subDir?.findFile(displayName)
println("RFILE ${baseFile.first?.filePath} $folder $subDir")
if (rFile?.exists() != true) { if (rFile?.exists() != true) {
fileLength = 0 fileLength = 0
if (subDir?.createFile(displayName) == null) return StreamData(ERROR_CONTENT_RESOLVER_NOT_FOUND) if (subDir?.createFile(displayName) == null) return StreamData(ERROR_CREATE_FILE)
} else { } else {
if (resume) { if (resume) {
fileLength = rFile.size() fileLength = rFile.size()
} else { } else {
fileLength = 0 fileLength = 0
if (!rFile.delete()) return StreamData(ERROR_CONTENT_RESOLVER_NOT_FOUND) if (!rFile.delete()) return StreamData(ERROR_DELETING_FILE)
if (subDir.createFile(displayName) == null) return StreamData(ERROR_CONTENT_RESOLVER_NOT_FOUND) if (subDir.createFile(displayName) == null) return StreamData(ERROR_CREATE_FILE)
} }
} }
fileStream = (subDir.findFile(displayName) ?: subDir.createFile(displayName))!!.openOutputStream() fileStream = (subDir.findFile(displayName) ?: subDir.createFile(displayName))!!.openOutputStream()
@ -813,6 +818,7 @@ object VideoDownloadManager {
fileStream.write(buffer, 0, count) fileStream.write(buffer, 0, count)
} }
} catch (e: Exception) { } catch (e: Exception) {
logError(e)
isFailed = true isFailed = true
updateNotification() updateNotification()
} }
@ -861,6 +867,7 @@ object VideoDownloadManager {
* Guarantees a directory is present with the dir name (if createMissingDirectories is true). * Guarantees a directory is present with the dir name (if createMissingDirectories is true).
* Works recursively when '/' is present. * Works recursively when '/' is present.
* Will remove any file with the dir name if present and add directory. * Will remove any file with the dir name if present and add directory.
* Will not work if the parent directory does not exist.
* *
* @param directoryName if null will use the current path. * @param directoryName if null will use the current path.
* @return UniFile / null if createMissingDirectories = false and folder is not found. * @return UniFile / null if createMissingDirectories = false and folder is not found.
@ -876,6 +883,15 @@ object VideoDownloadManager {
// println("Going to dir $directoryName from ${this.uri} ---- ${this.filePath}") // println("Going to dir $directoryName from ${this.uri} ---- ${this.filePath}")
try { try {
// Creates itself from parent if doesn't exist.
if (!this.exists() && createMissingDirectories && !this.name.isNullOrBlank()) {
if (this.parentFile != null) {
this.parentFile?.createDirectory(this.name)
} else if (this.filePath != null) {
UniFile.fromFile(File(this.filePath!!).parentFile)?.createDirectory(this.name)
}
}
val allDirectories = directoryName?.split("/") val allDirectories = directoryName?.split("/")
return if (allDirectories?.size == 1 || allDirectories == null) { return if (allDirectories?.size == 1 || allDirectories == null) {
val found = this.findFile(directoryName) val found = this.findFile(directoryName)
@ -1284,7 +1300,6 @@ object VideoDownloadManager {
) )
} }
} }
} ?: ERROR_UNKNOWN } ?: ERROR_UNKNOWN
} }
@ -1320,7 +1335,7 @@ object VideoDownloadManager {
link, link,
notificationCallback, notificationCallback,
resume resume
) ).also { println("Single episode finished with return code: $it") }
} }
} }
if (connectionResult != null && connectionResult > 0) { // SUCCESS if (connectionResult != null && connectionResult > 0) { // SUCCESS
@ -1410,6 +1425,7 @@ object VideoDownloadManager {
return try { return try {
file.delete() file.delete()
} catch (e: Exception) { } catch (e: Exception) {
logError(e)
val cr = context.contentResolver val cr = context.contentResolver
cr.delete(file.uri, null, null) > 0 cr.delete(file.uri, null, null) > 0
} }