Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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<String> {

@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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
/**
* String and image converter
*
*
* @see StringPathnameImageConverter
*/
@Deprecated
public class StringImageConverter implements Converter<String> {
@Override
public Class<?> supportJavaTypeKey() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> {

@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)));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Loading