Esempio n. 1
0
  /**
   * 업로드된 {@code file}을 주어진 {@code name}으로 {@code container}에 첨부한다.
   *
   * <p>when: 업로드된 파일이 사용자에게 첨부될 때. 혹은 사용자를 거치지 않고 바로 다른 리소스로 첨부될 때.
   *
   * <p>업로드된 파일을 업로드 디렉토리로 옮긴다. 이 때 파일이름을 그 파일의 해시값으로 변경한다. 그 후 이 파일에 대한 메타정보 및 첨부될 대상에 대한 정보를 이
   * 엔터티에 담는다. 만약 이 엔터티와 같은 내용을 갖고 있는 엔터티가 이미 존재한다면, 이미 {@code container}에 같은 첨부가 존재하고 있으므로 첨부하지 않고
   * {@code false}를 반환한다. 그렇지 않다면 첨부 후 {@code true}를 반환한다.
   *
   * @param file 첨부할 파일
   * @param name 파일 이름
   * @param container 파일이 첨부될 리소스
   * @return 파일이 새로 첨부되었다면 {@code true}, 이미 같은 첨부가 존재하여 첨부되지 않았다면 {@code false}
   * @throws IOException
   * @throws NoSuchAlgorithmException
   */
  @Transient
  public boolean store(File file, String name, Resource container)
      throws IOException, NoSuchAlgorithmException {
    // Store the file as its SHA1 hash in filesystem, and record its
    // metadata - containerType, containerId, size and hash - in Database.
    this.containerType = container.getType();
    this.containerId = container.getId();
    this.createdDate = JodaDateUtil.now();

    if (name == null) {
      this.name = file.getName();
    } else {
      this.name = name;
    }

    if (this.mimeType == null) {
      this.mimeType = FileUtil.detectMediaType(file, name);
    }

    // the size must be set before it is moved.
    this.size = file.length();
    this.hash = Attachment.moveFileIntoUploadDirectory(file);

    // Add the attachment into the Database only if there is no same record.
    Attachment sameAttach = Attachment.findBy(this);
    if (sameAttach == null) {
      super.save();
      return true;
    } else {
      this.id = sameAttach.id;
      return false;
    }
  }
Esempio n. 2
0
 /**
  * {@code from}에 첨부된 모든 첨부 파일을 {@code to}로 옮긴다.
  *
  * <p>when: 업로드 직후 일시적으로 사용자에게 첨부되었던 첨부 파일들을, 특정 리소스(이슈, 게시물 등)으로 옮기려 할 때
  *
  * @param from 첨부 파일이 원래 있었던 리소스
  * @param to 첨부 파일이 새로 옮겨질 리소스
  * @return
  */
 public static int moveAll(Resource from, Resource to) {
   List<Attachment> attachments = Attachment.findByContainer(from);
   for (Attachment attachment : attachments) {
     attachment.moveTo(to);
   }
   return attachments.size();
 }
Esempio n. 3
0
 /**
  * {@code from}에 첨부된 파일중 파일 아이디가{@code selectedFileIds}에 해당하는 첨부 파일을 {@code to}로 옮긴다.
  *
  * <p>when: 업로드 직후 일시적으로 사용자에게 첨부되었던 첨부 파일들을, 특정 리소스(이슈, 게시물 등)으로 옮기려 할 때
  *
  * @param from 첨부 파일이 원래 있었던 리소스
  * @param to 첨부 파일이 새로 옮겨질 리소스
  * @return
  */
 public static int moveOnlySelected(Resource from, Resource to, String[] selectedFileIds) {
   if (selectedFileIds.length == 0) {
     return NOTHING_TO_ATTACH;
   }
   List<Attachment> attachments =
       Attachment.find.where().idIn(Arrays.asList(selectedFileIds)).findList();
   for (Attachment attachment : attachments) {
     if (attachment.containerId.equals(from.getId())
         && attachment.containerType == from.getType()) {
       attachment.moveTo(to);
     }
   }
   return attachments.size();
 }
Esempio n. 4
0
  /**
   * Removes the Attachment specified by the attachmentId.
   *
   * @param attachmentId id of attachment to remove
   * @return removed Attachment or null if one was not found with the id
   */
  public Attachment removeAttachment(final long attachmentId) {
    Attachment removedAttachment = null;

    if (attachments != null) {
      for (int index = attachments.size() - 1; index >= 0; --index) {
        Attachment currentAttachment = attachments.get(index);

        if (currentAttachment.getId() == attachmentId) {
          removedAttachment = attachments.remove(index);
          break;
        }
      }
    }

    return removedAttachment;
  }
Esempio n. 5
0
 /**
  * 주어진 {@code container}에 첨부된 모든 첨부 파일을 삭제한다.
  *
  * <p>when: 첨부 파일을 가질 수 있는 어떤 리소스가 삭제되었을 때
  *
  * @param container 첨부 파일을 삭제할 리소스
  */
 public static void deleteAll(Resource container) {
   List<Attachment> attachments = findByContainer(container);
   for (Attachment attachment : attachments) {
     attachment.delete();
   }
 }