@Override
  public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters)
      throws ConversionException {
    try {
      // Make sure the toThumbnail command is available
      CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
      CommandAvailability commandAvailability = cles.getCommandAvailability(THUMBNAIL_COMMAND);
      if (!commandAvailability.isAvailable()) {
        return null;
      }
      // get the input and output of the command
      Blob blob = blobHolder.getBlob();
      File inputFile = null;
      if (blob instanceof FileBlob) {
        inputFile = ((FileBlob) blob).getFile();
      } else if (blob instanceof SQLBlob) {
        StreamSource source = ((SQLBlob) blob).getBinary().getStreamSource();
        inputFile = ((FileSource) source).getFile();
      } else if (blob instanceof StreamingBlob) {
        StreamingBlob streamingBlob = ((StreamingBlob) blob);
        if (!streamingBlob.isPersistent()) {
          streamingBlob.persist();
        }
        StreamSource source = streamingBlob.getStreamSource();
        inputFile = ((FileSource) source).getFile();
      }
      if (inputFile == null) {
        return null;
      }
      CmdParameters params = new CmdParameters();
      File outputFile = File.createTempFile("nuxeoImageTarget", "." + "png");
      String size = THUMBNAIL_DEFAULT_SIZE;
      if (parameters != null) {
        if (parameters.containsKey(THUMBNAIL_SIZE_PARAMETER_NAME)) {
          size = (String) parameters.get(THUMBNAIL_SIZE_PARAMETER_NAME);
        }
      }
      params.addNamedParameter(THUMBNAIL_SIZE_PARAMETER_NAME, size);
      params.addNamedParameter("inputFilePath", inputFile);
      params.addNamedParameter("outputFilePath", outputFile);

      ExecResult res = cles.execCommand(THUMBNAIL_COMMAND, params);
      if (!res.isSuccessful()) {
        throw res.getError();
      }
      Blob targetBlob = new FileBlob(outputFile);
      Framework.trackFile(outputFile, targetBlob);
      return new SimpleCachableBlobHolder(targetBlob);
    } catch (CommandNotAvailable | IOException | ClientException | CommandException e) {
      throw new ConversionException("Thumbnail conversion failed", e);
    }
  }
예제 #2
0
  @Override
  public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters)
      throws ConversionException {
    try {
      // Make sure the toThumbnail command is available
      CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
      CommandAvailability commandAvailability = cles.getCommandAvailability(THUMBNAIL_COMMAND);
      if (!commandAvailability.isAvailable()) {
        return null;
      }
      // get the input and output of the command
      Blob blob = blobHolder.getBlob();

      Blob targetBlob = Blobs.createBlobWithExtension(".png");
      targetBlob.setMimeType("image/png");
      try (CloseableFile source = blob.getCloseableFile()) {
        CmdParameters params = new CmdParameters();
        String size;
        if (parameters != null && parameters.containsKey(THUMBNAIL_SIZE_PARAMETER_NAME)) {
          size = (String) parameters.get(THUMBNAIL_SIZE_PARAMETER_NAME);
        } else {
          size = THUMBNAIL_DEFAULT_SIZE;
        }
        params.addNamedParameter(THUMBNAIL_SIZE_PARAMETER_NAME, size);
        params.addNamedParameter("inputFilePath", source.getFile());
        params.addNamedParameter("outputFilePath", targetBlob.getFile());

        ExecResult res = cles.execCommand(THUMBNAIL_COMMAND, params);
        if (!res.isSuccessful()) {
          throw res.getError();
        }
      }
      return new SimpleCachableBlobHolder(targetBlob);
    } catch (CommandNotAvailable | IOException | ClientException | CommandException e) {
      throw new ConversionException("Thumbnail conversion failed", e);
    }
  }