JCRSessionWrapper session, Map<String, List<String>> parameters, URLResolver urlResolver) throws Exception {
if (converterService.isEnabled()) {
// Get parameters + file
final FileUpload fu = (FileUpload) req.getAttribute(FileUpload.FILEUPLOAD_ATTRIBUTE);
DiskFileItem inputFile = fu.getFileItems().get("fileField");
List<String> mimeTypeList = parameters.get("mimeType");
String returnedMimeType = mimeTypeList != null ? mimeTypeList.get(0) : null;
// Convert
boolean conversionSucceeded = true;
String failureMessage = null;
File convertedFile = null;
try {
convertedFile = converterService.convert(inputFile.getStoreLocation(), inputFile.getContentType(),
returnedMimeType);
} catch (IOException ioe) {
conversionSucceeded = false;
failureMessage = ioe.getMessage();
} catch (OfficeException ioe) {
conversionSucceeded = false;
failureMessage = ioe.getMessage();
}
if (convertedFile == null) {
conversionSucceeded = false;
}
// Create a conversion node and the file node if all succeeded
String originFileName = inputFile.getName();
String originMimeType = inputFile.getContentType();
String convertedFileName = FilenameUtils.getBaseName(inputFile.getName()) + "." + converterService.getExtension(
returnedMimeType);
JCRNodeWrapper convertedFilesNode = session.getNode(renderContext.getUser().getLocalPath() + "/files");
JCRNodeWrapper convertedFileNode;
if (conversionSucceeded) {
FileInputStream iStream = new FileInputStream(convertedFile);
convertedFileNode = convertedFilesNode.uploadFile(convertedFileName, iStream, returnedMimeType);
convertedFileNode.addMixin("jmix:convertedFile");
iStream.close();
} else {
convertedFileNode = convertedFilesNode.uploadFile(convertedFileName, inputFile.getInputStream(), inputFile.getContentType());
convertedFileNode.addMixin("jmix:convertedFile");
convertedFileNode.setProperty("conversionFailedMessage", failureMessage);
}
convertedFileNode.setProperty("originDocName", originFileName);