-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
refactor(file): 파일 도메인 리팩토링
- Loading branch information
Showing
20 changed files
with
164 additions
and
259 deletions.
There are no files selected for viewing
23 changes: 13 additions & 10 deletions
23
...iki/global/s3/implement/ImageCreator.java → ...main/file/implementation/FileCreator.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
51 changes: 51 additions & 0 deletions
51
src/main/java/com/project/bumawiki/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,51 @@ | ||
package com.project.bumawiki.domain.file.presentation; | ||
|
||
import com.project.bumawiki.domain.file.presentation.dto.FileResponseDto; | ||
import com.project.bumawiki.domain.file.presentation.dto.R2FileResponseDto; | ||
import com.project.bumawiki.domain.file.service.CommandFileService; | ||
|
||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
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.bind.annotation.RequestPart; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/file") | ||
public class FileController { | ||
private final CommandFileService commandFileService; | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public R2FileResponseDto upload(@RequestPart("file") MultipartFile file) { | ||
return new R2FileResponseDto( | ||
commandFileService.uploadFile(file) | ||
); | ||
} | ||
|
||
@GetMapping("/display/{docsName}/{fileName}") | ||
public ResponseEntity<Resource> displayImage( | ||
@PathVariable String fileName, | ||
@PathVariable String docsName, | ||
HttpServletRequest request | ||
) { | ||
FileResponseDto fileResponseDto = commandFileService.getFile(docsName, fileName, request); | ||
return ResponseEntity.ok() | ||
.contentType(MediaType.parseMediaType(fileResponseDto.contentType())) | ||
.header("Content-Type", fileResponseDto.contentType()) | ||
.body(fileResponseDto.resource()); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/project/bumawiki/domain/file/presentation/dto/FileResponseDto.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,9 @@ | ||
package com.project.bumawiki.domain.file.presentation.dto; | ||
|
||
import org.springframework.core.io.Resource; | ||
|
||
public record FileResponseDto( | ||
Resource resource, | ||
String contentType | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/project/bumawiki/domain/file/presentation/dto/R2FileResponseDto.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,7 @@ | ||
package com.project.bumawiki.domain.file.presentation.dto; | ||
|
||
|
||
public record R2FileResponseDto( | ||
String url | ||
) { | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/project/bumawiki/domain/file/service/CommandFileService.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,68 @@ | ||
package com.project.bumawiki.domain.file.service; | ||
|
||
import com.project.bumawiki.domain.file.implementation.FileCreator; | ||
import com.project.bumawiki.domain.file.presentation.dto.FileResponseDto; | ||
import com.project.bumawiki.global.config.file.local.FileProperties; | ||
import com.project.bumawiki.global.error.exception.BumawikiException; | ||
|
||
import com.project.bumawiki.global.error.exception.ErrorCode; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.UrlResource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CommandFileService { | ||
private final FileCreator fileCreator; | ||
private final FileProperties fileProperties; | ||
|
||
public String uploadFile(MultipartFile file) { | ||
return fileCreator.create(file); | ||
} | ||
|
||
public FileResponseDto getFile(String docsName, String fileName, HttpServletRequest request) { | ||
Resource resource = loadFileAsResource(docsName, fileName); | ||
String contentType = getContentType(request, resource); | ||
return new FileResponseDto(resource, contentType); | ||
} | ||
|
||
private Resource loadFileAsResource(String docsName, String fileName) { | ||
Path uploadPath = Paths.get(fileProperties.path(), docsName); | ||
try { | ||
Path filePath = uploadPath.resolve(fileName).normalize(); | ||
Resource resource = new UrlResource(filePath.toUri()); | ||
if (resource.exists()) { | ||
return resource; | ||
} else { | ||
throw new BumawikiException(ErrorCode.IMAGE_NOT_FOUND_EXCEPTION); | ||
} | ||
} catch (MalformedURLException ex) { | ||
throw new BumawikiException(ErrorCode.MALFORMED_URL); | ||
} | ||
} | ||
|
||
private String getContentType(HttpServletRequest request, Resource resource) { | ||
String contentType = "application/octet-stream"; | ||
try { | ||
String mimeType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); | ||
if (mimeType != null) { | ||
contentType = mimeType; | ||
} | ||
} | ||
catch (IOException ignored) { | ||
} | ||
return contentType; | ||
} | ||
|
||
} |
45 changes: 0 additions & 45 deletions
45
src/main/java/com/project/bumawiki/domain/image/ImageController.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
src/main/java/com/project/bumawiki/domain/image/exception/NoImageException.java
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
src/main/java/com/project/bumawiki/domain/image/presentation/FileStorageProperties.java
This file was deleted.
Oops, something went wrong.
81 changes: 0 additions & 81 deletions
81
src/main/java/com/project/bumawiki/domain/image/service/ImageService.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/main/java/com/project/bumawiki/global/config/file/local/FileProperties.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,9 @@ | ||
package com.project.bumawiki.global.config.file.local; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "file") | ||
public record FileProperties( | ||
String path | ||
) { | ||
} |
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
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
Oops, something went wrong.