Search in sources :

Example 1 with Base64URIData

use of org.structr.web.common.FileHelper.Base64URIData 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)

Aggregations

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