From 997371d5c78cbcd33c07019ada45b52c1d68abfc Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Mon, 29 Jul 2024 12:20:32 +0200 Subject: [PATCH] Avoid that one variant references the same artifact file multiple times in OciRepositoryHandler --- .../oci/internal/dsl/OciRegistriesImpl.kt | 2 +- .../internal/registry/OciRepositoryHandler.kt | 34 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciRegistriesImpl.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciRegistriesImpl.kt index 17bbd867..e965bcdb 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciRegistriesImpl.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciRegistriesImpl.kt @@ -142,7 +142,7 @@ internal abstract class OciRegistryImpl @Inject constructor( name = this@OciRegistryImpl.name + "OciRegistry" setUrl(finalUrl.zip(registries.repositoryPort) { url, repositoryPort -> val escapedUrl = url.toString().escapePathSegment() - URI("http://localhost:$repositoryPort/v0.11/$escapedUrl") + URI("http://localhost:$repositoryPort/$OCI_REPOSITORY_VERSION/$escapedUrl") }) isAllowInsecureProtocol = true layout("gradle") diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt index b0ca592f..d183032c 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt @@ -3,8 +3,11 @@ package io.github.sgtsilvio.gradle.oci.internal.registry import com.github.benmanes.caffeine.cache.AsyncCache import com.github.benmanes.caffeine.cache.Caffeine import io.github.sgtsilvio.gradle.oci.attributes.* -import io.github.sgtsilvio.gradle.oci.internal.* import io.github.sgtsilvio.gradle.oci.internal.cache.getMono +import io.github.sgtsilvio.gradle.oci.internal.createOciMetadataClassifier +import io.github.sgtsilvio.gradle.oci.internal.createOciVariantInternalName +import io.github.sgtsilvio.gradle.oci.internal.createOciVariantName +import io.github.sgtsilvio.gradle.oci.internal.createPlatformPostfix import io.github.sgtsilvio.gradle.oci.internal.json.* import io.github.sgtsilvio.gradle.oci.internal.string.escapeReplace import io.github.sgtsilvio.gradle.oci.internal.string.unescapeReplace @@ -38,11 +41,13 @@ import java.util.function.BiFunction /v2/repository/ / // / ///<...>oci-component.json /v2/repository/ / // / ///<...>oci-layer -/v0.11/ / // / <...>.module -/v0.11/ / // / ////<...>.json -/v0.11/ / // / ///<...>oci-layer +/v0.15/ / // / <...>.module +/v0.15/ / // / ////<...>.json +/v0.15/ / // / ///<...>oci-layer */ +internal const val OCI_REPOSITORY_VERSION = "v0.15" + /** * @author Silvio Giebl */ @@ -65,7 +70,7 @@ internal class OciRepositoryHandler( override fun apply(request: HttpServerRequest, response: HttpServerResponse): Publisher { val segments = request.uri().substring(1).split('/') - if (segments[0] == "v0.11") { + if (segments[0] == OCI_REPOSITORY_VERSION) { return handleRepository(request, segments.drop(1), response) } return response.sendNotFound() @@ -176,6 +181,7 @@ internal class OciRepositoryHandler( addString("md5", DigestUtils.md5Hex(metadataJson)) } val escapedImageName = metadata.imageReference.name.escapePathSegment() + val digestCounts = HashMap() for (layerMetadata in metadata.layers) { layerMetadata.descriptor?.let { layerDescriptor -> addObject { @@ -183,10 +189,20 @@ internal class OciRepositoryHandler( val algorithmId = layerDigest.algorithm.id val encodedHash = layerDigest.encodedHash val layerSize = layerDescriptor.size - val layerName = fileNamePrefix + createOciLayerClassifier( - "main", - algorithmId + '!' + encodedHash.take(5) + ".." + encodedHash.takeLast(5), - ) + val count = digestCounts[layerDigest] ?: 0 + digestCounts[layerDigest] = count + 1 + val layerName = buildString { + append(fileNamePrefix) + append(algorithmId) + append('!') + append(encodedHash.take(5)) + append("..") + append(encodedHash.takeLast(5)) + if (count != 0) { + append("-dup").append(count) + } + append("-oci-layer") + } addString("name", layerName + mapLayerMediaTypeToExtension(layerDescriptor.mediaType)) addString("url", "$escapedImageName/$layerDigest/$layerSize/$layerName") addNumber("size", layerSize)