Example #1
0
  @HandlesEvent("LoadMedia")
  public Resolution sendMedia() throws FileNotFoundException {
    componentMedia = service.getPersistenceService().findById(ComponentMedia.class, mediaId);
    if (componentMedia == null) {
      throw new OpenStorefrontRuntimeException("Media not Found", "Check media Id");
    }

    InputStream in;
    long length;
    Path path = componentMedia.pathToMedia();
    if (path != null && path.toFile().exists()) {
      in = new FileInputStream(path.toFile());
      length = path.toFile().length();
    } else {
      Component component =
          service
              .getPersistenceService()
              .findById(Component.class, componentMedia.getComponentId());
      log.log(
          Level.WARNING,
          MessageFormat.format(
              "Media not on disk: {0} Check media record: {1} on component {2} ({3}) ",
              new Object[] {
                componentMedia.pathToMedia(),
                mediaId,
                component.getName(),
                component.getComponentId()
              }));
      in = new FileSystemManager().getClass().getResourceAsStream(MISSING_IMAGE);
      length = MISSING_MEDIA_IMAGE_SIZE;
    }
    return new RangeResolutionBuilder()
        .setContentType(componentMedia.getMimeType())
        .setInputStream(in)
        .setTotalLength(length)
        .setRequest(getContext().getRequest())
        .setFilename(componentMedia.getOriginalName())
        .createRangeResolution();
  }
Example #2
0
  @HandlesEvent("UploadMedia")
  public Resolution uploadMedia() {
    Resolution resolution = null;
    Map<String, String> errors = new HashMap<>();

    if (componentMedia != null) {
      Component component =
          service
              .getPersistenceService()
              .findById(Component.class, componentMedia.getComponentId());
      if (component != null) {
        boolean allow = false;
        if (SecurityUtil.isAdminUser()) {
          allow = true;
          log.log(Level.INFO, SecurityUtil.adminAuditLogMessage(getContext().getRequest()));
        } else if (SecurityUtil.isCurrentUserTheOwner(component)) {
          if (ApprovalStatus.APPROVED.equals(component.getApprovalState()) == false) {
            allow = true;
          }
        }
        if (allow) {

          if (doesFileExceedLimit(file)) {
            deleteTempFile(file);
            errors.put("file", "File size exceeds max allowed.");
          } else {

            componentMedia.setActiveStatus(ComponentMedia.ACTIVE_STATUS);
            componentMedia.setUpdateUser(SecurityUtil.getCurrentUserName());
            componentMedia.setCreateUser(SecurityUtil.getCurrentUserName());
            componentMedia.setOriginalName(file.getFileName());
            componentMedia.setMimeType(file.getContentType());

            ValidationModel validationModel = new ValidationModel(componentMedia);
            validationModel.setConsumeFieldsOnly(true);
            ValidationResult validationResult = ValidationUtil.validate(validationModel);
            if (validationResult.valid()) {
              try {
                service.getComponentService().saveMediaFile(componentMedia, file.getInputStream());

                if (SecurityUtil.isAdminUser() == false) {
                  if (ApprovalStatus.PENDING.equals(component.getApprovalState())) {
                    service
                        .getComponentService()
                        .checkComponentCancelStatus(
                            componentMedia.getComponentId(), ApprovalStatus.NOT_SUBMITTED);
                  }
                }
              } catch (IOException ex) {
                throw new OpenStorefrontRuntimeException(
                    "Unable to able to save media.",
                    "Contact System Admin. Check disk space and permissions.",
                    ex);
              } finally {
                deleteTempFile(file);
              }
            } else {
              errors.put("file", validationResult.toHtmlString());
            }
          }
        } else {
          resolution = new ErrorResolution(HttpServletResponse.SC_FORBIDDEN, "Access denied");
        }
      } else {
        errors.put("componentMedia", "Missing component; check Component Id");
      }
    } else {
      errors.put("componentMedia", "Missing component media information");
    }
    if (resolution == null) {
      resolution = streamUploadResponse(errors);
    }
    return resolution;
  }