All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.sgtsilvio.oci.registry.DistributionRegistryStorage.kt Maven / Gradle / Ivy

Go to download

OCI registry Java library that allows serving OCI artifacts to pull operations

There is a newer version: 0.4.1
Show newest version
package io.github.sgtsilvio.oci.registry

import java.nio.file.Path
import kotlin.io.path.exists
import kotlin.io.path.readBytes

/**
 * @author Silvio Giebl
 */
class DistributionRegistryStorage(private val directory: Path) : OciRegistryStorage() {

    override fun getManifest(name: String, tag: String): Path? {
        val linkFile = resolveRepositoryDirectory(name).resolve("_manifests/tags").resolve(tag).resolve("current/link")
        return getLinkedBlob(linkFile)
    }

    override fun getManifest(name: String, digest: OciDigest): Path? {
        val linkFile = resolveRepositoryDirectory(name).resolve("_manifests/revisions").resolveLinkFile(digest)
        return getLinkedBlob(linkFile)
    }

    override fun getBlob(name: String, digest: OciDigest): Path? {
        val linkFile = resolveRepositoryDirectory(name).resolve("_layers").resolveLinkFile(digest)
        return getLinkedBlob(linkFile)
    }

    private fun getLinkedBlob(linkFile: Path): Path? {
        if (!linkFile.exists()) {
            return null
        }
        val digest = linkFile.readBytes().decodeToString().toOciDigest()
        val blobFile = directory.resolve("blobs")
            .resolve(digest.algorithm)
            .resolve(digest.hash.substring(0, 2))
            .resolve(digest.hash)
            .resolve("data")
        if (!blobFile.exists()) {
            return null
        }
        return blobFile
    }

    private fun resolveRepositoryDirectory(name: String): Path = directory.resolve("repositories").resolve(name)

    private fun Path.resolveLinkFile(digest: OciDigest): Path =
        resolve(digest.algorithm).resolve(digest.hash).resolve("link")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy