Skip to content

Commit

Permalink
Merge pull request #17 from schoolzone06/refactor/#14
Browse files Browse the repository at this point in the history
�파일 도메인 리팩토링
  • Loading branch information
NameIsUser06 authored May 12, 2024
2 parents febaeac + a8a6c49 commit f8a57cd
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 177 deletions.

This file was deleted.

This file was deleted.

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();
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
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.*;
import org.springframework.web.multipart.MultipartFile;
import schoolzone.schoolzone_backend_v2.domain.file.presentation.dto.response.FileResponse;
import schoolzone.schoolzone_backend_v2.domain.file.service.GetFileService;
import schoolzone.schoolzone_backend_v2.domain.file.service.PostFileService;

import java.io.IOException;
import schoolzone.schoolzone_backend_v2.domain.file.presentation.dto.response.FileResponseDto;
import schoolzone.schoolzone_backend_v2.domain.file.presentation.dto.response.FileUrlResponseDto;
import schoolzone.schoolzone_backend_v2.domain.file.service.CommandFileService;
import schoolzone.schoolzone_backend_v2.domain.file.service.QueryFileService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/file")
public class FileController {
private final PostFileService postFileService;
private final GetFileService getFileService;
private final CommandFileService commandFileService;
private final QueryFileService queryFileService;

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<FileResponse> postFile(@RequestPart(value = "file") MultipartFile file) {
return postFileService.execute(file);
@ResponseStatus(HttpStatus.CREATED)
public FileUrlResponseDto postFile(@RequestPart(value = "file") MultipartFile file) {
return FileUrlResponseDto.from(commandFileService.create(file));
}

@GetMapping
public ResponseEntity<Resource> getFile(@RequestParam Long fileId, HttpServletRequest httpServletRequest) throws IOException {
return getFileService.execute(fileId, httpServletRequest);
@GetMapping("/{fileName}")
public ResponseEntity<Resource> getFile(@PathVariable String fileName, HttpServletRequest request) {
FileResponseDto dto = queryFileService.getFile(fileName, request);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dto.contentType()))
.header("Content-Type", dto.contentType())
.body(dto.resource());
}
}

This file was deleted.

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
) {
}
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);
}
}

This file was deleted.

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);
}
}

This file was deleted.

This file was deleted.

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;
}

}

This file was deleted.

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
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum ErrorCode {

// file
FILE_SAVE_ERROR(500, "FILE-500-1", "파일 저장 중 오류가 발생했습니다."),
FILE_NOT_FOUND(404, "FILE-404-1", "파일을 찾을 수 없습니다.");
FILE_NOT_FOUND(404, "FILE-404-1", "파일을 찾을 수 없습니다."),
INVALID_URL_FORMAT(400, "FILE-400-1", "유효하지 않은 URL 형식입니다");

private final int status;
private final String code;
Expand Down

0 comments on commit f8a57cd

Please sign in to comment.