return;
}
if (logger.isDebugEnabled()) {
logger.debug("Converting node " + nodeFact.getPath() + " into target MIME type '" + targetMimeType + "'");
}
DocumentFormat format = converterService.getFormatByMimeType(targetMimeType);
if (format == null) {
logger.warn("Unsupported target MIME type '" + targetMimeType + "'. Skip converting file "
+ nodeFact.getPath());
return;
}
InputStream is = null;
try {
JCRNodeWrapper node = nodeFact.getNode();
if (node.isNodeType("nt:file")) {
is = node.getFileContent().downloadFile();
File temp = File.createTempFile("doc-converter-rule", null);
FileOutputStream os = new FileOutputStream(temp);
try {
converterService.convert(is, nodeFact.getMimeType(), os, targetMimeType);
os.close();
JCRNodeWrapper folder = node.getParent();
String newName = StringUtils.substringBeforeLast(node.getName(), ".") + "." + format.getExtension();
if (!overwriteIfExists) {
newName = JCRContentUtils.findAvailableNodeName(folder, newName);
}
FileInputStream convertedStream = new FileInputStream(temp);
try {
folder.uploadFile(newName, convertedStream, format.getMediaType());
logger.info("Converted node " + nodeFact.getPath() + " to type " + format.getMediaType());
} finally {
IOUtils.closeQuietly(convertedStream);
}
} finally {
IOUtils.closeQuietly(os);