Fixed bug that if two bank icons have the same file name they overwrite each other

This commit is contained in:
dankito 2020-05-19 14:46:22 +02:00
parent 3d73289649
commit 68e11d08fc
1 changed files with 22 additions and 7 deletions

View File

@ -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