Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/land/oras/Layer.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ public static Layer fromData(ContainerRef containerRef, byte[] data) {
Map.of());
}

/**
* Create a layer from a compressed directory data (use gzip compression for unpacked directories)
* @param containerRef The container reference
* @param data The data
* @return The layer
*/
public static Layer fromCompressedDirectory(ContainerRef containerRef, byte[] data) {
return new Layer(
Const.DEFAULT_BLOB_DIR_MEDIA_TYPE,
containerRef.getAlgorithm().digest(data),
data.length,
Base64.getEncoder().encodeToString(data),
Map.of(Const.ANNOTATION_ORAS_UNPACK, "true"));
}

/**
* Create a layer from a digest
* @param digest The digest
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/land/oras/LayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.nio.file.Files;
import java.nio.file.Path;
import land.oras.utils.Const;
import land.oras.utils.SupportedAlgorithm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -69,6 +70,19 @@ void shouldReadLayerFromFile() throws Exception {
assertEquals(2, layer.getSize());
}

@Test
void shouldCreateLayerForCompressedDir() {
ContainerRef containerRef = ContainerRef.parse("test/container:latest");
byte[] data = "test".getBytes(); // We don't really care about the content here
Layer layer = Layer.fromCompressedDirectory(containerRef, data);
assertEquals("application/vnd.oci.image.layer.v1.tar+gzip", layer.getMediaType());
assertEquals("sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", layer.getDigest());

// Ensure unpack annotations is set
assertEquals(1, layer.getAnnotations().size());
assertEquals("true", layer.getAnnotations().get(Const.ANNOTATION_ORAS_UNPACK));
}

@Test
void shouldHaveEmptyLayer() {
String json = emptyLayer();
Expand Down