Repository navigation
Step6 #145
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 #145
Changes from all commits
Commits
Show all changes
5 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,79 @@ | ||
| package codesquad.domain; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import support.domain.AbstractEntity; | ||
|
|
||
| import javax.persistence.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.slf4j.LoggerFactory.getLogger; | ||
|
|
||
| @Entity | ||
| public class Multipart extends AbstractEntity { | ||
| private static final Logger log = getLogger(Multipart.class); | ||
|
|
||
| @ManyToOne | ||
| @JoinColumn(foreignKey = @ForeignKey(name = "fk_multipart_writer")) | ||
| private User writer; | ||
|
|
||
| @ManyToOne | ||
| @JoinColumn(foreignKey = @ForeignKey(name = "fk_multipart_issue")) | ||
| private Issue issue; | ||
|
|
||
| @Column | ||
| private String originalFilename; | ||
|
|
||
| @Column | ||
| private String saveName; | ||
|
|
||
| private String uploadPath; | ||
|
|
||
| public Multipart() { | ||
| } | ||
|
|
||
| public Multipart(User user, Issue issue, String file, String uploadPath) { | ||
| this.writer = user; | ||
| this.issue = issue; | ||
| this.originalFilename = file; | ||
| this.saveName = UUID.randomUUID() + "_" + file; | ||
| this.uploadPath = uploadPath; | ||
| } | ||
|
|
||
| public User getWriter() { | ||
| return writer; | ||
| } | ||
|
|
||
| public String getOriginalFilename() { | ||
| return originalFilename; | ||
| } | ||
|
|
||
| public String getSaveName() { | ||
| return saveName; | ||
| } | ||
|
|
||
| public String getUploadPath() { | ||
| return uploadPath; | ||
| } | ||
|
|
||
| public Issue getIssue() { | ||
| return issue; | ||
| } | ||
|
|
||
| public void setIssue(Issue issue) { | ||
| this.issue = issue; | ||
| } | ||
|
|
||
| public Path add(User user, MultipartFile file) throws IOException { | ||
| byte[] bytes = file.getBytes(); | ||
| Path path = Paths.get(uploadPath + saveName); | ||
| log.debug("path :~~~~~ {}", path.toString()); | ||
| return Files.write(path, bytes); | ||
| } | ||
| } | ||
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,9 @@ | ||
| package codesquad.domain; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface MultipartRepository extends JpaRepository<Multipart, 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
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,51 @@ | ||
| package codesquad.service; | ||
|
|
||
| import codesquad.domain.*; | ||
| import org.slf4j.Logger; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import javax.annotation.Resource; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| import static org.slf4j.LoggerFactory.getLogger; | ||
|
|
||
| @Service | ||
| public class MultipartFileService { | ||
| private static final Logger log = getLogger(MultipartFileService.class); | ||
| @Resource(name = "multipartRepository") | ||
| private MultipartRepository multipartRepository; | ||
|
|
||
| @Resource(name = "issueRepository") | ||
| private IssueRepository issueRepository; | ||
|
|
||
| @Value("${value.from.file}") | ||
| private String uploadPath; | ||
|
|
||
| @Transactional | ||
| public Multipart add(User user, long issueId, MultipartFile file) throws IOException { | ||
| Issue issue = issueRepository.findById(issueId).orElseThrow(UnknownError::new); | ||
| Multipart multipart = new Multipart(user, issue, file.getOriginalFilename(), uploadPath); | ||
| Path path = multipart.add(user, file); | ||
| file.transferTo(path); | ||
| return multipartRepository.save(multipart); | ||
| } | ||
|
|
||
| public List<Multipart> findAll() { | ||
| return multipartRepository.findAll(); | ||
| } | ||
|
|
||
| // public List<Multipart> findByIssueId(long issueId) { | ||
| // return multipartRepository.(issueId); | ||
| // } | ||
|
|
||
| public String findPath(long id) { | ||
| Multipart multipart = multipartRepository.findById(id).orElseThrow(UnknownError::new); | ||
| return multipart.getUploadPath() + multipart.getSaveName(); | ||
| } | ||
| } |
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,61 @@ | ||
| package codesquad.web; | ||
|
|
||
| import codesquad.domain.User; | ||
| import codesquad.security.LoginUser; | ||
| import codesquad.service.MultipartFileService; | ||
| import org.slf4j.Logger; | ||
| 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.ui.Model; | ||
| 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; | ||
| import javax.naming.CannotProceedException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| import static org.slf4j.LoggerFactory.getLogger; | ||
|
|
||
| @Controller | ||
| @RequestMapping("/attachments") | ||
| public class AttatchmentController { | ||
| private static final Logger log = getLogger(AttatchmentController.class); | ||
|
|
||
| @Resource(name = "multipartFileService") | ||
| private MultipartFileService multipartFileService; | ||
|
|
||
| @PostMapping("/{issueId}") | ||
| public String upload(@LoginUser User user, @PathVariable long issueId, MultipartFile file, Model model) throws Exception { | ||
| if (file.isEmpty()) { | ||
| throw new CannotProceedException(); | ||
| } | ||
| multipartFileService.add(user, issueId, file); | ||
| return "redirect:/issues/{issueId}"; | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<FileSystemResource> download(@PathVariable long id) throws IOException { | ||
| String saveName = multipartFileService.findPath(id); | ||
| Path path = Paths.get(saveName); | ||
| FileSystemResource resource = new FileSystemResource(path); | ||
|
|
||
| log.debug("~~~~~~~~1!!{}", saveName); | ||
|
|
||
| HttpHeaders headers = new HttpHeaders(); | ||
| headers.setContentType(MediaType.TEXT_XML); | ||
| headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + saveName); | ||
| headers.setContentLength(resource.contentLength()); | ||
| log.debug("!!!@#!@# {}", headers); | ||
| return new ResponseEntity<FileSystemResource>(resource, headers, HttpStatus.OK); | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
이 설정이 정상적으로 동작하나?
의도한데로 동작하는지 값을 출력해 본다.