1+ package com .runtracker .domain .upload .controller ;
2+
3+ import com .runtracker .domain .upload .service .FileStorageService ;
4+ import com .runtracker .global .response .ApiResponse ;
5+ import lombok .RequiredArgsConstructor ;
6+ import org .springframework .core .io .Resource ;
7+ import org .springframework .http .HttpHeaders ;
8+ import org .springframework .http .MediaType ;
9+ import org .springframework .http .ResponseEntity ;
10+ import org .springframework .web .bind .annotation .*;
11+ import org .springframework .web .multipart .MultipartFile ;
12+
13+ import java .util .HashMap ;
14+ import java .util .Map ;
15+
16+ @ RestController
17+ @ RequestMapping ("/api/upload" )
18+ @ RequiredArgsConstructor
19+ public class UploadController {
20+
21+ private final FileStorageService fileStorageService ;
22+
23+ @ PostMapping ("/image" )
24+ public ApiResponse <Map <String , String >> uploadImage (
25+ @ RequestParam ("file" ) MultipartFile file ) {
26+
27+ String fileUrl = fileStorageService .uploadImage (file );
28+
29+ Map <String , String > response = new HashMap <>();
30+ response .put ("url" , fileUrl );
31+
32+ return ApiResponse .ok (response );
33+ }
34+
35+ @ GetMapping ("/image/{filename:.+}" )
36+ public ResponseEntity <Resource > getImage (@ PathVariable String filename ) {
37+
38+ Resource resource = fileStorageService .loadFileAsResource (filename );
39+ String contentType = fileStorageService .determineContentType (resource );
40+
41+ return ResponseEntity .ok ()
42+ .contentType (MediaType .parseMediaType (contentType ))
43+ .header (HttpHeaders .CONTENT_DISPOSITION , "inline; filename=\" " + resource .getFilename () + "\" " )
44+ .body (resource );
45+ }
46+ }
0 commit comments