Created
October 2, 2017 19:07
-
-
Save anonymous/d644c9f6035c2d8544db5bc02f29cb3f to your computer and use it in GitHub Desktop.
Conversion Execution Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.instructure.knightrider.converters; | |
import com.amazonaws.util.Base64; | |
import com.groupdocs.conversion.License; | |
import com.groupdocs.conversion.converter.option.LoadOptions; | |
import com.groupdocs.conversion.converter.option.OutputType; | |
import com.groupdocs.conversion.converter.option.SaveOptions; | |
import com.groupdocs.conversion.handler.ConversionHandler; | |
import com.groupdocs.foundation.exception.GroupDocsException; | |
import org.apache.commons.io.IOUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.stereotype.Component; | |
import java.io.*; | |
import java.util.Dictionary; | |
import java.util.Enumeration; | |
@Component | |
public final class GroupDocsConverterImpl implements Converter { | |
private ConversionHandler conversionHandler; | |
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |
@Autowired | |
public GroupDocsConverterImpl(final ConversionHandler conversionHandler, @Qualifier("groupDocsLicense") final String groupDocsLicense) { | |
this.conversionHandler = conversionHandler; | |
initializeLicense(groupDocsLicense); | |
} | |
public File convert(final File file) throws IOException, UnsupportedFileException { | |
File convertedFile; | |
InputStream in = null; | |
try { | |
Dictionary<String, SaveOptions> availableConversions = getSaveOptions(file); | |
SaveOptions saveOptions = availableConversions.get("pdf"); | |
saveOptions.set | |
if (saveOptions != null) { | |
in = new BufferedInputStream(new FileInputStream(file)); | |
logger.debug("Starting conversion of: " + file.getName() + " to PDF"); | |
saveOptions.setOutputType(OutputType.String); | |
LoadOptions lo = new LoadOptions(); | |
String convertedFilePath = conversionHandler.convert(in, saveOptions); | |
convertedFile = new File(convertedFilePath); | |
} else { | |
logUnsupportedFileSaveOptions(availableConversions, file.getName()); | |
throw new UnsupportedFileException("Unable to convert: \"" + file.getName() + "\", file not supported by groupdocs"); | |
} | |
} catch (Exception e) { | |
if(e.getMessage().startsWith("Trying to load unknown file type")){ | |
throw new UnsupportedFileException("Unable to convert: " + file.getName(), e); | |
} | |
throw e; | |
} finally { | |
if (in != null) { | |
IOUtils.closeQuietly(in); | |
} | |
} | |
return convertedFile; | |
} | |
private Dictionary<String, SaveOptions> getSaveOptions(final File file) throws FileNotFoundException, UnsupportedFileException { | |
InputStream in = null; | |
try { | |
in = new BufferedInputStream(new FileInputStream(file)); | |
return conversionHandler.getSaveOptions(in); | |
} catch (GroupDocsException e) { | |
throw new UnsupportedFileException("Unable to convert: \"" + file.getName() + "\", file not supported by groupdocs", e); | |
} finally { | |
if (in != null) { | |
IOUtils.closeQuietly(in); | |
} | |
} | |
} | |
private void initializeLicense(String groupDocsLicense) { | |
InputStream is = null; | |
License license = new License(); | |
if (groupDocsLicense != null && !groupDocsLicense.isEmpty()) { | |
try { | |
is = new ByteArrayInputStream(Base64.decode(groupDocsLicense)); | |
license.setLicense(is); | |
} finally { | |
IOUtils.closeQuietly(is); | |
} | |
} | |
} | |
private void logUnsupportedFileSaveOptions(final Dictionary<String, SaveOptions> availableConversions, final String fileName) { | |
if (logger.isDebugEnabled()) { | |
StringBuilder sb = new StringBuilder("Supported SaveOptions for "); | |
sb.append(fileName); | |
sb.append(": "); | |
Enumeration<String> keys = availableConversions.keys(); | |
while (keys.hasMoreElements()) { | |
sb.append(System.getProperty("line.separator")); | |
sb.append(keys.nextElement()); | |
} | |
logger.debug(sb.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment