Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 35 additions & 40 deletions buildSrc/src/main/java/DownloadBrandLogosTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,47 @@ open class DownloadBrandLogosTask : DefaultTask() {
/* NSI uses (atm) a slightly different format than the normal presets: The presets are in
a sub-object called "presets" */
val presets = nsiPresetsJson.obj("presets")!!
for (entry in presets.entries) {
val id = entry.key
val presetJson = entry.value as JsonObject
val imageURL = presetJson["imageURL"] as String?
if (imageURL != null) {
val smallImageUrl = when {
imageURL.contains("graph.facebook.com") && imageURL.endsWith("large") -> {
// small is about 50x50px. large would be 200x200px
imageURL.replaceRange(imageURL.length - 5, imageURL.length, "small")
}
imageURL.contains("commons.wikimedia.org") && imageURL.endsWith("width=100") -> {
imageURL.replaceRange(imageURL.length - 3, imageURL.length, "50")
}
imageURL.contains("pbs.twimg.com/profile_images") -> {
// normal is 48x48px, bigger is about 128x128px
imageURL.replace("_bigger.", "_normal.")
}
else -> {
imageURL
}
}
try {
val conn = URL(smallImageUrl)
val suffix = when (conn.openConnection().contentType) {
"image/jpeg" -> "jpg"
"image/png" -> "png"
"image/webp" -> "webp"
else -> throw UnsupportedFormatException("only png, jpg, webp are supported")
}
for ((id, value) in presets.entries) {
val imageURL = (value as JsonObject)["imageURL"] as String?
if (imageURL != null) downloadLogo(id, imageURL, targetDir)
}
}

val targetFile = File("$targetDir/$id.$suffix")
targetFile.parentFile.mkdirs()
private fun downloadLogo(id: String, imageURL: String, targetDir: String) {
val smallImageUrl = computeSmallImageUrl(imageURL)
try {
val conn = URL(smallImageUrl)
val suffix = when (conn.openConnection().contentType) {
"image/jpeg" -> "jpg"
"image/png" -> "png"
"image/webp" -> "webp"
else -> throw UnsupportedFormatException("only png, jpg, webp are supported")
}

conn.openStream().use { input ->
FileOutputStream(targetFile).use { output ->
input.copyTo(output)
}
}
} catch (e: FileNotFoundException) {
println("$id: $imageURL not found")
} catch (e: UnsupportedFormatException) {
println("$id: $imageURL unsupported format")
val targetFile = File("$targetDir/$id.$suffix")
targetFile.parentFile.mkdirs()

conn.openStream().use { input ->
FileOutputStream(targetFile).use { output ->
input.copyTo(output)
}
}
} catch (e: FileNotFoundException) {
println("$id: $imageURL not found")
} catch (e: UnsupportedFormatException) {
println("$id: $imageURL unsupported format")
}
}

private fun computeSmallImageUrl(imageURL: String): String = when {
imageURL.contains("graph.facebook.com") && imageURL.endsWith("large") ->
imageURL.replaceRange(imageURL.length - 5, imageURL.length, "small") // small ~50x50px
imageURL.contains("commons.wikimedia.org") && imageURL.endsWith("width=100") ->
imageURL.replaceRange(imageURL.length - 3, imageURL.length, "50")
imageURL.contains("pbs.twimg.com/profile_images") ->
imageURL.replace("_bigger.", "_normal.") // normal ~48x48px
else -> imageURL
}
}

class UnsupportedFormatException(reason: String) : Exception(reason)