@@ -3,50 +3,39 @@ package com.coder.gateway.sdk
33import com.coder.gateway.icons.CoderIcons
44import com.intellij.openapi.components.Service
55import com.intellij.openapi.components.service
6- import com.intellij.util.IconUtil
7- import okhttp3.OkHttpClient
8- import okhttp3.Request
6+ import com.intellij.ui.scale.ScaleContext
7+ import com.intellij.util.ImageLoader
8+ import com.intellij.util.ui.ImageUtil
99import java.net.URL
1010import javax.swing.Icon
1111import javax.swing.ImageIcon
1212
1313@Service(Service .Level .APP )
1414class TemplateIconDownloader {
1515 private val coderClient: CoderRestClientService = service()
16- private val httpClient = OkHttpClient . Builder ().build()
17- fun load (url : String , templateName : String ): Icon {
18- if (url.isNullOrBlank ()) {
16+
17+ fun load (path : String , templateName : String ): Icon {
18+ if (path.isBlank ()) {
1919 return CoderIcons .UNKNOWN
2020 }
21- var byteArray: ByteArray? = null
2221
23- if (url.startsWith(" http" )) {
24- byteArray = if (url.contains(coderClient.coderURL.host)) {
25- coderClient.getImageIcon(url.toURL())
26- } else {
27- getExternalImageIcon(url.toURL())
28- }
29- } else if (url.contains(coderClient.coderURL.host)) {
30- byteArray = coderClient.getImageIcon(coderClient.coderURL.withPath(url))
22+ var url: URL ? = null
23+ if (path.startsWith(" http" )) {
24+ url = path.toURL()
25+ } else if (path.contains(coderClient.coderURL.host)) {
26+ url = coderClient.coderURL.withPath(path)
3127 }
3228
33- if (byteArray != null ) {
34- return IconUtil .resizeSquared(ImageIcon (byteArray), 32 )
29+ if (url != null ) {
30+ var img = ImageLoader .loadFromUrl(url)
31+ if (img != null ) {
32+ if (ImageUtil .getRealHeight(img) > 32 && ImageUtil .getRealWidth(img) > 32 ) {
33+ img = ImageUtil .resize(img, 32 , ScaleContext .create())
34+ }
35+ return ImageIcon (img)
36+ }
3537 }
3638
3739 return CoderIcons .UNKNOWN
3840 }
39-
40- private fun getExternalImageIcon (url : URL ): ByteArray? {
41- val request = Request .Builder ().url(url).build()
42- httpClient.newCall(request).execute().use { response ->
43- if (! response.isSuccessful) {
44- return null
45- }
46-
47- response.body!! .byteStream().use {
48- return it.readAllBytes()
49- }
50- }
51- }
5241}
0 commit comments