Skip to content

Commit

Permalink
fix scanlator name removing
Browse files Browse the repository at this point in the history
  • Loading branch information
axiel7 committed Apr 18, 2024
1 parent c1abf27 commit 1f2e82c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions app/src/main/java/com/axiel7/tachisync/utils/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.axiel7.tachisync.App

object FileUtils {

private val chapterRegex = Regex("(#\\d*)")

@Composable
fun rememberUriLauncher(onUriReceived: (Uri) -> Unit) = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult()
Expand Down Expand Up @@ -62,7 +64,7 @@ object FileUtils {

// Copy each file or directory to the destination directory
sourceDir.listFiles().forEach { file ->
if (file.name != null) {
file.name?.let { filename ->
if (file.isDirectory) {
// If the file is a directory, recursively call this function
val childSourceDir = sourceDir.findFile(file.name!!)
Expand All @@ -81,17 +83,23 @@ object FileUtils {
currentFileCount.intValue += 1
// If the file is a regular file, copy its contents to the destination file
// Check if the file already exist
var destFile = destDir?.findFile(file.name!!)
var destFile = destDir?.findFile(filename)
if (destFile == null && file.type != null) {
destFile = destDir?.createFile(file.type!!, file.name!!)
}
if (shouldRemoveScanlator) {
val nameWithoutScanlator = destFile?.name
?.replaceBefore('_', "")
?.drop(1)
if (nameWithoutScanlator != null) {
destFile?.renameTo(nameWithoutScanlator)
if (shouldRemoveScanlator && filename.contains('_')) {
val chapterNumber = chapterRegex.find(filename)?.value
val nameWithoutScanlator = filename
.replaceBefore('_', "")
.drop(1)
val finalName = if (chapterNumber != null
&& !nameWithoutScanlator.contains(chapterNumber)
) {
"$chapterNumber $nameWithoutScanlator"
} else {
nameWithoutScanlator
}
destFile?.renameTo(finalName)
}
if (destFile?.uri != null) {
val inputStream = contentResolver.openInputStream(file.uri)
Expand Down

0 comments on commit 1f2e82c

Please sign in to comment.