diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringBase64ImageConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringBase64ImageConverter.java new file mode 100644 index 000000000..5281e2672 --- /dev/null +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringBase64ImageConverter.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fesod.sheet.converters.string; + +import java.io.IOException; +import java.util.Base64; +import org.apache.fesod.sheet.converters.Converter; +import org.apache.fesod.sheet.metadata.GlobalConfiguration; +import org.apache.fesod.sheet.metadata.data.WriteCellData; +import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; + +/** + * Base64 and image converter (support base64 with prefix). + */ +public class StringBase64ImageConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return String.class; + } + + @Override + public WriteCellData convertToExcelData( + String base64, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) + throws IOException { + String value; + int commaIndex = base64.indexOf(","); + if (commaIndex != -1) { + value = base64.substring(commaIndex + 1); + } else { + value = base64; + } + + return new WriteCellData<>(Base64.getDecoder().decode(value)); + } +} diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringImageConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringImageConverter.java index 35baff451..06f426a33 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringImageConverter.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringImageConverter.java @@ -36,8 +36,9 @@ /** * String and image converter * - * + * @see StringPathnameImageConverter */ +@Deprecated public class StringImageConverter implements Converter { @Override public Class supportJavaTypeKey() { diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringPathnameImageConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringPathnameImageConverter.java new file mode 100644 index 000000000..02cb52bf9 --- /dev/null +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringPathnameImageConverter.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fesod.sheet.converters.string; + +import java.io.File; +import java.io.IOException; +import org.apache.fesod.sheet.converters.Converter; +import org.apache.fesod.sheet.metadata.GlobalConfiguration; +import org.apache.fesod.sheet.metadata.data.WriteCellData; +import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; +import org.apache.fesod.sheet.util.FileUtils; + +/** + * Pathname and image converter. + */ +public class StringPathnameImageConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return String.class; + } + + @Override + public WriteCellData convertToExcelData( + String pathname, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) + throws IOException { + return new WriteCellData<>(FileUtils.readFileToByteArray(new File(pathname))); + } +} diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringBase64ImageConverterTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringBase64ImageConverterTest.java new file mode 100644 index 000000000..685916d28 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringBase64ImageConverterTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fesod.sheet.converter; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Base64; +import org.apache.commons.io.IOUtils; +import org.apache.fesod.sheet.converters.string.StringBase64ImageConverter; +import org.apache.fesod.sheet.metadata.GlobalConfiguration; +import org.apache.fesod.sheet.metadata.data.WriteCellData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link StringBase64ImageConverter}. + */ +class StringBase64ImageConverterTest { + + private final StringBase64ImageConverter converter = new StringBase64ImageConverter(); + private byte[] imageBytes; + + @BeforeEach + void setup() throws IOException { + try (InputStream is = getClass().getClassLoader().getResourceAsStream("converter/img.jpg")) { + imageBytes = IOUtils.toByteArray(is); + } + } + + @Test + void test_supportJavaTypeKey() { + Assertions.assertEquals(String.class, converter.supportJavaTypeKey()); + } + + @Test + void test_convertToExcelData() throws IOException { + String base64 = Base64.getEncoder().encodeToString(imageBytes); + + WriteCellData cellData = converter.convertToExcelData(base64, null, new GlobalConfiguration()); + + Assertions.assertArrayEquals( + imageBytes, cellData.getImageDataList().get(0).getImage()); + } + + @Test + void test_convertToExcelData_withBase64Prefix() throws IOException { + String base64 = "data:image/jpg;base64," + Base64.getEncoder().encodeToString(imageBytes); + + WriteCellData cellData = converter.convertToExcelData(base64, null, new GlobalConfiguration()); + + Assertions.assertArrayEquals( + imageBytes, cellData.getImageDataList().get(0).getImage()); + } + + @Test + void test_convertToExcelData_invalidBase64() { + GlobalConfiguration configuration = new GlobalConfiguration(); + Assertions.assertThrows( + IllegalArgumentException.class, + () -> converter.convertToExcelData("invalid-base64", null, configuration)); + } +} diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringPathnameImageConverterTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringPathnameImageConverterTest.java new file mode 100644 index 000000000..8f3c522d2 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringPathnameImageConverterTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fesod.sheet.converter; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.commons.io.IOUtils; +import org.apache.fesod.sheet.converters.string.StringPathnameImageConverter; +import org.apache.fesod.sheet.metadata.GlobalConfiguration; +import org.apache.fesod.sheet.metadata.data.WriteCellData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * Tests {@link StringPathnameImageConverter}. + */ +class StringPathnameImageConverterTest { + + @TempDir + Path tempDir; + + private final StringPathnameImageConverter converter = new StringPathnameImageConverter(); + private byte[] imageBytes; + + @BeforeEach + void setup() throws IOException { + try (InputStream is = getClass().getClassLoader().getResourceAsStream("converter/img.jpg")) { + imageBytes = IOUtils.toByteArray(is); + } + } + + @Test + void test_supportJavaTypeKey() { + Assertions.assertEquals(String.class, converter.supportJavaTypeKey()); + } + + @Test + void test_convertToExcelData() throws IOException { + Path imageFile = tempDir.resolve("test.jpg"); + Files.write(imageFile, imageBytes); + + WriteCellData cellData = converter.convertToExcelData(imageFile.toString(), null, new GlobalConfiguration()); + + Assertions.assertArrayEquals( + imageBytes, cellData.getImageDataList().get(0).getImage()); + } + + @Test + void test_convertToExcelData_nonExistentFile() { + String nonExistentPath = tempDir.resolve("nonexistent.jpg").toString(); + + Assertions.assertThrows( + IOException.class, + () -> converter.convertToExcelData(nonExistentPath, null, new GlobalConfiguration())); + } +}