-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathImage.java
More file actions
42 lines (36 loc) · 1.59 KB
/
Image.java
File metadata and controls
42 lines (36 loc) · 1.59 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
package nextstep.courses.domain;
public class Image {
private static final int MAX_IMAGE_SIZE = 1024 * 1024;
private static final int WIDTH_LIMIT = 300;
private static final int HEIGHT_LIMIT = 200;
private static final double REQUIRED_ASPECT_RATIO = 1.5;
private static final double ASPECT_RATIO_TOLERANCE = 0.01;
private String fileName;
private String contentType;
private long sizeInBytes;
private int width;
private int height;
public Image(String fileName, String contentType, long sizeInBytes, int width, int height) {
validate(contentType, sizeInBytes, width, height);
this.fileName = fileName;
this.contentType = contentType;
this.sizeInBytes = sizeInBytes;
this.width = width;
this.height = height;
}
private void validate(String contentType, long sizeInBytes, int width, int height) {
if (sizeInBytes > MAX_IMAGE_SIZE) {
throw new IllegalArgumentException("이미지 크기는 1MB 이하여야 합니다.");
}
if (!contentType.matches("(gif|jpeg|jpg|png|svg)")) {
throw new IllegalArgumentException("허용되지 않는 이미지 형식입니다.");
}
if (width < WIDTH_LIMIT || height < HEIGHT_LIMIT) {
throw new IllegalArgumentException("이미지 크기가 너무 작습니다.");
}
double ratio = (double) width / height;
if (Math.abs(ratio - REQUIRED_ASPECT_RATIO) > ASPECT_RATIO_TOLERANCE) {
throw new IllegalArgumentException("이미지 비율은 3:2여야 합니다.");
}
}
}