Skip to content

Commit

Permalink
Better support for the image property
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesJander committed Jan 16, 2024
1 parent d9ab0a4 commit 0369c35
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.frictionlessdata</groupId>
<artifactId>datapackage-java</artifactId>
<version>0.6.14-SNAPSHOT</version>
<version>0.6.16-SNAPSHOT</version>
<packaging>jar</packaging>
<issueManagement>
<url>https://github.com/frictionlessdata/datapackage-java/issues</url>
Expand All @@ -22,9 +22,9 @@
<java.version>8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<tableschema-java-version>0.6.15</tableschema-java-version>
<tableschema-java-version>0.6.16</tableschema-java-version>
<hamcrest.version>1.3</hamcrest.version>
<junit.version>5.9.1</junit.version>
<junit.version>5.9.2</junit.version>
<slf4j-simple.version>2.0.5</slf4j-simple.version>
<apache-commons-collections4.version>4.4</apache-commons-collections4.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/frictionlessdata/datapackage/JSONBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,24 @@ protected static String getZipFileContentAsString(Path inFilePath, String fileNa
return content;
}

protected static byte[] getZipFileContentAsByteArray(Path inFilePath, String fileName) throws IOException {
// Read in memory the file inside the zip.
ZipFile zipFile = new ZipFile(inFilePath.toFile());
ZipEntry entry = findZipEntry(zipFile, fileName);

// Throw exception if expected datapackage.json file not found.
if(entry == null){
throw new DataPackageException("The zip file does not contain the expected file: " + fileName);
}
try (InputStream inputStream = zipFile.getInputStream(entry);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
for (int b; (b = inputStream.read()) != -1; ) {
out.write(b);
}
return out.toByteArray();
}
}

