Skip to content

Commit

Permalink
fix downloading of file with same name not working
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDiamondPro committed Jun 13, 2024
1 parent ccc5578 commit facba4c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of Resourcify
* Copyright (C) 2023 DeDiamondPro
* Copyright (C) 2023-2024 DeDiamondPro
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -134,10 +134,11 @@ class ProjectScreen(
if (installed || it.mouseButton != 0) return@onMouseClick
if (DownloadManager.getProgress(url) == null) {
text?.setText("${ChatColor.BOLD}${localize("resourcify.version.installing")}")
DownloadManager.download(
File(downloadFolder, version.getFileName()),
version.getSha1(), url
) {
var file = File(downloadFolder, version.getFileName())
if (file.exists()) {
file = File(downloadFolder, Utils.incrementFileName(version.getFileName()))
}
DownloadManager.download(file, version.getSha1(), url) {
text?.setText("${ChatColor.BOLD}${localize("resourcify.version.installed")}")
installed = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of Resourcify
* Copyright (C) 2023 DeDiamondPro
* Copyright (C) 2023-2024 DeDiamondPro
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -20,10 +20,7 @@ package dev.dediamondpro.resourcify.gui.projectpage.components
import dev.dediamondpro.resourcify.gui.projectpage.VersionsPage
import dev.dediamondpro.resourcify.services.IService
import dev.dediamondpro.resourcify.services.IVersion
import dev.dediamondpro.resourcify.util.DownloadManager
import dev.dediamondpro.resourcify.util.capitalizeAll
import dev.dediamondpro.resourcify.util.localize
import dev.dediamondpro.resourcify.util.toURL
import dev.dediamondpro.resourcify.util.*
import gg.essential.elementa.UIComponent
import gg.essential.elementa.components.UIBlock
import gg.essential.elementa.components.UIContainer
Expand Down Expand Up @@ -194,10 +191,11 @@ class VersionCard(
if (installed || it.mouseButton != 0) return@onMouseClick
if (DownloadManager.getProgress(url) == null) {
text?.setText("${ChatColor.BOLD}${localize("resourcify.version.installing")}")
DownloadManager.download(
File(downloadFolder, version.getFileName()),
version.getSha1(), url
) {
var file = File(downloadFolder, version.getFileName())
if (file.exists()) {
file = File(downloadFolder, Utils.incrementFileName(version.getFileName()))
}
DownloadManager.download(file, version.getSha1(), url) {
text?.setText("${ChatColor.BOLD}${localize("resourcify.version.installed")}")
installed = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,10 @@ class UpdateCard(
if (DownloadManager.getProgress(updateUrl) == null) {
gui.registerUpdate(this, Platform.getSelectedResourcePacks().contains(file))
text?.setText("${ChatColor.BOLD}${localize("resourcify.updates.updating")}")
val newFileName = if (file.name == newVersion.getFileName()) {
incrementFileName(newVersion.getFileName())
} else {
newVersion.getFileName()
var downloadFile = File(file.parentFile, newVersion.getFileName())
if (downloadFile.exists()) {
downloadFile = File(file.parentFile, Utils.incrementFileName(newVersion.getFileName()))
}
val downloadFile = File(file.parentFile, newFileName)
DownloadManager.download(
downloadFile,
newVersion.getSha1(), updateUrl
Expand Down Expand Up @@ -202,24 +200,6 @@ class UpdateCard(
}
}

private fun incrementFileName(fileName: String): String {
val regex = """\((\d+)\)(\.\w+)$""".toRegex()
val matchResult = regex.find(fileName)

return if (matchResult != null) {
val currentNumber = matchResult.groupValues[1].toInt()
val extension = matchResult.groupValues[2]
fileName.replace(regex, "(${currentNumber + 1})$extension")
} else {
val dotIndex = fileName.lastIndexOf('.')
if (dotIndex != -1) {
fileName.substring(0, dotIndex) + " (1)." + fileName.substring(dotIndex + 1)
} else {
"$fileName (1)"
}
}
}

fun getProgress(): Float {
return DownloadManager.getProgress(updateUrl) ?: 0f
}
Expand Down
20 changes: 19 additions & 1 deletion src/main/kotlin/dev/dediamondpro/resourcify/util/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of Resourcify
* Copyright (C) 2023 DeDiamondPro
* Copyright (C) 2023-2024 DeDiamondPro
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -132,4 +132,22 @@ object Utils {
val rgb = color.rgb
return Color(rgb and 16579836 shr 2 or (rgb and -16777216))
}

fun incrementFileName(fileName: String): String {
val regex = """\((\d+)\)(\.\w+)$""".toRegex()
val matchResult = regex.find(fileName)

return if (matchResult != null) {
val currentNumber = matchResult.groupValues[1].toInt()
val extension = matchResult.groupValues[2]
fileName.replace(regex, "(${currentNumber + 1})$extension")
} else {
val dotIndex = fileName.lastIndexOf('.')
if (dotIndex != -1) {
fileName.substring(0, dotIndex) + " (1)." + fileName.substring(dotIndex + 1)
} else {
"$fileName (1)"
}
}
}
}

0 comments on commit facba4c

Please sign in to comment.