@@ -6,16 +6,19 @@ import com.coder.toolbox.util.OS
6
6
import com.coder.toolbox.util.getHeaders
7
7
import com.coder.toolbox.util.getOS
8
8
import com.coder.toolbox.util.sha1
9
+ import com.coder.toolbox.util.withLastSegment
9
10
import okhttp3.ResponseBody
10
11
import retrofit2.Response
11
12
import java.io.FileInputStream
13
+ import java.net.HttpURLConnection.HTTP_NOT_FOUND
12
14
import java.net.HttpURLConnection.HTTP_NOT_MODIFIED
13
15
import java.net.HttpURLConnection.HTTP_OK
14
16
import java.net.URL
15
17
import java.nio.file.Files
16
18
import java.nio.file.Path
17
19
import java.nio.file.StandardOpenOption
18
20
import java.util.zip.GZIPInputStream
21
+ import kotlin.io.path.name
19
22
import kotlin.io.path.notExists
20
23
21
24
/* *
@@ -32,21 +35,20 @@ class CoderDownloadService(
32
35
33
36
suspend fun downloadCli (buildVersion : String , showTextProgress : (String ) -> Unit ): DownloadResult {
34
37
val eTag = calculateLocalETag()
35
- val headers = getRequestHeaders()
36
38
if (eTag != null ) {
37
39
context.logger.info(" Found existing binary at $localBinaryPath ; calculated hash as $eTag " )
38
40
}
39
41
val response = downloadApi.downloadCli(
40
42
url = remoteBinaryURL.toString(),
41
43
eTag = eTag?.let { " \" $it \" " },
42
- headers = headers
44
+ headers = getRequestHeaders()
43
45
)
44
46
45
47
return when (response.code()) {
46
48
HTTP_OK -> {
47
49
context.logger.info(" Downloading binary to $localBinaryPath " )
48
- response.saveBinaryToDisk(buildVersion , showTextProgress)?.makeExecutable()
49
- DownloadResult .Downloaded
50
+ response.saveToDisk(localBinaryPath , showTextProgress, buildVersion )?.makeExecutable()
51
+ DownloadResult .Downloaded (localBinaryPath)
50
52
}
51
53
52
54
HTTP_NOT_MODIFIED -> {
@@ -84,16 +86,17 @@ class CoderDownloadService(
84
86
}
85
87
}
86
88
87
- private fun Response<ResponseBody>.saveBinaryToDisk (
88
- buildVersion : String ,
89
- showTextProgress : (String ) -> Unit
89
+ private fun Response<ResponseBody>.saveToDisk (
90
+ localPath : Path ,
91
+ showTextProgress : (String ) -> Unit ,
92
+ buildVersion : String? = null
90
93
): Path ? {
91
94
val responseBody = this .body() ? : return null
92
- Files .deleteIfExists(localBinaryPath )
93
- Files .createDirectories(localBinaryPath .parent)
95
+ Files .deleteIfExists(localPath )
96
+ Files .createDirectories(localPath .parent)
94
97
95
98
val outputStream = Files .newOutputStream(
96
- localBinaryPath ,
99
+ localPath ,
97
100
StandardOpenOption .CREATE ,
98
101
StandardOpenOption .TRUNCATE_EXISTING
99
102
)
@@ -108,7 +111,7 @@ class CoderDownloadService(
108
111
var bytesRead: Int
109
112
var totalRead = 0L
110
113
// caching this because the settings store recomputes it every time
111
- val binaryName = context.settingsStore.defaultCliBinaryNameByOsAndArch
114
+ val binaryName = localPath.name
112
115
sourceStream.use { source ->
113
116
outputStream.use { sink ->
114
117
while (source.read(buffer).also { bytesRead = it } != - 1 ) {
@@ -143,4 +146,39 @@ class CoderDownloadService(
143
146
val gb = mb / 1024.0
144
147
return String .format(" %.1f GB" , gb)
145
148
}
149
+
150
+ suspend fun downloadSignature (showTextProgress : (String ) -> Unit ): DownloadResult {
151
+ val defaultCliNameWithoutExt = context.settingsStore.defaultCliBinaryNameByOsAndArch.split(' .' ).first()
152
+ val signatureName = " $defaultCliNameWithoutExt .asc"
153
+
154
+ val signatureURL = remoteBinaryURL.withLastSegment(signatureName)
155
+ val localSignaturePath = localBinaryPath.parent.resolve(signatureName)
156
+ context.logger.info(" Downloading signature from $signatureURL " )
157
+
158
+ val response = downloadApi.downloadSignature(
159
+ url = signatureURL.toString(),
160
+ headers = getRequestHeaders()
161
+ )
162
+
163
+ return when (response.code()) {
164
+ HTTP_OK -> {
165
+ response.saveToDisk(localSignaturePath, showTextProgress)
166
+ DownloadResult .Downloaded (localSignaturePath)
167
+ }
168
+
169
+ HTTP_NOT_FOUND -> {
170
+ context.logger.warn(" Signature file not found at $signatureURL " )
171
+ DownloadResult .NotFound
172
+ }
173
+
174
+ else -> {
175
+ DownloadResult .Failed (
176
+ ResponseException (
177
+ " Failed to download signature from $signatureURL " ,
178
+ response.code()
179
+ )
180
+ )
181
+ }
182
+ }
183
+ }
146
184
}
0 commit comments