use of com.github.ooxi.jdatauri.DataUri in project xwiki-platform by xwiki.
the class ImageFilter method getFileName.
private String getFileName(Attr source) throws MimeTypeException {
String value = source.getValue();
String fileName = null;
@SuppressWarnings("unchecked") Map<String, byte[]> embeddedImages = (Map<String, byte[]>) source.getOwnerDocument().getUserData(EMBEDDED_IMAGES);
if (embeddedImages != null && value.startsWith("data:")) {
// An image embedded using the Data URI scheme.
DataUri dataURI = DataUri.parse(value, Charset.forName(UTF_8));
fileName = dataURI.getFilename();
if (StringUtils.isEmpty(fileName)) {
fileName = String.valueOf(Math.abs(dataURI.hashCode()));
if (!StringUtils.isEmpty(dataURI.getMime())) {
String extension = MimeTypes.getDefaultMimeTypes().forName(dataURI.getMime()).getExtension();
fileName += extension;
}
}
embeddedImages.put(fileName, dataURI.getData());
} else if (!value.contains("://")) {
// A relative path.
int separator = value.lastIndexOf('/');
fileName = separator < 0 ? value : value.substring(separator + 1);
try {
// We have to decode the image file name in case it contains URL special characters.
fileName = URLDecoder.decode(fileName, UTF_8);
} catch (Exception e) {
// This shouldn't happen. Use the encoded image file name.
}
}
return fileName;
}
Aggregations