관리자가 회사 로고를 업로드하면 스프링 프로젝트 파일의 src/main/resources/upload
경로에 저장됩니다.
이미지 파일의 이름은 중복되지 않도록 랜덤 UUID를 통해 저장합니다.
저장된 파일은 DB의 회사 테이블의 logo_url 컬럼에 저장됩니다.
@RestController
@RequiredArgsConstructor
public class ResourceController {
private final ResourceLoader resourceLoader;
@GetMapping("/images/{fileName}")
public ResponseEntity<Resource> serveImage(@PathVariable String fileName) {
Resource image = resourceLoader.getResource(FileUtil.URL_PATH + fileName);
HttpHeaders headers = new HttpHeaders();
if (fileName.endsWith(".svg")) {
headers.setContentType(MediaType.valueOf("image/svg+xml"));
}
return ResponseEntity.ok().headers(headers).body(image);
}
}
저장된 이미지 파일은 스프링의 ResourceLoader
를 통해 불러온 다음, ResponseBody를 통해 응답 바디에 담아 응답합니다.
http://localhost:8080/images/{이미지파일이름.확장자}
경로로 GET 요청시 이미지 파일이 응답되는 것을 확인할 수 있습니다.
현재 로컬환경에서 개발하고 있기 때문에, 각자 업로드 테스트를 할 시 upload 디렉토리에 파일이 점점 추가되게 됩니다.
이 디렉토리를 깃으로 관리하게 된다면 깃허브에 필요없는 파일이 올라가게 됩니다.