fix: Synchronized

This commit is contained in:
rebelonion 2024-05-27 05:11:39 -05:00
parent 7b36cd0d29
commit 43e560a893

View file

@ -13,7 +13,6 @@ import ani.dantotsu.snackString
import ani.dantotsu.util.Logger
import com.anggrayudi.storage.callback.FolderCallback
import com.anggrayudi.storage.file.deleteRecursively
import com.anggrayudi.storage.file.findFolder
import com.anggrayudi.storage.file.moveFolderTo
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
@ -279,6 +278,7 @@ class DownloadsManager(private val context: Context) {
* @param type the type of media
* @return the base directory
*/
@Synchronized
private fun getBaseDirectory(context: Context, type: MediaType): DocumentFile? {
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
if (baseDirectory == Uri.EMPTY) return null
@ -307,6 +307,7 @@ class DownloadsManager(private val context: Context) {
* @param chapter the chapter of the media
* @return the subdirectory
*/
@Synchronized
fun getSubDirectory(
context: Context,
type: MediaType,
@ -344,22 +345,32 @@ class DownloadsManager(private val context: Context) {
}
}
@Synchronized
private fun getBaseDirectory(context: Context): DocumentFile? {
val baseDirectory = Uri.parse(PrefManager.getVal<String>(PrefName.DownloadsDir))
if (baseDirectory == Uri.EMPTY) return null
return DocumentFile.fromTreeUri(context, baseDirectory)
}
private val lock = Any()
private fun DocumentFile.findOrCreateFolder(
name: String, overwrite: Boolean
): DocumentFile? {
val validName = name.findValidName()
synchronized(lock) {
return if (overwrite) {
findFolder(name.findValidName())?.delete()
createDirectory(name.findValidName())
findFolder(validName)?.delete()
createDirectory(validName)
} else {
findFolder(name.findValidName()) ?: createDirectory(name.findValidName())
val folder = findFolder(validName)
return folder ?: createDirectory(validName)
}
}
}
private fun DocumentFile.findFolder(name: String): DocumentFile? =
listFiles().find { it.name == name && it.isDirectory }
private const val RATIO_THRESHOLD = 95
fun Media.compareName(name: String): Boolean {
@ -379,7 +390,7 @@ class DownloadsManager(private val context: Context) {
private const val RESERVED_CHARS = "|\\?*<\":>+[]/'"
fun String?.findValidName(): String {
return this?.replace("/","_")?.filterNot { RESERVED_CHARS.contains(it) } ?: ""
return this?.replace("/", "_")?.filterNot { RESERVED_CHARS.contains(it) } ?: ""
}
data class DownloadedType(