@@ -6,16 +6,19 @@ import com.coder.toolbox.util.OS
66import com.coder.toolbox.util.getHeaders
77import com.coder.toolbox.util.getOS
88import com.coder.toolbox.util.sha1
9+ import com.coder.toolbox.util.withLastSegment
910import okhttp3.ResponseBody
1011import retrofit2.Response
1112import java.io.FileInputStream
13+ import java.net.HttpURLConnection.HTTP_NOT_FOUND
1214import java.net.HttpURLConnection.HTTP_NOT_MODIFIED
1315import java.net.HttpURLConnection.HTTP_OK
1416import java.net.URL
1517import java.nio.file.Files
1618import java.nio.file.Path
1719import java.nio.file.StandardOpenOption
1820import java.util.zip.GZIPInputStream
21+ import kotlin.io.path.name
1922import kotlin.io.path.notExists
2023
2124/* *
@@ -32,21 +35,20 @@ class CoderDownloadService(
3235
3336 suspend fun downloadCli (buildVersion : String , showTextProgress : (String ) -> Unit ): DownloadResult {
3437 val eTag = calculateLocalETag()
35- val headers = getRequestHeaders()
3638 if (eTag != null ) {
3739 context.logger.info(" Found existing binary at $localBinaryPath ; calculated hash as $eTag " )
3840 }
3941 val response = downloadApi.downloadCli(
4042 url = remoteBinaryURL.toString(),
4143 eTag = eTag?.let { " \" $it \" " },
42- headers = headers
44+ headers = getRequestHeaders()
4345 )
4446
4547 return when (response.code()) {
4648 HTTP_OK -> {
4749 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)
5052 }
5153
5254 HTTP_NOT_MODIFIED -> {
@@ -84,16 +86,17 @@ class CoderDownloadService(
8486 }
8587 }
8688
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
9093 ): Path ? {
9194 val responseBody = this .body() ? : return null
92- Files .deleteIfExists(localBinaryPath )
93- Files .createDirectories(localBinaryPath .parent)
95+ Files .deleteIfExists(localPath )
96+ Files .createDirectories(localPath .parent)
9497
9598 val outputStream = Files .newOutputStream(
96- localBinaryPath ,
99+ localPath ,
97100 StandardOpenOption .CREATE ,
98101 StandardOpenOption .TRUNCATE_EXISTING
99102 )
@@ -108,7 +111,7 @@ class CoderDownloadService(
108111 var bytesRead: Int
109112 var totalRead = 0L
110113 // caching this because the settings store recomputes it every time
111- val binaryName = context.settingsStore.defaultCliBinaryNameByOsAndArch
114+ val binaryName = localPath.name
112115 sourceStream.use { source ->
113116 outputStream.use { sink ->
114117 while (source.read(buffer).also { bytesRead = it } != - 1 ) {
@@ -143,4 +146,39 @@ class CoderDownloadService(
143146 val gb = mb / 1024.0
144147 return String .format(" %.1f GB" , gb)
145148 }
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+ }
146184}
0 commit comments