11package com .semosan .api .domain .image .service ;
22
3+ import com .semosan .api .common .config .MinioProperties ;
34import com .semosan .api .common .exception .GeneralException ;
45import com .semosan .api .common .status .ErrorStatus ;
56import com .semosan .api .domain .image .dto .response .PresignedUrlResponse ;
67import io .minio .GetPresignedObjectUrlArgs ;
78import io .minio .MinioClient ;
89import io .minio .http .Method ;
910import lombok .RequiredArgsConstructor ;
10- import org .springframework .beans .factory .annotation .Value ;
1111import org .springframework .stereotype .Service ;
1212
13+ import java .util .Map ;
1314import java .util .Set ;
1415import java .util .UUID ;
1516import java .util .concurrent .TimeUnit ;
@@ -20,14 +21,15 @@ public class ImageService {
2021
2122 public static final Set <String > ALLOWED_BUCKETS = Set .of ("reviews" , "mountains" , "restaurants" , "posts" , "semofeed" );
2223 private static final Set <String > ALLOWED_EXTENSIONS = Set .of (".jpg" , ".jpeg" , ".png" , ".webp" );
24+ private static final Map <String , String > CONTENT_TYPE_MAP = Map .of (
25+ ".jpg" , "image/jpeg" ,
26+ ".jpeg" , "image/jpeg" ,
27+ ".png" , "image/png" ,
28+ ".webp" , "image/webp"
29+ );
2330
2431 private final MinioClient minioClient ;
25-
26- @ Value ("${minio.endpoint}" )
27- private String endpoint ;
28-
29- @ Value ("${minio.public-url}" )
30- private String publicUrl ;
32+ private final MinioProperties minioProperties ;
3133
3234 public PresignedUrlResponse generatePresignedUrl (String bucket , String filename ) {
3335 validateBucket (bucket );
@@ -37,17 +39,19 @@ public PresignedUrlResponse generatePresignedUrl(String bucket, String filename)
3739 String key = UUID .randomUUID () + extension ;
3840
3941 try {
42+ String contentType = CONTENT_TYPE_MAP .get (extension .toLowerCase ());
4043 String uploadUrl = minioClient .getPresignedObjectUrl (
4144 GetPresignedObjectUrlArgs .builder ()
4245 .method (Method .PUT )
4346 .bucket (bucket )
4447 .object (key )
4548 .expiry (10 , TimeUnit .MINUTES )
49+ .extraHeaders (Map .of ("Content-Type" , contentType ))
4650 .build ()
4751 );
4852
49- uploadUrl = uploadUrl .replace (endpoint , publicUrl );
50- String imageUrl = publicUrl + "/" + bucket + "/" + key ;
53+ uploadUrl = uploadUrl .replace (minioProperties . endpoint (), minioProperties . publicUrl () );
54+ String imageUrl = minioProperties . publicUrl () + "/" + bucket + "/" + key ;
5155
5256 return new PresignedUrlResponse (uploadUrl , imageUrl );
5357 } catch (Exception e ) {
0 commit comments