From facba4c03d7b94305c3723b7401eea6363901d24 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Thu, 13 Jun 2024 14:51:10 +0200 Subject: [PATCH] fix downloading of file with same name not working --- .../gui/projectpage/ProjectScreen.kt | 11 ++++---- .../gui/projectpage/components/VersionCard.kt | 16 +++++------- .../gui/update/components/UpdateCard.kt | 26 +++---------------- .../dev/dediamondpro/resourcify/util/Utils.kt | 20 +++++++++++++- 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/ProjectScreen.kt b/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/ProjectScreen.kt index 5e0372b..7eab6c2 100644 --- a/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/ProjectScreen.kt +++ b/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/ProjectScreen.kt @@ -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 @@ -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 } diff --git a/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/components/VersionCard.kt b/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/components/VersionCard.kt index 0c65941..f363d29 100644 --- a/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/components/VersionCard.kt +++ b/src/main/kotlin/dev/dediamondpro/resourcify/gui/projectpage/components/VersionCard.kt @@ -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 @@ -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 @@ -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 } diff --git a/src/main/kotlin/dev/dediamondpro/resourcify/gui/update/components/UpdateCard.kt b/src/main/kotlin/dev/dediamondpro/resourcify/gui/update/components/UpdateCard.kt index cfec063..5699b30 100644 --- a/src/main/kotlin/dev/dediamondpro/resourcify/gui/update/components/UpdateCard.kt +++ b/src/main/kotlin/dev/dediamondpro/resourcify/gui/update/components/UpdateCard.kt @@ -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 @@ -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 } diff --git a/src/main/kotlin/dev/dediamondpro/resourcify/util/Utils.kt b/src/main/kotlin/dev/dediamondpro/resourcify/util/Utils.kt index 389a294..59c9e19 100644 --- a/src/main/kotlin/dev/dediamondpro/resourcify/util/Utils.kt +++ b/src/main/kotlin/dev/dediamondpro/resourcify/util/Utils.kt @@ -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 @@ -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)" + } + } + } } \ No newline at end of file