-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS3ImageService.java
More file actions
148 lines (126 loc) · 4.2 KB
/
Copy pathS3ImageService.java
File metadata and controls
148 lines (126 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.dropit.global.storage;
import com.dropit.global.exception.ServiceException;
import io.awspring.cloud.s3.ObjectMetadata;
import io.awspring.cloud.s3.S3Template;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.exception.SdkException;
import java.io.IOException;
import java.time.Duration;
import java.util.Map;
import java.util.UUID;
@Slf4j
@Service
@RequiredArgsConstructor
public class S3ImageService {
private static final Duration URL_EXPIRATION = Duration.ofMinutes(10);
private static final String TEMPORARY_IMAGE_CACHE_CONTROL = "no-store";
private static final String IMAGE_CACHE_CONTROL = "public, max-age=31536000, immutable";
private static final Map<String, String> ALLOWED_IMAGE_TYPES = Map.of(
"image/jpeg", ".jpg",
"image/png", ".png",
"image/webp", ".webp"
);
private final S3Template s3Template;
@Value("${spring.cloud.aws.s3.bucket}")
private String bucket;
@Value("${app.cdn.domain}")
private String cdnDomain;
public ImageUploadResult upload(
MultipartFile file,
String directory
) {
validate(file);
String contentType = file.getContentType();
String extension = ALLOWED_IMAGE_TYPES.get(contentType);
String imageId = UUID.randomUUID().toString();
String sourceKey =
"incoming/"
+ directory
+ "/"
+ imageId
+ extension;
String optimizedKey =
"optimized/"
+ directory
+ "/"
+ imageId
+ ".webp";
try {
ObjectMetadata metadata = ObjectMetadata.builder()
.contentType(contentType)
.cacheControl(TEMPORARY_IMAGE_CACHE_CONTROL)
.build();
s3Template.upload(
bucket,
sourceKey,
file.getInputStream(),
metadata
);
return new ImageUploadResult(
sourceKey,
optimizedKey
);
} catch (IOException | SdkException exception) {
throw new ServiceException(
StorageErrorCode.FILE_UPLOAD_FAILED
);
}
}
public String createDownloadUrl(String key) {
if (key == null) {
return null;
}
try {
return s3Template.createSignedGetURL(
bucket,
key,
URL_EXPIRATION
).toString();
} catch (SdkException exception) {
throw new ServiceException(
StorageErrorCode.FILE_URL_CREATION_FAILED
);
}
}
public String createPublicUrl(String key) {
if (key == null) {
return null;
}
return "https://" + cdnDomain + "/" + key;
}
public void deleteQuietly(String key) {
if (key == null) {
return;
}
try {
s3Template.deleteObject(bucket, key);
} catch (SdkException exception) {
/*
* The DB transaction has already succeeded.
* Failing the HTTP request now would not restore that transaction,
* so log the orphaned key for later cleanup.
*/
log.warn(
"Failed to delete obsolete S3 image. key={}",
key,
exception
);
}
}
private void validate(MultipartFile file) {
if (file == null || file.isEmpty()) {
throw new ServiceException(
StorageErrorCode.EMPTY_FILE
);
}
if (!ALLOWED_IMAGE_TYPES.containsKey(file.getContentType())) {
throw new ServiceException(
StorageErrorCode.UNSUPPORTED_IMAGE_TYPE
);
}
}
}