Repository navigation
Step6 PR #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Step6 PR #152
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package codesquad.domain; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import support.domain.AbstractEntity; | ||
|
|
||
| import javax.persistence.*; | ||
|
|
||
| @Entity | ||
| public class Attachment extends AbstractEntity { | ||
| private static final Logger log = LoggerFactory.getLogger(Attachment.class); | ||
|
|
||
| @Column | ||
| private String originalName; | ||
|
|
||
| @Column | ||
| private String extension; | ||
|
|
||
| @Column | ||
| private String convertedName; | ||
|
|
||
| @ManyToOne | ||
| @JoinColumn(foreignKey = @ForeignKey(name = "fk_attachment_uploader")) | ||
| private User uploader; | ||
|
|
||
| public Attachment() { | ||
|
|
||
| } | ||
|
|
||
| public static Attachment convertedAttachment(MultipartFile file, String convertedName) { | ||
| Attachment attachment = new Attachment(); | ||
| attachment.originalName = file.getOriginalFilename(); | ||
| attachment.convertedName = convertedName; | ||
| return attachment; | ||
| } | ||
|
|
||
| public void loadedBy(User uploader) { | ||
| this.uploader = uploader; | ||
| } | ||
|
|
||
| public String getOriginalName() { | ||
| return originalName; | ||
| } | ||
|
|
||
| public void setOriginalName(String originalName) { | ||
| this.originalName = originalName; | ||
| } | ||
|
|
||
| public String getConvertedName() { | ||
| return convertedName; | ||
| } | ||
|
|
||
| public void setConvertedName(String convertedName) { | ||
| this.convertedName = convertedName; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package codesquad.domain; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface AttachmentRepository extends JpaRepository<Attachment, Long> { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package codesquad.service; | ||
|
|
||
| import codesquad.domain.Attachment; | ||
| import codesquad.domain.AttachmentRepository; | ||
| import codesquad.domain.User; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import javax.annotation.Resource; | ||
| import javax.persistence.EntityNotFoundException; | ||
|
|
||
| @Service | ||
| public class AttachmentService { | ||
| private static final Logger log = LoggerFactory.getLogger(AttachmentService.class); | ||
|
|
||
| @Resource(name = "attachmentRepository") | ||
| private AttachmentRepository attachmentRepository; | ||
|
|
||
| public Attachment add(Attachment attachment) { | ||
| return attachmentRepository.save(attachment); | ||
| } | ||
|
|
||
| @Transactional | ||
| public Attachment save(User loginUser, Attachment attachment) { | ||
| attachment.loadedBy(loginUser); | ||
| return add(attachment); | ||
| } | ||
|
|
||
| public Attachment findById(long id) { | ||
| return attachmentRepository.findById(id).orElseThrow(EntityNotFoundException::new); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package codesquad.web; | ||
|
|
||
| import java.nio.file.*; | ||
| import java.util.UUID; | ||
|
|
||
| import codesquad.domain.Attachment; | ||
| import codesquad.domain.User; | ||
| import codesquad.security.LoginUser; | ||
| import codesquad.service.AttachmentService; | ||
| import codesquad.service.CommentService; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.core.io.FileSystemResource; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import javax.annotation.Resource; | ||
|
|
||
| @Controller | ||
| @RequestMapping("/issues/{issueId}/attachments") | ||
| public class AttachmentController { | ||
| private static final Logger log = LoggerFactory.getLogger(AttachmentController.class); | ||
|
|
||
| @Resource(name = "attachmentService") | ||
| private AttachmentService attachmentService; | ||
|
|
||
| @Resource(name = "commentService") | ||
| private CommentService commentService; | ||
|
|
||
| @PostMapping("") | ||
| public String upload(@LoginUser User loginUser, @PathVariable long issueId, MultipartFile file) throws Exception { | ||
| log.debug("original file name: {}", file.getOriginalFilename()); | ||
| log.debug("contenttype: {}", file.getContentType()); | ||
|
|
||
| String url = "/Users/zingo/Documents/work"; | ||
| String convertedName = UUID.randomUUID().toString(); | ||
|
|
||
| Files.copy(file.getInputStream(), Paths.get(url, convertedName), StandardCopyOption.REPLACE_EXISTING); | ||
| commentService.uploadAttachment(loginUser, issueId, attachmentService.save(loginUser, Attachment.convertedAttachment(file, convertedName))); | ||
|
|
||
| return "redirect:/issues/{issueId}"; | ||
| } | ||
|
|
||
| @GetMapping(value = "/{commentId}", produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE}) | ||
| public ResponseEntity<FileSystemResource> download(@PathVariable long commentId) throws Exception { | ||
| Attachment attachment = attachmentService.findById(commentId); | ||
| Path path = Paths.get("/Users/zingo/Documents/work/" + attachment.getConvertedName()); | ||
| log.debug("***** path : {}", path.toString()); | ||
| FileSystemResource resource = new FileSystemResource(path); | ||
|
|
||
| //TODO: OCTET_STREAM 써도 무방한가? | ||
| HttpHeaders header = new HttpHeaders(); | ||
| header.setContentType(MediaType.APPLICATION_OCTET_STREAM); | ||
| header.set(HttpHeaders.CONTENT_DISPOSITION, | ||
| "attachment; filename=" + java.net.URLEncoder.encode(attachment.getOriginalName(), "UTF-8")); | ||
| header.setContentLength(resource.contentLength()); | ||
|
|
||
| return new ResponseEntity<>(resource, header, HttpStatus.OK); | ||
| } | ||
| } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package codesquad.web; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| import org.junit.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.core.io.ClassPathResource; | ||
| import org.springframework.http.HttpEntity; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.util.MultiValueMap; | ||
| import support.test.AcceptanceTest; | ||
| import support.test.HtmlFormDataBuilder; | ||
|
|
||
| public class AttachmentControllerTest extends AcceptanceTest { | ||
| private static final Logger log = LoggerFactory.getLogger(AttachmentControllerTest.class); | ||
|
|
||
| @Test | ||
| public void download() throws Exception { | ||
| ResponseEntity<String> result = template.getForEntity("/attachments/1", String.class); | ||
| assertEquals(HttpStatus.OK, result.getStatusCode()); | ||
| log.debug("body : {}", result.getBody()); | ||
| } | ||
|
|
||
| @Test | ||
| public void upload() throws Exception { | ||
| HttpEntity<MultiValueMap<String, Object>> request = HtmlFormDataBuilder | ||
| .multipartFormData() | ||
| .addParameter("file", new ClassPathResource("logback.xml")) | ||
| .build(); | ||
| ResponseEntity<String> result = template.postForEntity("/attachments", request, String.class); | ||
| assertEquals(HttpStatus.FOUND, result.getStatusCode()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
첨부 파일이 하나가 아니라 n개가 될 수도 있는데 그 부분까지 고려하면 좋을 것 같네요.