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);
    }
  }
  @Override
  public Image updateImpl(com.liferay.portal.model.Image image) throws SystemException {
    image = toUnwrappedModel(image);

    boolean isNew = image.isNew();

    Session session = null;

    try {
      session = openSession();

      if (image.isNew()) {
        session.save(image);

        image.setNew(false);
      } else {
        session.merge(image);
      }
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

    if (isNew || !ImageModelImpl.COLUMN_BITMASK_ENABLED) {
      FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    EntityCacheUtil.putResult(
        ImageModelImpl.ENTITY_CACHE_ENABLED, ImageImpl.class, image.getPrimaryKey(), image);

    return image;
  }
  public Object getNewValue(Object oldValue) throws Exception {
    _type = null;
    _height = null;
    _width = null;
    _size = null;

    String text = (String) oldValue;

    byte[] bytes = (byte[]) Base64.stringToObject(text);

    try {
      Image image = ImageLocalServiceUtil.getImage(bytes);

      _type = image.getType();
      _height = new Integer(image.getHeight());
      _width = new Integer(image.getWidth());
      _size = new Integer(image.getSize());
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        String imageId = (String) _imageIdColumn.getOldValue();

        if (_log.isWarnEnabled()) {
          _log.warn("Unable to get image data for " + imageId + ": " + e.getMessage());
        }
      }

      _type = ImageConstants.TYPE_NOT_AVAILABLE;
      _height = null;
      _width = null;
      _size = new Integer(bytes.length);
    }

    return oldValue;
  }
  /**
   * Creates a new image with the primary key. Does not add the image to the database.
   *
   * @param imageId the primary key for the new image
   * @return the new image
   */
  public Image create(long imageId) {
    Image image = new ImageImpl();

    image.setNew(true);
    image.setPrimaryKey(imageId);

    return image;
  }
  @Override
  public void clearCache(List<Image> images) {
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

    for (Image image : images) {
      EntityCacheUtil.removeResult(
          ImageModelImpl.ENTITY_CACHE_ENABLED, ImageImpl.class, image.getPrimaryKey());
    }
  }
示例#6
0
  @Override
  public String getSmallImageType() throws PortalException {
    if ((_smallImageType == null) && isSmallImage()) {
      Image smallImage = ImageLocalServiceUtil.getImage(getSmallImageId());

      _smallImageType = smallImage.getType();
    }

    return _smallImageType;
  }
 /**
  * Caches the images in the entity cache if it is enabled.
  *
  * @param images the images
  */
 public void cacheResult(List<Image> images) {
   for (Image image : images) {
     if (EntityCacheUtil.getResult(
             ImageModelImpl.ENTITY_CACHE_ENABLED, ImageImpl.class, image.getPrimaryKey())
         == null) {
       cacheResult(image);
     } else {
       image.resetOriginalValues();
     }
   }
 }
示例#8
0
  @Override
  public File getSmallImageFile() throws PortalException, SystemException {
    if (_smallImageFile == null) {
      Image smallImage = fetchSmallImage();

      if (smallImage != null) {
        _smallImageFile = getSmallImageFile(smallImage.getTextObj());
      }
    }

    return _smallImageFile;
  }
示例#9
0
  @Override
  public String getSmallImageType() throws PortalException, SystemException {
    if (_smallImageType == null) {
      Image smallImage = fetchSmallImage();

      if (smallImage != null) {
        _smallImageType = smallImage.getType();
      }
    }

    return _smallImageType;
  }
  protected String getLayoutIconPath(
      PortletDataContext portletDataContext, Layout layout, Image image) {

    StringBundler sb = new StringBundler(5);

    sb.append(portletDataContext.getLayoutPath(layout.getLayoutId()));
    sb.append("/icons/");
    sb.append(image.getImageId());
    sb.append(StringPool.PERIOD);
    sb.append(image.getType());

    return sb.toString();
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, BlogsEntry entry)
      throws Exception {

    Element entryElement = portletDataContext.getExportDataElement(entry);

    if (entry.isSmallImage()) {
      Image smallImage = _imageLocalService.fetchImage(entry.getSmallImageId());

      if (smallImage != null) {
        String smallImagePath =
            ExportImportPathUtil.getModelPath(
                entry, smallImage.getImageId() + StringPool.PERIOD + smallImage.getType());

        entryElement.addAttribute("small-image-path", smallImagePath);

        entry.setSmallImageType(smallImage.getType());

        portletDataContext.addZipEntry(smallImagePath, smallImage.getTextObj());
      }
    }

    if (entry.getSmallImageFileEntryId() != 0) {
      FileEntry fileEntry =
          PortletFileRepositoryUtil.getPortletFileEntry(entry.getSmallImageFileEntryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, entry, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK);
    }

    if (entry.getCoverImageFileEntryId() != 0) {
      FileEntry fileEntry =
          PortletFileRepositoryUtil.getPortletFileEntry(entry.getCoverImageFileEntryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, entry, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK);
    }

    String content =
        _blogsEntryExportImportContentProcessor.replaceExportContentReferences(
            portletDataContext,
            entry,
            entry.getContent(),
            portletDataContext.getBooleanParameter("blogs", "referenced-content"),
            true);

    entry.setContent(content);

    portletDataContext.addClassedModel(
        entryElement, ExportImportPathUtil.getModelPath(entry), entry);
  }
示例#12
0
  public InputStream getImageAsStream(Image image) throws PortalException, SystemException {

    try {
      File file = getFile(image.getImageId(), image.getType());

      if (!file.exists()) {
        throw new NoSuchFileException(file.getPath());
      }

      return new FileInputStream(file);
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    }
  }
  protected List<byte[]> getImages(UploadPortletRequest uploadPortletRequest, String imagePrefix)
      throws Exception {

    List<byte[]> images = new ArrayList<>();

    for (String name : getSortedParameterNames(uploadPortletRequest, imagePrefix)) {

      String contentType = uploadPortletRequest.getContentType(name);

      if (!MimeTypesUtil.isWebImage(contentType)) {
        throw new ProductEntryScreenshotsException();
      }

      int priority = GetterUtil.getInteger(name.substring(imagePrefix.length()));

      boolean preserveScreenshot =
          ParamUtil.getBoolean(uploadPortletRequest, "preserveScreenshot" + priority);

      byte[] bytes = null;

      if (preserveScreenshot) {
        SCProductScreenshot productScreenshot =
            getProductScreenshot(uploadPortletRequest, priority);

        Image image = null;

        if (imagePrefix.equals("fullImage")) {
          image = ImageLocalServiceUtil.getImage(productScreenshot.getFullImageId());
        } else {
          image = ImageLocalServiceUtil.getImage(productScreenshot.getThumbnailId());
        }

        bytes = image.getTextObj();
      } else {
        InputStream inputStream = uploadPortletRequest.getFileAsStream(name);

        if (inputStream != null) {
          bytes = FileUtil.getBytes(inputStream);
        }
      }

      if (ArrayUtil.isNotEmpty(bytes)) {
        images.add(bytes);
      } else {
        throw new ProductEntryScreenshotsException();
      }
    }

    return images;
  }
示例#14
0
  public byte[] getImageAsBytes(Image image) throws PortalException, SystemException {

    try {
      File file = getFile(image.getImageId(), image.getType());

      if (!file.exists()) {
        throw new NoSuchFileException(file.getPath());
      }

      return FileUtil.getBytes(file);
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    }
  }
  /**
   * Adds the image to the database. Also notifies the appropriate model listeners.
   *
   * @param image the image
   * @return the image that was added
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public Image addImage(Image image) {
    image.setNew(true);

    return imagePersistence.update(image);
  }
  protected void writeImage(Image image, HttpServletRequest request, HttpServletResponse response) {

    if (image == null) {
      return;
    }

    String contentType = null;

    String type = image.getType();

    if (!type.equals(ImageConstants.TYPE_NOT_AVAILABLE)) {
      contentType = MimeTypesUtil.getContentType("A." + type);

      response.setContentType(contentType);
    }

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

    try {
      byte[] bytes = getImageBytes(request, image);

      if (Validator.isNotNull(fileName)) {
        ServletResponseUtil.sendFile(request, response, fileName, bytes, contentType);
      } else {
        ServletResponseUtil.write(response, bytes);
      }
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(e, e);
      }
    }
  }
示例#17
0
  protected void migrateFile(long repositoryId, long companyId, String name, Image image)
      throws Exception {

    InputStream is = _sourceHook.getImageAsStream(image);

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

    if (name == null) {
      name = image.getImageId() + StringPool.PERIOD + image.getType();
    }

    if (DLStoreUtil.hasFile(companyId, repositoryId, name)) {
      DLStoreUtil.deleteFile(companyId, repositoryId, name);
    }

    DLStoreUtil.addFile(companyId, repositoryId, name, false, bytes);
  }
  public int compareTo(Image image) {
    int value = 0;

    if (getImageId() < image.getImageId()) {
      value = -1;
    } else if (getImageId() > image.getImageId()) {
      value = 1;
    } else {
      value = 0;
    }

    if (value != 0) {
      return value;
    }

    return 0;
  }
  protected void exportLayoutIconImage(
      PortletDataContext portletDataContext, Layout layout, Element layoutElement)
      throws Exception {

    Image image = _imageLocalService.getImage(layout.getIconImageId());

    if (image != null) {
      String iconPath =
          ExportImportPathUtil.getModelPath(
              portletDataContext.getScopeGroupId(), Image.class.getName(), image.getImageId());

      Element iconImagePathElement = layoutElement.addElement("icon-image-path");

      iconImagePathElement.addText(iconPath);

      portletDataContext.addZipEntry(iconPath, image.getTextObj());
    }
  }
  protected Image getUserPortraitImageResized(Image image, long imageId)
      throws PortalException, SystemException {

    if (image == null) {
      return null;
    }

    if ((image.getHeight() > PropsValues.USERS_IMAGE_MAX_HEIGHT)
        || (image.getWidth() > PropsValues.USERS_IMAGE_MAX_WIDTH)) {

      User user = UserLocalServiceUtil.getUserByPortraitId(imageId);

      UserLocalServiceUtil.updatePortrait(user.getUserId(), image.getTextObj());

      return ImageLocalServiceUtil.getImage(imageId);
    }

    return image;
  }
示例#21
0
  public void updateImage(Image image, String type, byte[] bytes) throws SystemException {

    try {
      File file = getFile(image.getImageId(), type);

      FileUtil.write(file, bytes);
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    }
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static Image toModel(ImageSoap soapModel) {
    Image model = new ImageImpl();

    model.setImageId(soapModel.getImageId());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setText(soapModel.getText());
    model.setType(soapModel.getType());
    model.setHeight(soapModel.getHeight());
    model.setWidth(soapModel.getWidth());
    model.setSize(soapModel.getSize());

    return model;
  }
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }

    Image image = null;

    try {
      image = (Image) obj;
    } catch (ClassCastException cce) {
      return false;
    }

    long pk = image.getPrimaryKey();

    if (getPrimaryKey() == pk) {
      return true;
    } else {
      return false;
    }
  }
  public Image updateImage(long imageId, byte[] bytes, String type, int height, int width, int size)
      throws PortalException, SystemException {

    Image image = imagePersistence.fetchByPrimaryKey(imageId);

    if (image == null) {
      image = imagePersistence.create(imageId);
    }

    image.setModifiedDate(new Date());
    image.setType(type);
    image.setHeight(height);
    image.setWidth(width);
    image.setSize(size);

    Hook hook = HookFactory.getInstance();

    hook.updateImage(image, type, bytes);

    imagePersistence.update(image, false);

    WebServerServletTokenUtil.resetToken(imageId);

    return image;
  }
示例#25
0
  protected byte[] getImageBytes(UploadRequest uploadRequest, String fieldNameValue)
      throws Exception {

    File file = uploadRequest.getFile(fieldNameValue + "File");

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

    if (ArrayUtil.isNotEmpty(bytes)) {
      return bytes;
    }

    String url = uploadRequest.getParameter(fieldNameValue + "URL");

    long imageId = GetterUtil.getLong(HttpUtil.getParameter(url, "img_id", false));

    Image image = ImageLocalServiceUtil.fetchImage(imageId);

    if (image == null) {
      return null;
    }

    return image.getTextObj();
  }
  protected Image toUnwrappedModel(Image image) {
    if (image instanceof ImageImpl) {
      return image;
    }

    ImageImpl imageImpl = new ImageImpl();

    imageImpl.setNew(image.isNew());
    imageImpl.setPrimaryKey(image.getPrimaryKey());

    imageImpl.setImageId(image.getImageId());
    imageImpl.setModifiedDate(image.getModifiedDate());
    imageImpl.setText(image.getText());
    imageImpl.setType(image.getType());
    imageImpl.setHeight(image.getHeight());
    imageImpl.setWidth(image.getWidth());
    imageImpl.setSize(image.getSize());

    return imageImpl;
  }
  @Override
  protected Image removeImpl(Image image) throws SystemException {
    image = toUnwrappedModel(image);

    Session session = null;

    try {
      session = openSession();

      if (image.isCachedModel()) {
        image = (Image) session.get(ImageImpl.class, image.getPrimaryKeyObj());
      }

      session.delete(image);
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    clearCache(image);

    return image;
  }
  public Image updateImage(long imageId, InputStream is) throws PortalException, SystemException {

    Image image = getImage(is);

    return updateImage(
        imageId,
        image.getTextObj(),
        image.getType(),
        image.getHeight(),
        image.getWidth(),
        image.getSize());
  }
  protected Image getImage(InputStream is, byte[] bytes) throws PortalException, SystemException {

    try {
      if (is != null) {
        bytes = FileUtil.getBytes(is);
      }

      ImageBag imageBag = ImageProcessorUtil.read(bytes);

      RenderedImage renderedImage = imageBag.getRenderedImage();
      String type = imageBag.getType();

      if (renderedImage == null) {
        throw new ImageTypeException();
      }

      int height = renderedImage.getHeight();
      int width = renderedImage.getWidth();
      int size = bytes.length;

      Image image = new ImageImpl();

      image.setTextObj(bytes);
      image.setType(type);
      image.setHeight(height);
      image.setWidth(width);
      image.setSize(size);

      return image;
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ioe) {
          if (_log.isWarnEnabled()) {
            _log.warn(ioe);
          }
        }
      }
    }
  }
  protected Image getImage(InputStream is, byte[] bytes, boolean cleanUpStream)
      throws PortalException, SystemException {

    try {
      if (is != null) {
        bytes = FileUtil.getBytes(is, -1, cleanUpStream);
      }

      if (bytes == null) {
        return null;
      }

      ImageBag imageBag = ImageToolUtil.read(bytes);

      RenderedImage renderedImage = imageBag.getRenderedImage();
      String type = imageBag.getType();

      if (renderedImage == null) {
        throw new ImageTypeException();
      }

      int height = renderedImage.getHeight();
      int width = renderedImage.getWidth();
      int size = bytes.length;

      Image image = new ImageImpl();

      image.setTextObj(bytes);
      image.setType(type);
      image.setHeight(height);
      image.setWidth(width);
      image.setSize(size);

      return image;
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    }
  }