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;
}
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;
}
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;
}
}
Aggregations