Search in sources :

Example 1 with MagicMatch

use of net.sf.jmimemagic.MagicMatch in project structr by structr.

the class FileDataConverter method convert.

@Override
public Object convert(final Object source) throws FrameworkException {
    if (source == null) {
        return false;
    }
    final File currentFile = (File) getCurrentObject();
    if (source instanceof byte[]) {
        try {
            byte[] data = (byte[]) source;
            MagicMatch match = Magic.getMagicMatch(data);
            String mimeType = match.getMimeType();
            try {
                FileHelper.setFileData(currentFile, data, mimeType);
            } catch (IOException ioex) {
                logger.warn("Unable to store file", ioex);
            }
        } catch (MagicException | MagicParseException | MagicMatchNotFoundException mex) {
            logger.warn("Unable to parse file data", mex);
        }
    } else if (source instanceof String) {
        String sourceString = (String) source;
        if (StringUtils.isNotBlank(sourceString)) {
            final Base64URIData uriData = new Base64URIData(sourceString);
            try {
                FileHelper.setFileData(currentFile, uriData.getBinaryData(), uriData.getContentType());
            } catch (IOException ioex) {
                logger.warn("Unable to store file", ioex);
            }
        }
    }
    return null;
}
Also used : MagicMatchNotFoundException(net.sf.jmimemagic.MagicMatchNotFoundException) MagicException(net.sf.jmimemagic.MagicException) Base64URIData(org.structr.web.common.FileHelper.Base64URIData) IOException(java.io.IOException) File(org.structr.web.entity.File) MagicParseException(net.sf.jmimemagic.MagicParseException) MagicMatch(net.sf.jmimemagic.MagicMatch)

Example 2 with MagicMatch

use of net.sf.jmimemagic.MagicMatch in project structr by structr.

the class ImageConverter method convert.

// ~--- methods --------------------------------------------------------
@Override
public Object convert(final Object source) {
    if (source == null) {
        return false;
    }
    try {
        Image img = null;
        try {
            if (source instanceof byte[]) {
                byte[] data = (byte[]) source;
                MagicMatch match = Magic.getMagicMatch(data);
                String mimeType = match.getMimeType();
                if (keyAndClass != null) {
                    img = (Image) ImageHelper.createFile(securityContext, data, mimeType, keyAndClass.getCls());
                } else {
                    ImageHelper.setImageData((Image) currentObject, data, mimeType);
                }
            } else if (source instanceof String) {
                String sourceString = (String) source;
                if (StringUtils.isNotBlank(sourceString)) {
                    if (keyAndClass != null) {
                        // UUID?
                        if (sourceString.length() == 32) {
                            img = (Image) ImageHelper.transformFile(securityContext, sourceString, keyAndClass != null ? keyAndClass.getCls() : null);
                        }
                        if (img == null) {
                            img = (Image) ImageHelper.createFileBase64(securityContext, sourceString, keyAndClass != null ? keyAndClass.getCls() : null);
                        }
                    } else {
                        ImageHelper.decodeAndSetFileData((Image) currentObject, sourceString);
                        ImageHelper.updateMetadata((Image) currentObject);
                    }
                }
            }
        } catch (Throwable t) {
            logger.warn("Cannot create image node from given data", t);
        }
        if (img != null) {
            // manual indexing of UUID needed here to avoid a 404 in the following setProperty call
            img.updateInIndex();
            currentObject.setProperties(securityContext, new PropertyMap(keyAndClass.getPropertyKey(), img));
        }
    } catch (Throwable t) {
        logger.warn("Cannot create image node from given data", t);
    }
    return null;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Image(org.structr.web.entity.Image) MagicMatch(net.sf.jmimemagic.MagicMatch)

Example 3 with MagicMatch

use of net.sf.jmimemagic.MagicMatch in project jeesuite-libs by vakinge.

the class UploadObject method perseMimeType.

private static String perseMimeType(byte[] bytes) {
    try {
        MagicMatch match = Magic.getMagicMatch(bytes);
        String mimeType = match.getMimeType();
        return mimeType;
    } catch (Exception e) {
        return null;
    }
}
Also used : MagicMatch(net.sf.jmimemagic.MagicMatch)

Aggregations

MagicMatch (net.sf.jmimemagic.MagicMatch)3 IOException (java.io.IOException)1 MagicException (net.sf.jmimemagic.MagicException)1 MagicMatchNotFoundException (net.sf.jmimemagic.MagicMatchNotFoundException)1 MagicParseException (net.sf.jmimemagic.MagicParseException)1 PropertyMap (org.structr.core.property.PropertyMap)1 Base64URIData (org.structr.web.common.FileHelper.Base64URIData)1 File (org.structr.web.entity.File)1 Image (org.structr.web.entity.Image)1