Пример #1
0
 /**
  * Creates a serializable blob from a stream, with filename and mimetype detection.
  *
  * <p>Creates an in-memory blob if data is under 64K, otherwise constructs a serializable FileBlob
  * which stores data in a temporary file on the hard disk.
  *
  * @param file the input stream holding data
  * @param filename the file name. Will be set on the blob and will used for mimetype detection.
  * @param mimeType the detected mimetype at upload. Can be null. Will be verified by the mimetype
  *     service.
  */
 public static Blob createSerializableBlob(InputStream file, String filename, String mimeType) {
   Blob blob = null;
   try {
     // persisting the blob makes it possible to read the binary content
     // of the request stream several times (mimetype sniffing, digest
     // computation, core binary storage)
     blob = StreamingBlob.createFromStream(file, mimeType).persist();
     // filename
     if (filename != null) {
       filename = getCleanFileName(filename);
     }
     blob.setFilename(filename);
     // mimetype detection
     MimetypeRegistry mimeService = Framework.getService(MimetypeRegistry.class);
     String detectedMimeType =
         mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob, null);
     if (detectedMimeType == null) {
       if (mimeType != null) {
         detectedMimeType = mimeType;
       } else {
         // default
         detectedMimeType = "application/octet-stream";
       }
     }
     blob.setMimeType(detectedMimeType);
   } catch (MimetypeDetectionException e) {
     log.error(String.format("could not fetch mimetype for file %s", filename), e);
   } catch (IOException e) {
     log.error(e);
   } catch (Exception e) {
     log.error(e);
   }
   return blob;
 }
Пример #2
0
 /**
  * Returns the format for the file passed as a parameter.
  *
  * <p>We will ask the mimetype registry service to sniff its mimetype.
  *
  * @return DocumentFormat for the given file
  */
 private static DocumentFormat getSourceFormat(
     OfficeDocumentConverter documentConverter, File file) {
   MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class);
   String mimetypeStr = mimetypeRegistry.getMimetypeFromFile(file);
   DocumentFormat format = documentConverter.getFormatRegistry().getFormatByMediaType(mimetypeStr);
   return format;
 }
Пример #3
0
  @POST
  public Object convert(
      @FormParam("converter") String converter,
      @FormParam("type") String type,
      @FormParam("format") String format,
      @FormParam("async") boolean async,
      @Context UriInfo uriInfo) {
    if (!async) {
      return convert(converter, type, format, uriInfo);
    }

    String conversionId;
    BlobHolder bh = getBlobHolderToConvert();
    Map<String, Serializable> parameters = computeConversionParameters(uriInfo);
    ConversionService conversionService = Framework.getService(ConversionService.class);
    if (StringUtils.isNotBlank(converter)) {
      conversionId = conversionService.scheduleConversion(converter, bh, parameters);
    } else if (StringUtils.isNotBlank(type)) {
      conversionId = conversionService.scheduleConversionToMimeType(type, bh, parameters);
    } else if (StringUtils.isNotBlank(format)) {
      MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class);
      String mimeType = mimetypeRegistry.getMimetypeFromExtension(format);
      conversionId = conversionService.scheduleConversionToMimeType(mimeType, bh, parameters);
    } else {
      throw new IllegalParameterException("No converter, type or format parameter specified");
    }

    String serverURL = ctx.getServerURL().toString();
    if (serverURL.endsWith("/")) {
      serverURL = serverURL.substring(0, serverURL.length() - 1);
    }
    String pollingURL =
        String.format("%s%s/conversions/%s/poll", serverURL, ctx.getModulePath(), conversionId);
    ConversionScheduled conversionScheduled = new ConversionScheduled(conversionId, pollingURL);
    try {
      return Response.status(Response.Status.ACCEPTED)
          .location(new URI(pollingURL))
          .entity(conversionScheduled)
          .build();
    } catch (URISyntaxException e) {
      throw new NuxeoException(e);
    }
  }
Пример #4
0
 /**
  * Creates a TemporaryFileBlob.
  *
  * <p>Similar to FileUtils.createSerializableBlob.
  *
  * @since 5.7.2
  */
 public static Blob createTemporaryFileBlob(File file, String filename, String mimeType) {
   if (filename != null) {
     filename = FileUtils.getCleanFileName(filename);
   }
   Blob blob = new TemporaryFileBlob(file, mimeType, null, filename, null);
   try {
     // mimetype detection
     MimetypeRegistry mimeService = Framework.getLocalService(MimetypeRegistry.class);
     String detectedMimeType =
         mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob, null);
     if (detectedMimeType == null) {
       if (mimeType != null) {
         detectedMimeType = mimeType;
       } else {
         // default
         detectedMimeType = "application/octet-stream";
       }
     }
     blob.setMimeType(detectedMimeType);
   } catch (MimetypeDetectionException e) {
     log.error(String.format("could not fetch mimetype for file %s", filename), e);
   }
   return blob;
 }
Пример #5
0
 protected Blob convertWithFormat(BlobHolder bh, String format, UriInfo uriInfo) {
   MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class);
   String mimeType = mimetypeRegistry.getMimetypeFromExtension(format);
   return convertWithMimeType(bh, mimeType, uriInfo);
 }