From 68e11d08fc8b3d5a557faaf09350d98d1ca0d1b2 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 19 May 2020 14:46:22 +0200 Subject: [PATCH] Fixed bug that if two bank icons have the same file name they overwrite each other --- .../banking/ui/presenter/BankingPresenter.kt | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt index 59b5ab9a..d44a76fa 100644 --- a/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/main/java/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -181,7 +181,7 @@ open class BankingPresenter( try { bankIconFinder.findIconForBank(bank.name)?.let { bankIconUrl -> - val bankIconFile = saveBankIconToDisk(bankIconUrl) + val bankIconFile = saveBankIconToDisk(bank, bankIconUrl) bank.iconUrl = "file://" + bankIconFile.absolutePath // without 'file://' Android will not find it @@ -194,15 +194,12 @@ open class BankingPresenter( } } - private fun saveBankIconToDisk(bankIconUrl: String): File { + protected open fun saveBankIconToDisk(bank: Bank, bankIconUrl: String): File { val bankIconsDir = File(dataFolder, "bank_icons") bankIconsDir.mkdirs() - var iconFilename = File(bankIconUrl).name - if (iconFilename.contains('?')) { - iconFilename = iconFilename.substring(0, iconFilename.indexOf('?')) - } - val bankIconFile = File(bankIconsDir, URLEncoder.encode(iconFilename, Charsets.US_ASCII.name())) + val extension = getIconFileExtension(bankIconUrl) + val bankIconFile = File(bankIconsDir, bank.bankCode + if (extension != null) (".$extension") else "") URL(bankIconUrl).openConnection().getInputStream().buffered().use { iconInputStream -> FileOutputStream(bankIconFile).use { fileOutputStream -> @@ -213,6 +210,24 @@ open class BankingPresenter( return bankIconFile } + protected open fun getIconFileExtension(bankIconUrl: String): String? { + try { + var iconFilename = File(bankIconUrl).name + if (iconFilename.contains('?')) { + iconFilename = iconFilename.substring(0, iconFilename.indexOf('?')) + } + + val extension = File(iconFilename).extension + if (extension.isNotBlank()) { + return extension + } + } catch (e: Exception) { + log.info("Could not get icon file extension from url '$bankIconUrl'", e) + } + + return null + } + open fun deleteAccount(account: Account) { val wasSelected = isSingleSelectedAccount(account) or // either account or one of its bank accounts is currently selected