Search in sources :

Example 1 with DataUri

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;
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) MimeTypeException(org.apache.tika.mime.MimeTypeException) DataUri(com.github.ooxi.jdatauri.DataUri)

Aggregations

DataUri (com.github.ooxi.jdatauri.DataUri)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MimeTypeException (org.apache.tika.mime.MimeTypeException)1