Beispiel #1
0
  /**
   * 파일의 목록을 가져온다.
   *
   * <p>이슈, 게시물, 댓글을 볼 때, 첨부된 파일들의 목록을 보여주기 위해 이슈, 게시물, 댓글을 편집할 때, 첨부된 파일들의 목록 및 사용자 파일들의 목록을 보여주기
   * 위해
   *
   * <p>로그인한 사용자의 파일들의 목록을 {@code tempFiles} 프로퍼티로 넘겨준다. 첨부 파일들의 목록을 {@code attachments} 프로퍼티로 넘겨준다.
   * 첨부 파일들 중 로그인한 사용자가 읽기 권한을 갖지 못한 것이 하나라도 있다면 403 Forbidden 으로 응답한다.
   *
   * @return json 포맷으로 된 파일 목록을 본문으로 하는 응답. 다음고 같은 형식이다. {@code {tempFiles: 사용자 파일 목록, attachments:
   *     첨부 파일 목록 }}
   */
  public static Result getFileList() {
    Map<String, List<Map<String, String>>> files = new HashMap<String, List<Map<String, String>>>();

    // Get files from the user's area.
    List<Map<String, String>> userFiles = new ArrayList<Map<String, String>>();
    for (Attachment attach : Attachment.findByContainer(UserApp.currentUser().asResource())) {
      userFiles.add(extractFileMetaDataFromAttachementAsMap(attach));
    }
    files.put("tempFiles", userFiles);

    // Get attached files only if the user has permission to read it.
    Map<String, String[]> query = request().queryString();
    String containerType = HttpUtil.getFirstValueFromQuery(query, "containerType");
    String containerId = HttpUtil.getFirstValueFromQuery(query, "containerId");

    if (containerType != null && containerId != null) {
      List<Map<String, String>> attachments = new ArrayList<Map<String, String>>();
      for (Attachment attach :
          Attachment.findByContainer(
              ResourceType.valueOf(containerType), Long.parseLong(containerId))) {
        if (!AccessControl.isAllowed(UserApp.currentUser(), attach.asResource(), Operation.READ)) {
          return forbidden();
        }
        attachments.add(extractFileMetaDataFromAttachementAsMap(attach));
      }
      files.put("attachments", attachments);
    }

    // Return the list of files as JSON.
    response().setHeader("Content-Type", "application/json");
    return ok(toJson(files));
  }
Beispiel #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();
 }