protected Image convertFileEntry(boolean smallImage, FileEntry fileEntry)
      throws PortalException, SystemException {

    try {
      Image image = new ImageImpl();

      image.setModifiedDate(fileEntry.getModifiedDate());

      InputStream is = null;

      if (smallImage) {
        is = ImageProcessorUtil.getThumbnailAsStream(fileEntry.getFileVersion(), 0);
      } else {
        is = fileEntry.getContentStream();
      }

      byte[] bytes = FileUtil.getBytes(is);

      image.setTextObj(bytes);

      image.setType(fileEntry.getExtension());

      return image;
    } catch (PortalException pe) {
      throw pe;
    } catch (SystemException se) {
      throw se;
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
  protected void updateThumbnails(long fileEntryId) throws Exception {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
      con = DataAccess.getUpgradeOptimizedConnection();

      ps =
          con.prepareStatement(
              "select fileVersionId, userId, extension, mimeType, version "
                  + "from DLFileVersion where fileEntryId = "
                  + fileEntryId
                  + " order by version asc");

      rs = ps.executeQuery();

      while (rs.next()) {
        long fileVersionId = rs.getLong("fileVersionId");
        long userId = rs.getLong("userId");
        String extension = rs.getString("extension");
        String mimeType = rs.getString("mimeType");
        String version = rs.getString("version");

        if (_imageMimeTypes.contains(mimeType)) {
          DLFileVersion dlFileVersion = new DLFileVersionImpl();

          dlFileVersion.setFileVersionId(fileVersionId);
          dlFileVersion.setUserId(userId);
          dlFileVersion.setFileEntryId(fileEntryId);
          dlFileVersion.setExtension(extension);
          dlFileVersion.setMimeType(mimeType);
          dlFileVersion.setVersion(version);

          FileVersion fileVersion = new LiferayFileVersion(dlFileVersion);

          try {
            ImageProcessorUtil.generateImages(null, fileVersion);
          } catch (Exception e) {
            if (_log.isWarnEnabled()) {
              _log.warn("Unable to generate thumbnails for " + fileVersion.getFileVersionId(), e);
            }
          }
        }
      }
    } finally {
      DataAccess.cleanUp(con, ps, rs);
    }
  }
  protected void sendFile(
      HttpServletRequest request, HttpServletResponse response, User user, String[] pathArray)
      throws Exception {

    // Retrieve file details

    FileEntry fileEntry = getFileEntry(pathArray);

    if (fileEntry == null) {
      throw new NoSuchFileEntryException();
    }

    String version = ParamUtil.getString(request, "version");

    if (Validator.isNull(version)) {
      if (Validator.isNotNull(fileEntry.getVersion())) {
        version = fileEntry.getVersion();
      }
    }

    String tempFileId = DLUtil.getTempFileId(fileEntry.getFileEntryId(), version);

    FileVersion fileVersion = fileEntry.getFileVersion(version);

    String fileName = fileVersion.getTitle();

    String extension = fileVersion.getExtension();

    if (Validator.isNotNull(extension) && !fileName.endsWith(StringPool.PERIOD + extension)) {

      fileName += StringPool.PERIOD + extension;
    }

    // Handle requested conversion

    boolean converted = false;

    String targetExtension = ParamUtil.getString(request, "targetExtension");
    int imageThumbnail = ParamUtil.getInteger(request, "imageThumbnail");
    int documentThumbnail = ParamUtil.getInteger(request, "documentThumbnail");
    int previewFileIndex = ParamUtil.getInteger(request, "previewFileIndex");
    boolean audioPreview = ParamUtil.getBoolean(request, "audioPreview");
    boolean imagePreview = ParamUtil.getBoolean(request, "imagePreview");
    boolean videoPreview = ParamUtil.getBoolean(request, "videoPreview");
    int videoThumbnail = ParamUtil.getInteger(request, "videoThumbnail");

    InputStream inputStream = null;
    long contentLength = 0;

    if ((imageThumbnail > 0) && (imageThumbnail <= 3)) {
      fileName =
          FileUtil.stripExtension(fileName)
              .concat(StringPool.PERIOD)
              .concat(ImageProcessorUtil.getThumbnailType(fileVersion));

      int thumbnailIndex = imageThumbnail - 1;

      inputStream = ImageProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex);
      contentLength = ImageProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex);

      converted = true;
    } else if ((documentThumbnail > 0) && (documentThumbnail <= 3)) {
      fileName =
          FileUtil.stripExtension(fileName)
              .concat(StringPool.PERIOD)
              .concat(PDFProcessor.THUMBNAIL_TYPE);

      int thumbnailIndex = documentThumbnail - 1;

      inputStream = PDFProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex);
      contentLength = PDFProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex);

      converted = true;
    } else if (previewFileIndex > 0) {
      fileName =
          FileUtil.stripExtension(fileName)
              .concat(StringPool.PERIOD)
              .concat(PDFProcessor.PREVIEW_TYPE);
      inputStream = PDFProcessorUtil.getPreviewAsStream(fileVersion, previewFileIndex);
      contentLength = PDFProcessorUtil.getPreviewFileSize(fileVersion, previewFileIndex);

      converted = true;
    } else if (audioPreview || videoPreview) {
      String type = ParamUtil.getString(request, "type");

      fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(type);

      if (audioPreview) {
        inputStream = AudioProcessorUtil.getPreviewAsStream(fileVersion, type);
        contentLength = AudioProcessorUtil.getPreviewFileSize(fileVersion, type);
      } else {
        inputStream = VideoProcessorUtil.getPreviewAsStream(fileVersion, type);
        contentLength = VideoProcessorUtil.getPreviewFileSize(fileVersion, type);
      }

      converted = true;
    } else if (imagePreview) {
      String type = ImageProcessorUtil.getPreviewType(fileVersion);

      fileName = FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(type);

      inputStream = ImageProcessorUtil.getPreviewAsStream(fileVersion);

      contentLength = ImageProcessorUtil.getPreviewFileSize(fileVersion);

      converted = true;
    } else if ((videoThumbnail > 0) && (videoThumbnail <= 3)) {
      fileName =
          FileUtil.stripExtension(fileName)
              .concat(StringPool.PERIOD)
              .concat(VideoProcessor.THUMBNAIL_TYPE);

      int thumbnailIndex = videoThumbnail - 1;

      inputStream = VideoProcessorUtil.getThumbnailAsStream(fileVersion, thumbnailIndex);
      contentLength = VideoProcessorUtil.getThumbnailFileSize(fileVersion, thumbnailIndex);

      converted = true;
    } else {
      inputStream = fileVersion.getContentStream(true);
      contentLength = fileVersion.getSize();

      if (Validator.isNotNull(targetExtension)) {
        File convertedFile =
            DocumentConversionUtil.convert(tempFileId, inputStream, extension, targetExtension);

        if (convertedFile != null) {
          fileName =
              FileUtil.stripExtension(fileName).concat(StringPool.PERIOD).concat(targetExtension);
          inputStream = new FileInputStream(convertedFile);
          contentLength = convertedFile.length();

          converted = true;
        }
      }
    }

    // Determine proper content type

    String contentType = null;

    if (converted) {
      contentType = MimeTypesUtil.getContentType(fileName);
    } else {
      contentType = fileVersion.getMimeType();
    }

    // Support range HTTP header

    response.setHeader(HttpHeaders.ACCEPT_RANGES, HttpHeaders.ACCEPT_RANGES_BYTES_VALUE);

    List<Range> ranges = null;

    try {
      ranges = ServletResponseUtil.getRanges(request, response, contentLength);
    } catch (IOException ioe) {
      if (_log.isErrorEnabled()) {
        _log.error(ioe);
      }

      response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + contentLength);

      response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);

      return;
    }

    if ((ranges == null) || ranges.isEmpty()) {
      ServletResponseUtil.sendFile(
          request, response, fileName, inputStream, contentLength, contentType);
    } else {
      if (_log.isDebugEnabled()) {
        _log.debug("Request has range header " + request.getHeader(HttpHeaders.RANGE));
      }

      ServletResponseUtil.write(
          request, response, fileName, ranges, inputStream, contentLength, contentType);
    }
  }
