Esempio n. 1
0
  protected void addTempAttachment(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
    String sourceFileName = uploadPortletRequest.getFileName("file");

    InputStream inputStream = null;

    try {
      inputStream = uploadPortletRequest.getFileAsStream("file");

      String mimeType = uploadPortletRequest.getContentType("file");

      String tempFileName = TempFileEntryUtil.getTempFileName(sourceFileName);

      FileEntry fileEntry =
          _wikiPageService.addTempFileEntry(
              nodeId, _TEMP_FOLDER_NAME, tempFileName, inputStream, mimeType);

      JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

      jsonObject.put("groupId", fileEntry.getGroupId());
      jsonObject.put("name", fileEntry.getTitle());
      jsonObject.put("title", sourceFileName);
      jsonObject.put("uuid", fileEntry.getUuid());

      JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }
  protected FileEntry getTempImageFileEntry(PortletRequest portletRequest) throws PortalException {

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    return TempFileEntryUtil.getTempFileEntry(
        themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
        getTempImageFolderName(), getTempImageFileName(portletRequest));
  }
Esempio n. 3
0
  protected void addMultipleFileEntries(
      PortletConfig portletConfig,
      ActionRequest actionRequest,
      ActionResponse actionResponse,
      String selectedFileName,
      List<KeyValuePair> validFileNameKVPs,
      List<KeyValuePair> invalidFileNameKVPs)
      throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
    String title = ParamUtil.getString(actionRequest, "title");

    FileEntry tempFileEntry = null;

    try {
      tempFileEntry =
          TempFileEntryUtil.getTempFileEntry(
              themeDisplay.getScopeGroupId(),
              themeDisplay.getUserId(),
              _TEMP_FOLDER_NAME,
              selectedFileName);

      String originalSelectedFileName =
          TempFileEntryUtil.getOriginalTempFileName(tempFileEntry.getFileName());
      InputStream inputStream = tempFileEntry.getContentStream();
      String mimeType = tempFileEntry.getMimeType();

      FileEntry fileEntry =
          _wikiPageService.addPageAttachment(
              nodeId, title, originalSelectedFileName, inputStream, mimeType);

      validFileNameKVPs.add(new KeyValuePair(fileEntry.getTitle(), selectedFileName));
    } catch (Exception e) {
      String errorMessage =
          getAddMultipleFileEntriesErrorMessage(portletConfig, actionRequest, actionResponse, e);

      invalidFileNameKVPs.add(new KeyValuePair(selectedFileName, errorMessage));
    } finally {
      if (tempFileEntry != null) {
        TempFileEntryUtil.deleteTempFileEntry(tempFileEntry.getFileEntryId());
      }
    }
  }
  protected FileEntry addTempImageFileEntry(PortletRequest portletRequest) throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(portletRequest);

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String contentType = uploadPortletRequest.getContentType("fileName");

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

    String fileName = uploadPortletRequest.getFileName("fileName");

    try {
      TempFileEntryUtil.deleteTempFileEntry(
          themeDisplay.getScopeGroupId(),
          themeDisplay.getUserId(),
          getTempImageFolderName(),
          fileName);
    } catch (Exception e) {
    }

    InputStream inputStream = null;

    try {
      inputStream = uploadPortletRequest.getFileAsStream("fileName");

      return TempFileEntryUtil.addTempFileEntry(
          themeDisplay.getScopeGroupId(),
          themeDisplay.getUserId(),
          getTempImageFolderName(),
          fileName,
          inputStream,
          contentType);
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }
  protected void addMultipleFileEntries(
      PortletConfig portletConfig,
      ActionRequest actionRequest,
      ActionResponse actionResponse,
      String selectedFileName,
      List<KeyValuePair> validFileNameKVPs,
      List<KeyValuePair> invalidFileNameKVPs)
      throws Exception {

    String originalSelectedFileName = selectedFileName;

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
    long folderId = ParamUtil.getLong(actionRequest, "folderId");
    String description = ParamUtil.getString(actionRequest, "description");
    String changeLog = ParamUtil.getString(actionRequest, "changeLog");

    FileEntry tempFileEntry = null;

    try {
      tempFileEntry =
          TempFileEntryUtil.getTempFileEntry(
              themeDisplay.getScopeGroupId(),
              themeDisplay.getUserId(),
              TEMP_FOLDER_NAME,
              selectedFileName);

      selectedFileName =
          DLUtil.getFileName(
              tempFileEntry.getGroupId(), tempFileEntry.getFolderId(), tempFileEntry.getFileName());

      String mimeType = tempFileEntry.getMimeType();

      InputStream inputStream = tempFileEntry.getContentStream();
      long size = tempFileEntry.getSize();

      ServiceContext serviceContext =
          ServiceContextFactory.getInstance(DLFileEntry.class.getName(), actionRequest);

      _dlAppService.addFileEntry(
          repositoryId,
          folderId,
          selectedFileName,
          mimeType,
          selectedFileName,
          description,
          changeLog,
          inputStream,
          size,
          serviceContext);

      validFileNameKVPs.add(new KeyValuePair(selectedFileName, originalSelectedFileName));

      return;
    } catch (Exception e) {
      String errorMessage =
          getAddMultipleFileEntriesErrorMessage(portletConfig, actionRequest, actionResponse, e);

      invalidFileNameKVPs.add(new KeyValuePair(originalSelectedFileName, errorMessage));
    } finally {
      if (tempFileEntry != null) {
        TempFileEntryUtil.deleteTempFileEntry(tempFileEntry.getFileEntryId());
      }
    }
  }
  protected FileEntry saveTempImageFileEntry(ActionRequest actionRequest) throws Exception {

    FileEntry tempFileEntry = null;

    InputStream tempImageStream = null;

    try {
      tempFileEntry = getTempImageFileEntry(actionRequest);

      tempImageStream = tempFileEntry.getContentStream();

      ImageBag imageBag = ImageToolUtil.read(tempImageStream);

      RenderedImage renderedImage = imageBag.getRenderedImage();

      String cropRegionJSON = ParamUtil.getString(actionRequest, "cropRegion");

      if (Validator.isNotNull(cropRegionJSON)) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(cropRegionJSON);

        int height = jsonObject.getInt("height");
        int width = jsonObject.getInt("width");
        int x = jsonObject.getInt("x");
        int y = jsonObject.getInt("y");

        if ((x == 0)
            && (y == 0)
            && (renderedImage.getHeight() == height)
            && (renderedImage.getWidth() == width)) {

          return tempFileEntry;
        }

        if ((height + y) > renderedImage.getHeight()) {
          height = renderedImage.getHeight() - y;
        }

        if ((width + x) > renderedImage.getWidth()) {
          width = renderedImage.getWidth() - x;
        }

        renderedImage = ImageToolUtil.crop(renderedImage, height, width, x, y);
      }

      byte[] bytes = ImageToolUtil.getBytes(renderedImage, imageBag.getType());

      ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

      File file = FileUtil.createTempFile(bytes);

      try {
        TempFileEntryUtil.deleteTempFileEntry(
            themeDisplay.getScopeGroupId(),
            themeDisplay.getUserId(),
            getTempImageFolderName(),
            getTempImageFileName(actionRequest));
      } catch (Exception e) {
      }

      return TempFileEntryUtil.addTempFileEntry(
          themeDisplay.getScopeGroupId(),
          themeDisplay.getUserId(),
          getTempImageFolderName(),
          getTempImageFileName(actionRequest),
          file,
          tempFileEntry.getMimeType());
    } catch (NoSuchFileEntryException nsfee) {
      throw new UploadException(nsfee);
    } catch (NoSuchRepositoryException nsre) {
      throw new UploadException(nsre);
    } finally {
      StreamUtil.cleanUp(tempImageStream);
    }
  }