Пример #1
0
  /**
   * {@code id}로 파일을 찾아서 첨부파일로 돌려준다.
   *
   * <p>when: 첨부파일을 다운로드 받을 때
   *
   * <p>주의사항: 파일명이 깨지지 않도록 {@link utils.HttpUtil#encodeContentDisposition)}로 인코딩한다.
   *
   * @param id 첨부파일 id
   * @return 파일이 첨부된 응답
   * @throws NoSuchAlgorithmException
   * @throws IOException
   */
  public static Result getFile(Long id) throws NoSuchAlgorithmException, IOException {
    Attachment attachment = Attachment.find.byId(id);

    if (attachment == null) {
      return notFound();
    }

    if (!AccessControl.isAllowed(UserApp.currentUser(), attachment.asResource(), Operation.READ)) {
      return forbidden();
    }

    File file = attachment.getFile();

    String filename = HttpUtil.encodeContentDisposition(attachment.name);

    response().setHeader("Content-Type", attachment.mimeType);
    response().setHeader("Content-Disposition", "attachment; " + filename);

    return ok(file);
  }