Ejemplo n.º 4
0
  protected void migrateImage(
      long fileEntryId,
      long companyId,
      long groupId,
      long folderId,
      String name,
      long smallImageId,
      long largeImageId,
      long custom1ImageId,
      long custom2ImageId)
      throws Exception {

    Image largeImage = null;

    if (largeImageId != 0) {
      largeImage = ImageLocalServiceUtil.getImage(largeImageId);

      long repositoryId = DLFolderConstants.getDataRepositoryId(groupId, folderId);

      try {
        migrateFile(repositoryId, companyId, name, largeImage);
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Ignoring exception for image " + largeImageId, e);
        }
      }
    }

    long thumbnailImageId = 0;

    if (smallImageId != 0) {
      thumbnailImageId = smallImageId;
    } else if (custom1ImageId != 0) {
      thumbnailImageId = custom1ImageId;
    } else if (custom2ImageId != 0) {
      thumbnailImageId = custom2ImageId;
    }

    Image thumbnailImage = null;

    if (thumbnailImageId != 0) {
      thumbnailImage = ImageLocalServiceUtil.getImage(thumbnailImageId);

      Connection con = null;
      PreparedStatement ps = null;
      ResultSet rs = null;

      try {
        InputStream is = _sourceHook.getImageAsStream(thumbnailImage);

        con = DataAccess.getConnection();

        ps =
            con.prepareStatement(
                "select max(fileVersionId) from DLFileVersion where "
                    + "fileEntryId = "
                    + fileEntryId);

        rs = ps.executeQuery();

        if (rs.next()) {
          long fileVersionId = rs.getLong(1);

          ImageProcessorUtil.storeThumbnail(
              companyId,
              groupId,
              fileEntryId,
              fileVersionId,
              custom1ImageId,
              custom2ImageId,
              is,
              thumbnailImage.getType());
        }
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Ignoring exception for image " + thumbnailImageId, e);
        }
      } finally {
        DataAccess.cleanUp(con, ps, rs);
      }
    }

    if (largeImageId != 0) {
      _sourceHook.deleteImage(largeImage);

      runSQL("delete from Image where imageId = " + largeImageId);
    }

    if ((largeImageId != thumbnailImageId) && (thumbnailImageId != 0)) {
      _sourceHook.deleteImage(thumbnailImage);

      runSQL("delete from Image where imageId = " + thumbnailImageId);
    }
  }