This is an automated email from the ASF dual-hosted git repository.

psxjoy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fesod.git


The following commit(s) were added to refs/heads/main by this push:
     new 920bb263 feat: split StringImageConverter into Pathname and Base64 
(#934)
920bb263 is described below

commit 920bb263808d2c872559cfae8693b14abfe9305b
Author: Bengbengbalabalabeng 
<[email protected]>
AuthorDate: Wed Jul 1 10:20:41 2026 +0800

    feat: split StringImageConverter into Pathname and Base64 (#934)
    
    * feat: split StringImageConverter into Pathname and Base64
    
    * refactor: optimize base64 prefix removal using substring
    
    * test: add @Tag(Tags.UNIT) to unit tests
    
    ---------
    
    Co-authored-by: Shuxin Pan <[email protected]>
---
 ...verter.java => StringBase64ImageConverter.java} | 28 ++++----
 .../converters/string/StringImageConverter.java    |  3 +-
 ...rter.java => StringPathnameImageConverter.java} | 17 ++---
 .../converter/StringBase64ImageConverterTest.java  | 83 ++++++++++++++++++++++
 .../StringPathnameImageConverterTest.java          | 80 +++++++++++++++++++++
 5 files changed, 184 insertions(+), 27 deletions(-)

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/StringBase64ImageConverter.java
similarity index 71%
copy from 
fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringImageConverter.java
copy to 
fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringBase64ImageConverter.java
index 35baff45..5281e267 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/StringBase64ImageConverter.java
@@ -17,28 +17,20 @@
  * under the License.
  */
 
-/*
- * This file is part of the Apache Fesod (Incubating) project, which was 
derived from Alibaba EasyExcel.
- *
- * Copyright (C) 2018-2024 Alibaba Group Holding Ltd.
- */
-
 package org.apache.fesod.sheet.converters.string;
 
-import java.io.File;
 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;
-import org.apache.fesod.sheet.util.FileUtils;
 
 /**
- * String and image converter
- *
- *
+ * Base64 and image converter (support base64 with prefix).
  */
-public class StringImageConverter implements Converter<String> {
+public class StringBase64ImageConverter implements Converter<String> {
+
     @Override
     public Class<?> supportJavaTypeKey() {
         return String.class;
@@ -46,8 +38,16 @@ public class StringImageConverter implements 
Converter<String> {
 
     @Override
     public WriteCellData<?> convertToExcelData(
-            String value, ExcelContentProperty contentProperty, 
GlobalConfiguration globalConfiguration)
+            String base64, ExcelContentProperty contentProperty, 
GlobalConfiguration globalConfiguration)
             throws IOException {
-        return new WriteCellData<>(FileUtils.readFileToByteArray(new 
File(value)));
+        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 35baff45..06f426a3 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 @@ import org.apache.fesod.sheet.util.FileUtils;
 /**
  * String and image converter
  *
- *
+ * @see StringPathnameImageConverter
  */
+@Deprecated
 public class StringImageConverter implements Converter<String> {
     @Override
     public Class<?> supportJavaTypeKey() {
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/StringPathnameImageConverter.java
similarity index 78%
copy from 
fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringImageConverter.java
copy to 
fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/string/StringPathnameImageConverter.java
index 35baff45..02cb52bf 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/StringPathnameImageConverter.java
@@ -17,12 +17,6 @@
  * under the License.
  */
 
-/*
- * This file is part of the Apache Fesod (Incubating) project, which was 
derived from Alibaba EasyExcel.
- *
- * Copyright (C) 2018-2024 Alibaba Group Holding Ltd.
- */
-
 package org.apache.fesod.sheet.converters.string;
 
 import java.io.File;
@@ -34,11 +28,10 @@ import 
org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
 import org.apache.fesod.sheet.util.FileUtils;
 
 /**
- * String and image converter
- *
- *
+ * Pathname and image converter.
  */
-public class StringImageConverter implements Converter<String> {
+public class StringPathnameImageConverter implements Converter<String> {
+
     @Override
     public Class<?> supportJavaTypeKey() {
         return String.class;
@@ -46,8 +39,8 @@ public class StringImageConverter implements 
Converter<String> {
 
     @Override
     public WriteCellData<?> convertToExcelData(
-            String value, ExcelContentProperty contentProperty, 
GlobalConfiguration globalConfiguration)
+            String pathname, ExcelContentProperty contentProperty, 
GlobalConfiguration globalConfiguration)
             throws IOException {
-        return new WriteCellData<>(FileUtils.readFileToByteArray(new 
File(value)));
+        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 00000000..7a1bafcd
--- /dev/null
+++ 
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringBase64ImageConverterTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.apache.fesod.sheet.testkit.Tags;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link StringBase64ImageConverter}.
+ */
+@Tag(Tags.UNIT)
+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 00000000..9a34d038
--- /dev/null
+++ 
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/StringPathnameImageConverterTest.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.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.apache.fesod.sheet.testkit.Tags;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests {@link StringPathnameImageConverter}.
+ */
+@Tag(Tags.UNIT)
+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()));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to