-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a3fcd7
commit 16960b5
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/org/bssm/attachit/domain/file/presentation/FileController.java
This file contains 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,24 @@ | ||
package org.bssm.attachit.domain.file.presentation; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.bssm.attachit.domain.file.service.GetFileService; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.io.IOException; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/file") | ||
public class FileController { | ||
|
||
private final GetFileService getFileService; | ||
|
||
@GetMapping | ||
public ResponseEntity<Resource> getFile(@RequestParam("attachmentId") Long id, HttpServletRequest httpServletRequest) throws IOException { | ||
return getFileService.execute(id, httpServletRequest); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/bssm/attachit/domain/file/service/GetFileService.java
This file contains 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,36 @@ | ||
package org.bssm.attachit.domain.file.service; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.bssm.attachit.domain.attachment.domain.Attachment; | ||
import org.bssm.attachit.domain.attachment.exception.FileNotFoundException; | ||
import org.bssm.attachit.domain.attachment.repository.AttachmentRepository; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.UrlResource; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class GetFileService { | ||
|
||
private final AttachmentRepository attachmentRepository; | ||
|
||
public ResponseEntity<Resource> execute(Long id, HttpServletRequest httpServletRequest) throws IOException { | ||
Attachment attachment = attachmentRepository.findById(id).orElseThrow( | ||
() -> FileNotFoundException.EXCEPTION | ||
); | ||
Path path = Paths.get(attachment.getPath()); | ||
Resource resource = new UrlResource(path.toUri()); | ||
String contentType = httpServletRequest.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); | ||
return ResponseEntity.ok() | ||
.contentType(MediaType.parseMediaType(contentType)) | ||
.header(contentType) | ||
.body(resource); | ||
} | ||
} |
This file contains 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