/**
  * 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;
 }
 /**
  * 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;
 }