예제 #1
0
  /**
   * @param rFile
   * @param fileOutput
   * @return
   * @throws ExecutePlanException
   */
  private RepresentationFile updateFile(
      final RepresentationFile rFile, final WorkflowExecutionOutput fileOutput)
      throws ExecutePlanException {

    final File outputPortFile = new File(fileOutput.outputDir, workflowOutputPort);
    if (!outputPortFile.exists()) {
      throw new ExecutePlanException(
          "Output port '" + workflowOutputPort + "' for file " + rFile.getId() + " doesn't exist");
    }

    try {
      logger.debug("Reading output file from output port file " + outputPortFile);

      final FileInputStream fisOutputPort = new FileInputStream(outputPortFile);
      final String outputFilepath = IOUtils.toString(fisOutputPort);
      fisOutputPort.close();

      logger.debug("Output file is " + outputFilepath);

      final File outputFile = new File(outputFilepath);
      if (!outputFile.exists()) {
        throw new ExecutePlanException("Output file '" + outputFile + " doesn't exist");
      }

      rFile.setOriginalName(outputFile.getName());
      rFile.setSize(outputFile.length());
      rFile.importFileFormat(FormatUtility.getFileFormat(outputFile, outputFile.getName()));

      final File rFilepath = new File(new URI(rFile.getAccessURL()));

      if (rFilepath.delete()) {
        logger.debug("Deleted previous representation file " + rFilepath);
      }
      logger.debug("Moving output file " + outputFile + " to representation file " + rFilepath);
      FileUtils.moveFile(outputFile, rFilepath);

      logger.debug("Output representation file is " + rFile);

      return rFile;

    } catch (IOException e) {
      logger.error("Error copying file - " + e.getMessage(), e);
      throw new ExecutePlanException("Error copying file - " + e.getMessage(), e);
    } catch (URISyntaxException e) {
      logger.error("Error copying file - " + e.getMessage(), e);
      throw new ExecutePlanException("Error copying file - " + e.getMessage(), e);
    }
  }
  protected RepresentationFile convertRootFile(
      RepresentationFile originalRootFile, File tempDirectory, StringBuffer report)
      throws IOException, CommandException {

    // /tmp/original.../F0
    File originalFile = new File(URI.create(originalRootFile.getAccessURL()));
    // Detect extension by DROID
    String origNameWithExt = originalRootFile.getOriginalName();
    FileFormat detectedFileFormat = FormatUtility.getFileFormat(originalFile, origNameWithExt);
    if ((detectedFileFormat != null)
        && (detectedFileFormat.getExtensions() != null)
        && (detectedFileFormat.getExtensions().length > 0)) {
      origNameWithExt = origNameWithExt + "." + detectedFileFormat.getExtensions()[0];
    }
    // /tmp/original.../originalName.doc
    File originalFileWithExtension = new File(originalFile.getParentFile(), origNameWithExt);
    // mv /tmp/original.../F0 /tmp/original.../originalName.doc
    FileUtils.moveFile(originalFile, originalFileWithExtension);

    // /tmp/converted.../F0
    File convertedFile = new File(tempDirectory, originalFile.getName());
    // /tmp/converted.../originalName.doc.pdf
    File convertedFileWithExtension =
        new File(tempDirectory, originalRootFile.getOriginalName() + this.formatExtension);

    // Execute the command
    List<String> commandArgs = new ArrayList<String>();
    commandArgs.add(this.office2pdfExecutable);
    commandArgs.add(originalFileWithExtension.getAbsolutePath());
    commandArgs.add(convertedFileWithExtension.getAbsolutePath());

    String convertOutput = CommandUtility.execute(commandArgs);

    String commandline = StringUtility.join(commandArgs, " "); // $NON-NLS-1$

    String message;
    if (StringUtils.isBlank(convertOutput)) {
      message =
          String.format(
              "%s: %s => %s%n(Command: %s)%n%n", //$NON-NLS-1$
              originalRootFile.getId(),
              originalRootFile.getOriginalName(),
              convertedFileWithExtension.getName(),
              commandline);
    } else {
      message =
          String.format(
              "%s: %s => %s (%s)%n(Command: %s)%n%n",
              originalRootFile //$NON-NLS-1$
                  .getId(),
              originalRootFile.getOriginalName(),
              convertedFileWithExtension.getName(),
              convertOutput,
              commandline);
    }

    logger.trace(message);
    report.append(message);

    // mv /tmp/converted.../originalName.doc.pdf /tmp/converted.../F0
    FileUtils.moveFile(convertedFileWithExtension, convertedFile);

    RepresentationFile convertedRootFile = new RepresentationFile(originalRootFile);

    // convertedRootFile.setMimetype(format);
    convertedRootFile.importFileFormat(
        FormatUtility.getFileFormat(convertedFile, convertedFileWithExtension.getName()));
    convertedRootFile.setSize(convertedFile.length());
    convertedRootFile.setAccessURL(convertedFile.getAbsolutePath());
    convertedRootFile.setOriginalName(convertedRootFile.getOriginalName() + this.formatExtension);

    return convertedRootFile;
  }