Example #1
0
 @Override
 @HasPermission(action = UGC_UPDATE, type = SocialPermission.class)
 public FileInfo updateAttachment(
     @SecuredObject final String ugcId,
     final String contextId,
     final String attachmentId,
     final InputStream newAttachment)
     throws UGCException, FileNotFoundException {
   if (!ObjectId.isValid(ugcId)) {
     throw new IllegalArgumentException("Given Ugc Id is not valid");
   }
   if (!ObjectId.isValid(attachmentId)) {
     throw new IllegalArgumentException("Given UGC Id is not valid");
   }
   try {
     T ugc = (T) ugcRepository.findUGC(contextId, ugcId);
     if (ugc == null) {
       throw new IllegalUgcException("Given UGC Id does not exists");
     }
     FileInfo oldInfo = ugcRepository.getFileInfo(attachmentId);
     ugc.getAttachments().remove(oldInfo);
     FileInfo newInfo =
         ugcRepository.updateFile(
             new ObjectId(attachmentId),
             newAttachment,
             oldInfo.getFileName(),
             oldInfo.getContentType(),
             true);
     ugc.getAttachments().add(newInfo);
     ugcRepository.update(ugcId, ugc);
     reactor.notify(
         UGCEvent.DELETE_ATTACHMENT.getName(),
         Event.wrap(new SocialEvent<>(ugcId, attachmentId, UGCEvent.DELETE_ATTACHMENT)));
     reactor.notify(
         UGCEvent.ADD_ATTACHMENT.getName(),
         Event.wrap(
             new SocialEvent<>(
                 ugcId, new InputStream[] {new CloseShieldInputStream(newAttachment)}),
             UGCEvent.ADD_ATTACHMENT));
     return newInfo;
   } catch (MongoDataException e) {
     log.error("logging.ugc.attachmentError");
     throw new UGCException("Unable to removeWatcher Attachment");
   } catch (FileExistsException e) {
     log.error("logging.ugc.attachmentNotFound", attachmentId);
     throw new UGCException("Unable to find attachment with given id", e);
   }
 }
Example #2
0
  @Override
  @HasPermission(action = UGC_UPDATE, type = SocialPermission.class)
  public FileInfo addAttachment(
      @SecuredObject final String ugcId,
      final String contextId,
      final InputStream attachment,
      final String fileName,
      final String contentType)
      throws FileExistsException, UGCException {
    String internalFileName =
        File.separator + contextId + File.separator + ugcId + File.separator + fileName;
    try {
      checkForVirus(attachment);
    } catch (IOException | VirusScannerException ex) {
      log.error("logging.ugc.errorScanVirus", ex);
      return null;
    }

    try {
      UGC ugc = ugcRepository.findUGC(contextId, ugcId);
      if (ugc == null) {
        throw new IllegalUgcException("UGC with given Id does not exist");
      }
      FileInfo info = ugcRepository.saveFile(attachment, internalFileName, contentType);
      try {
        info.setFileName(new URLCodec().decode(fileName));
      } catch (DecoderException e) {
        info.setFileName(fileName);
      }
      info.setAttribute("owner", ugcId);
      ugc.getAttachments().add(info);
      ugcRepository.update(ugcId, ugc);
      reactor.notify(
          UGCEvent.ADD_ATTACHMENT.getName(),
          Event.wrap(
              new SocialEvent<>(
                  ugcId, new InputStream[] {new CloseShieldInputStream(attachment)})));
      return info;
    } catch (MongoDataException e) {
      log.error("logging.ugc.unableToSaveAttachment", e, internalFileName);
      throw new UGCException("Unable to save File to UGC");
    }
  }