public static ObjectNode dereference(File fileObj, Path basePath, boolean isArchivePackage) throws IOException {
String jsonContentString;
if (isArchivePackage) {
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/io/frictionlessdata/datapackage/Package.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -286,18 +287,36 @@ public URL getHomepage() {
*
* @return path or URL to the image data
*/
@JsonProperty("image")
public String getImagePath() {
return image;
}

/**
* Returns the image data if the image is stored inside the data package, null if {@link #getImagePath()}
* would return a URLL
* would return a URL
*
* @return binary image data
*/
public byte[] getImage() {
return imageData;
@JsonIgnore
public byte[] getImage() throws IOException {
if (null != imageData)
return imageData;
if (!StringUtils.isEmpty(image)) {
if (isArchivePackage) {
return getZipFileContentAsByteArray((Path)basePath, image);
} else {
File imgFile = new File (((Path)basePath).toFile(), image);
try (InputStream inputStream = new FileInputStream(imgFile);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
for (int b; (b = inputStream.read()) != -1; ) {
out.write(b);
}
return out.toByteArray();
}
}
}
return null;
}

public ZonedDateTime getCreated() {
Expand Down Expand Up @@ -382,6 +401,7 @@ public Object getProperty(String key, Class<?> clazz) {
* an invalid Package would throw an exception.
* @return true if either `strictValidation` is true or no errors were encountered
*/
@JsonIgnore
public boolean isValid() {
if (strictValidation){
return true;
Expand Down Expand Up @@ -837,7 +857,8 @@ private void setImagePath(String image) {
}

public void setImage(String fileName, byte[]data) throws IOException {
this.image = fileName;
String sanitizedFileName = fileName.replaceAll("[\\s/\\\\]+", "_");
this.image = sanitizedFileName;
this.imageData = data;
}

Expand Down Expand Up @@ -962,7 +983,6 @@ private void writeDescriptor (FileSystem outFs, String parentDirName) throws IOE
Path nf = outFs.getPath(parentDirName+File.separator+DATAPACKAGE_FILENAME);
try (Writer writer = Files.newBufferedWriter(nf, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
ObjectNode jsonNode = this.getJsonNode();
jsonNode.remove(JSON_KEY_IMAGE);
writer.write(jsonNode.toPrettyString());
}
}
Expand Down
43 changes: 41 additions & 2 deletions src/test/java/io/frictionlessdata/datapackage/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.frictionlessdata.tableschema.schema.Schema;
import io.frictionlessdata.tableschema.tabledatasource.TableDataSource;
import io.frictionlessdata.tableschema.util.JsonUtil;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -445,6 +444,46 @@ public void testSetProfile() throws Exception {
Assertions.assertEquals(Profile.PROFILE_DATA_PACKAGE_DEFAULT, dp.getProfile());
}

@Test
@DisplayName("Test setting the 'image' propertye")
public void testSetImage() throws Exception {
Path tempDirPath = Files.createTempDirectory("datapackage-");
String fName = "/fixtures/datapackages/with-image";
Path resourcePath = TestUtil.getResourcePath(fName);
Package dp = new Package(resourcePath, true);

byte[] imageData = TestUtil.getResourceContent("/fixtures/files/test.png");
dp.setImage("test.png", imageData);

File tmpFile = new File(tempDirPath.toFile(), "saved-pkg.zip");
dp.write(tmpFile, true);

// Read the datapckage we just saved in the temp dir.
Package readPackage = new Package(tmpFile.toPath(), false);
byte[] readImageData = readPackage.getImage();
Assertions.assertArrayEquals(imageData, readImageData);
}

@Test
@DisplayName("Test setting the 'image' property, ZIP file")
public void testSetImageZip() throws Exception {
Path tempDirPath = Files.createTempDirectory("datapackage-");
String fName = "/fixtures/datapackages/employees/datapackage.json";
Path resourcePath = TestUtil.getResourcePath(fName);
Package dp = new Package(resourcePath, true);

byte[] imageData = TestUtil.getResourceContent("/fixtures/files/test.png");
dp.setImage("test.png", imageData);

File tmpFile = new File(tempDirPath.toFile(), "saved-pkg.zip");
dp.write(tmpFile, true);

// Read the datapckage we just saved in the zip file.
Package readPackage = new Package(tmpFile.toPath(), false);
byte[] readImageData = readPackage.getImage();
Assertions.assertArrayEquals(imageData, readImageData);
}

@Test
@DisplayName("Test setting invalid 'profile' property, must throw")
public void testSetInvalidProfile() throws Exception {
Expand Down Expand Up @@ -633,7 +672,7 @@ public void testWriteImageToFolderPackage() throws Exception{
System.out.println(tempDirPath);
File descriptor = new File (dir, "datapackage.json");
String json = String.join("\n", Files.readAllLines(descriptor.toPath()));
Assertions.assertFalse(json.contains("\"image\""));
Assertions.assertFalse(json.contains("\"imageData\""));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,name,dateOfBirth,isAdmin,addressCoordinates,contractLength,info
1,John Doe,1976-01-13,true,"{""lon"": 90, ""lat"": 45}",P2DT3H4M,"{""ssn"": 90, ""pin"": 45, ""rate"": 83.23}"
2,Frank McKrank,1992-02-14,false,"{""lon"": 90, ""lat"": 45}",PT15M,"{""ssn"": 90, ""pin"": 45, ""rate"": 83.23}"
3,Pencil Vester,1983-03-16,false,"{""lon"": 90, ""lat"": 45}",PT20.345S,"{""ssn"": 90, ""pin"": 45, ""rate"": 83.23}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "employees",
"profile": "tabular-data-package",
"image" : "test.png",
"resources": [{
"name": "employee-data",
"path": "data/employees.csv",
"schema": "schema.json",
"profile": "tabular-data-resource"
}
]
}
39 changes: 39 additions & 0 deletions src/test/resources/fixtures/datapackages/with-image/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"fields":[
{
"name":"id",
"format":"default",
"type":"integer"
},
{
"name":"name",
"format":"default",
"type":"string"
},
{
"name":"dateOfBirth",
"format":"default",
"type":"date"
},
{
"name":"isAdmin",
"format":"default",
"type":"boolean"
},
{
"name":"addressCoordinates",
"format":"object",
"type":"geopoint"
},
{
"name":"contractLength",
"format":"default",
"type":"duration"
},
{
"name":"info",
"format":"default",
"type":"object"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/fixtures/files/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0369c35

Please sign in to comment.