chore: Got rid of fake Github error messages when attempting to download mod preview images

This commit is contained in:
yairm210 2025-07-20 16:23:59 +03:00
parent c6c820afa3
commit 286aab0cf1

View File

@ -49,11 +49,12 @@ object Github {
/** /**
* Helper opens an url and accesses its input stream, logging errors to the console * Helper opens an url and accesses its input stream, logging errors to the console
* @param url String representing a [URI] to download. * @param url String representing a [URI] to download.
* @param shouldLogError Some downloads are just us guessing where an image could be, an exception is acceptable in these circumstances
* @param preDownloadAction Optional callback that will be executed between opening the connection and * @param preDownloadAction Optional callback that will be executed between opening the connection and
* accessing its data - passes the [connection][HttpURLConnection] and allows e.g. reading the response headers. * accessing its data - passes the [connection][HttpURLConnection] and allows e.g. reading the response headers.
* @return The [InputStream] if successful, `null` otherwise. * @return The [InputStream] if successful, `null` otherwise.
*/ */
fun download(url: String, preDownloadAction: (HttpURLConnection) -> Unit = {}): InputStream? { fun download(url: String, shouldLogError: Boolean = true, preDownloadAction: (HttpURLConnection) -> Unit = {}): InputStream? {
try { try {
// Problem type 1 - opening the URL connection // Problem type 1 - opening the URL connection
// URL(string) is deprecated, URI.toUrl(string) API level 36, see [Android Doc](https://developer.android.com/reference/java/net/URI#toURL()): // URL(string) is deprecated, URI.toUrl(string) API level 36, see [Android Doc](https://developer.android.com/reference/java/net/URI#toURL()):
@ -75,7 +76,7 @@ object Github {
} catch (_: RedirectionException) { } catch (_: RedirectionException) {
return null return null
} catch (ex: Exception) { } catch (ex: Exception) {
Log.error("Exception during GitHub download", ex) if (shouldLogError) Log.error("Exception during GitHub download", ex)
return null return null
} }
} }
@ -374,12 +375,12 @@ object Github {
// Thus we ask for a "preview" file as part of the repo contents instead. // Thus we ask for a "preview" file as part of the repo contents instead.
val fileLocation = GithubAPI.getUrlForPreview(modUrl, defaultBranch) val fileLocation = GithubAPI.getUrlForPreview(modUrl, defaultBranch)
try { try {
val file = download("$fileLocation.jpg") val file = download("$fileLocation.jpg", shouldLogError = false)
?: download("$fileLocation.png") ?: download("$fileLocation.png", shouldLogError = false)
// Note: avatar urls look like: https://avatars.githubusercontent.com/u/<number>?v=4 // Note: avatar urls look like: https://avatars.githubusercontent.com/u/<number>?v=4
// So the image format is only recognizable from the response "Content-Type" header // So the image format is only recognizable from the response "Content-Type" header
// or by looking for magic markers in the bits - which the Pixmap constructor below does. // or by looking for magic markers in the bits - which the Pixmap constructor below does.
?: avatarUrl?.let { download(it) } ?: avatarUrl?.let { download(it, shouldLogError = false) }
?: return null ?: return null
val byteArray = file.readBytes() val byteArray = file.readBytes()
val buffer = ByteBuffer.allocateDirect(byteArray.size).put(byteArray).position(0) val buffer = ByteBuffer.allocateDirect(byteArray.size).put(byteArray).position(0)