-
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.
Browse files
Browse the repository at this point in the history
�파일 도메인 리팩토링
- Loading branch information
Showing
17 changed files
with
178 additions
and
177 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
src/main/java/schoolzone/schoolzone_backend_v2/domain/file/domain/File.java
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
...in/java/schoolzone/schoolzone_backend_v2/domain/file/exception/FileNotFoundException.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
src/main/java/schoolzone/schoolzone_backend_v2/domain/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package schoolzone.schoolzone_backend_v2.domain.file.implementation; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import schoolzone.schoolzone_backend_v2.global.annotation.Implementation; | ||
import schoolzone.schoolzone_backend_v2.global.config.file.FileProperties; | ||
import schoolzone.schoolzone_backend_v2.global.error.exception.ErrorCode; | ||
import schoolzone.schoolzone_backend_v2.global.error.exception.SchoolzoneException; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.*; | ||
import java.util.UUID; | ||
|
||
@Implementation | ||
@RequiredArgsConstructor | ||
public class FileCreator { | ||
private final FileProperties fileProperties; | ||
|
||
public String upload(MultipartFile file) { | ||
return fileProperties.serverUrl() + save(file); | ||
} | ||
|
||
private String save(MultipartFile file) { | ||
String filename = UUID.randomUUID() + file.getOriginalFilename(); | ||
Path path = fileProperties.path().resolve(filename); | ||
try { | ||
Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING); | ||
} catch (IOException e) { | ||
throw new SchoolzoneException(ErrorCode.FILE_SAVE_ERROR); | ||
} | ||
return path.toString(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/schoolzone/schoolzone_backend_v2/domain/file/implementation/FileReader.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,33 @@ | ||
package schoolzone.schoolzone_backend_v2.domain.file.implementation; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.UrlResource; | ||
import schoolzone.schoolzone_backend_v2.global.annotation.Implementation; | ||
import schoolzone.schoolzone_backend_v2.global.config.file.FileProperties; | ||
import schoolzone.schoolzone_backend_v2.global.error.exception.ErrorCode; | ||
import schoolzone.schoolzone_backend_v2.global.error.exception.SchoolzoneException; | ||
|
||
import java.net.MalformedURLException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
@Implementation | ||
@RequiredArgsConstructor | ||
public class FileReader { | ||
private final FileProperties fileProperties; | ||
|
||
public Resource loadFileAsResource(String fileName) { | ||
Path path = Paths.get(fileProperties.path() + fileName).normalize(); | ||
try { | ||
Resource resource = new UrlResource(path.toUri()); | ||
if (!resource.exists()) { | ||
return resource; | ||
} else { | ||
throw new SchoolzoneException(ErrorCode.FILE_NOT_FOUND); | ||
} | ||
} catch (MalformedURLException e) { | ||
throw new SchoolzoneException(ErrorCode.INVALID_URL_FORMAT); | ||
} | ||
} | ||
} |
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
5 changes: 0 additions & 5 deletions
5
.../schoolzone/schoolzone_backend_v2/domain/file/presentation/dto/response/FileResponse.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
...hoolzone/schoolzone_backend_v2/domain/file/presentation/dto/response/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 schoolzone.schoolzone_backend_v2.domain.file.presentation.dto.response; | ||
|
||
import org.springframework.core.io.Resource; | ||
|
||
public record FileResponseDto( | ||
Resource resource, | ||
String contentType | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
...lzone/schoolzone_backend_v2/domain/file/presentation/dto/response/FileUrlResponseDto.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 schoolzone.schoolzone_backend_v2.domain.file.presentation.dto.response; | ||
|
||
public record FileUrlResponseDto( | ||
String url | ||
) { | ||
public static FileUrlResponseDto from(String url) { | ||
return new FileUrlResponseDto(url); | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
src/main/java/schoolzone/schoolzone_backend_v2/domain/file/repository/FileRepository.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/main/java/schoolzone/schoolzone_backend_v2/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,18 @@ | ||
package schoolzone.schoolzone_backend_v2.domain.file.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import schoolzone.schoolzone_backend_v2.domain.file.implementation.FileCreator; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class CommandFileService { | ||
private final FileCreator fileCreator; | ||
|
||
public String create(MultipartFile file) { | ||
return fileCreator.upload(file); | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
src/main/java/schoolzone/schoolzone_backend_v2/domain/file/service/GetFileService.java
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
src/main/java/schoolzone/schoolzone_backend_v2/domain/file/service/PostFileService.java
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
src/main/java/schoolzone/schoolzone_backend_v2/domain/file/service/QueryFileService.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 schoolzone.schoolzone_backend_v2.domain.file.service; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Service; | ||
import schoolzone.schoolzone_backend_v2.domain.file.implementation.FileReader; | ||
import schoolzone.schoolzone_backend_v2.domain.file.presentation.dto.response.FileResponseDto; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class QueryFileService { | ||
private final FileReader fileReader; | ||
|
||
public FileResponseDto getFile(String fileName, HttpServletRequest request) { | ||
Resource resource = fileReader.loadFileAsResource(fileName); | ||
String contentType = getContentType(resource, request); | ||
return new FileResponseDto(resource, contentType); | ||
} | ||
|
||
private String getContentType(Resource resource, HttpServletRequest request) { | ||
String contentType = "application/octet-stream"; | ||
try { | ||
String mimeType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); | ||
if (mimeType != null) { | ||
contentType = mimeType; | ||
} | ||
} | ||
catch (IOException ignored) { | ||
} | ||
return contentType; | ||
} | ||
|
||
} |
34 changes: 0 additions & 34 deletions
34
src/main/java/schoolzone/schoolzone_backend_v2/domain/file/util/FileSaveUtil.java
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/main/java/schoolzone/schoolzone_backend_v2/global/config/file/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,21 @@ | ||
package schoolzone.schoolzone_backend_v2.global.config.file; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.bind.ConstructorBinding; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
@ConfigurationProperties(prefix = "file") | ||
public record FileProperties( | ||
Path path, | ||
String serverUrl | ||
) { | ||
@ConstructorBinding | ||
public FileProperties(String path, String serverUrl) { | ||
this( | ||
Paths.get(path), | ||
serverUrl | ||
); | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
...main/java/schoolzone/schoolzone_backend_v2/global/config/properties/ServerProperties.java
This file was deleted.
Oops, something went wrong.
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