Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added image data URL handling to the excel cell content handler (#1237) #1239

Merged
Merged
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
Expand Up @@ -2,13 +2,13 @@
* Copyright (c) 2011, 2012, 2013 James Talbut.
* jim-emitters@spudsoft.co.uk
*
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* James Talbut - Initial implementation.
************************************************************************************/
Expand All @@ -17,14 +17,14 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
Expand Down Expand Up @@ -116,6 +116,10 @@ public class CellContentHandler extends AbstractHandler {
*/
protected String hyperlinkBookmark;

private static String DATA_PROTOCOL = "data:";

private static String DATA_PROTOCOL_BASE = ";base64,";

public CellContentHandler(IContentEmitter emitter, Logger log, IHandler parent, ICellContent cell) {
super(log, parent, cell);
contentVisitor = new ContentEmitterVisitor(emitter);
Expand Down Expand Up @@ -495,20 +499,29 @@ public void recordImage(HandlerState state, Coordinate location, IImageContent i
Workbook wb = state.getWb();
String mimeType = image.getMIMEType();
if ((data == null) && (image.getURI() != null)) {
try {
URL imageUrl = new URL(image.getURI());
URLConnection conn = imageUrl.openConnection();
conn.connect();
mimeType = conn.getContentType();
int imageType = smu.poiImageTypeFromMimeType(mimeType, null);
if (imageType == 0) {
log.debug("Unrecognised/unhandled image MIME type: " + mimeType);
} else {
data = smu.downloadImage(conn);
String stringURI = image.getURI().toString().toLowerCase();
if (stringURI.startsWith(DATA_PROTOCOL) && stringURI.contains(DATA_PROTOCOL_BASE)) {
String base64[] = image.getURI().toString().split(DATA_PROTOCOL_BASE);
if (base64.length >= 2) {
data = Base64.decodeBase64(base64[1]);
}
} else {
try {
URL imageUrl = new URL(image.getURI());
URLConnection conn = imageUrl.openConnection();
conn.connect();
mimeType = conn.getContentType();
int imageType = smu.poiImageTypeFromMimeType(mimeType, null);
if (imageType == 0) {
log.debug("Unrecognised/unhandled image MIME type: " + mimeType);
} else {
data = smu.downloadImage(conn);
image.setData(data);
}
} catch (IOException ex) {
log.debug(ex.getClass(), ": ", ex.getMessage());
ex.printStackTrace();
}
} catch (IOException ex) {
log.debug(ex.getClass(), ": ", ex.getMessage());
ex.printStackTrace();
}
}
if (data != null) {
Expand Down