Beispiel #1
0
 @Override
 @HasPermission(action = UGC_UPDATE, type = SocialPermission.class)
 public void removeAttachment(
     @SecuredObject final String ugcId, final String contextId, final String attachmentId)
     throws UGCException, FileNotFoundException {
   try {
     UGC ugc = ugcRepository.findUGC(contextId, ugcId);
     if (ugc == null) {
       throw new IllegalUgcException("UGC with given Id does not exist");
     }
     if (!ObjectId.isValid(attachmentId)) {
       throw new IllegalArgumentException("Given Attachment id is not valid");
     }
     ObjectId attachmentOid = new ObjectId(attachmentId);
     FileInfo info = ugcRepository.getFileInfo(attachmentOid);
     if (!info.getStoreName().startsWith(File.separator + contextId)) {
       throw new IllegalSocialQueryException(
           "Given Attachment does not belong to the given context");
     }
     ugc.getAttachments().remove(info);
     ugcRepository.deleteFile(attachmentOid);
     ugcRepository.update(ugcId, ugc);
     reactor.notify(
         UGCEvent.DELETE_ATTACHMENT.getName(),
         Event.wrap(new SocialEvent<>(ugcId, attachmentId, UGCEvent.DELETE_ATTACHMENT)));
   } catch (MongoDataException e) {
     log.error("logging.ugc.attachmentToRemove", e, attachmentId);
     throw new UGCException("Unable to save File to UGC");
   }
 }
Beispiel #2
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);
   }
 }