-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathImage.java
More file actions
76 lines (59 loc) · 1.88 KB
/
Image.java
File metadata and controls
76 lines (59 loc) · 1.88 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
package nextstep.courses.domain;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Image {
private static final Set<String> ALLOWED_FILE_FORMAT = new HashSet<>(List.of("gif", "jpg", "jpeg", "png", "svg"));
private Long id;
private final File file;
private String imageUrl;
private int width;
private int height;
// DB용 생성자 (id 포함, 검증 생략 가능)
public Image(Long id, float fileSize, String fileType, String imageUrl, int width, int height) {
this(fileSize, fileType, imageUrl, width, height);
this.id = id;
}
public Image(float fileSize, String fileType, String imageUrl, int width, int height) {
this.file = new File(ALLOWED_FILE_FORMAT, fileSize, fileType);
this.imageUrl = imageUrl;
this.width = width;
this.height = height;
validate();
}
private void validate() {
validateFileSize();
validateFileType();
validateFileRatio();
}
private void validateFileSize() {
if (file.getSize() > 1024) {
throw new IllegalArgumentException("FileSize Should be under or equal to 1MB");
}
}
private void validateFileType() {
if (!file.allowedFileType(file.getType())) {
throw new IllegalArgumentException("Allowed file types are only gif, jpg/jpeg,png, svg");
}
}
private void validateFileRatio() {
if (width * 2 != height * 3) {
throw new IllegalArgumentException("The ratio of width:height must be 3:2");
}
}
public float getSize() {
return file.getSize();
}
public String getType(){
return file.getType();
}
public String getImageUrl() {
return imageUrl;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}