@@ -3,50 +3,39 @@ package com.coder.gateway.sdk
3
3
import com.coder.gateway.icons.CoderIcons
4
4
import com.intellij.openapi.components.Service
5
5
import 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
9
9
import java.net.URL
10
10
import javax.swing.Icon
11
11
import javax.swing.ImageIcon
12
12
13
13
@Service(Service .Level .APP )
14
14
class TemplateIconDownloader {
15
15
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 ()) {
19
19
return CoderIcons .UNKNOWN
20
20
}
21
- var byteArray: ByteArray? = null
22
21
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)
31
27
}
32
28
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
+ }
35
37
}
36
38
37
39
return CoderIcons .UNKNOWN
38
40
}
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
- }
52
41
}
0 commit comments