Skip to content

Commit 2f743a5

Browse files
authored
feature/#185 image resizing 기능 구현 (#193)
1 parent c23b115 commit 2f743a5

File tree

7 files changed

+57
-9
lines changed

7 files changed

+57
-9
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ out/
3434
/.nb-gradle/
3535

3636
### VS Code ###
37-
.vscode/
37+
.vscode/
38+
39+
## file
40+
/cloud

aics-api/src/main/resources/db/data.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- about
2-
INSERT INTO about (content, category, description, created_at, updated_at)
3-
VALUES ('학과 소개 내용입니다.', 'DEPT_INTRO', "경기대학교 AI컴퓨터공학부를 소개해요.", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
4-
('경기대학교 수원캠퍼스 8강의동에 있습니다.', 'DIRECTIONS', "경기대학교 AI컴퓨터공학부에 찾아오시는 길을 알려드려요.", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
5-
('이런 저런 동아리가 있습니다.', 'CLUB', "경기대학교 AI컴퓨터공학부에 어떤 동아리가 있는지 알려드려요.", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
2+
INSERT INTO about (content, category, created_at, updated_at)
3+
VALUES ('학과 소개 내용입니다.', 'DEPT_INTRO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
4+
('경기대학교 수원캠퍼스 8강의동에 있습니다.', 'DIRECTIONS', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
5+
('이런 저런 동아리가 있습니다.', 'CLUB', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
66

77
-- club
88
INSERT INTO club (name, description, site, created_at, updated_at)

aics-api/src/main/resources/db/schema.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ CREATE TABLE about
77
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
88
deleted_at TIMESTAMP(6) DEFAULT NULL,
99
content TEXT NOT NULL,
10-
description VARCHAR(100),
1110
category VARCHAR(50) NOT NULL UNIQUE
1211
CONSTRAINT about_category_check
13-
CHECK ((category)::TEXT = ANY (ARRAY ['DEPT_INTRO', 'DIRECTIONS', 'CLUB']))
12+
CHECK ((category)::TEXT = ANY (ARRAY ['DEPT_INTRO', 'DIRECTIONS']))
1413
);
1514

1615
-- club

aics-domain/src/main/java/kgu/developers/domain/file/infrastructure/FileStorageServiceImpl.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828

2929
@Service
3030
public class FileStorageServiceImpl implements FileStorageService {
31+
private static final String IMAGE_CONTENT_TYPE_PREFIX = "image/";
32+
3133
private final Path rootLocation;
3234
private final String url;
3335
private final Set<String> disallowedExtensions;
36+
private final ImageResizingService imageResizingService;
3437

3538
@Autowired
36-
public FileStorageServiceImpl(FilePathProperties filePathProperties) {
39+
public FileStorageServiceImpl(FilePathProperties filePathProperties,
40+
ImageResizingService imageResizingService) {
3741
this.rootLocation = Paths.get(filePathProperties.getUploadPath())
3842
.toAbsolutePath().normalize();
3943
this.url = filePathProperties.getUrl();
@@ -43,6 +47,7 @@ public FileStorageServiceImpl(FilePathProperties filePathProperties) {
4347
} catch (Exception e) {
4448
throw new FileDirectoryCreationFailedException();
4549
}
50+
this.imageResizingService = imageResizingService;
4651
}
4752

4853
@Override
@@ -53,7 +58,15 @@ public String store(MultipartFile file, FileDomain fileDomain) {
5358
try {
5459
Path targetLocation = this.rootLocation.resolve(path);
5560
Files.copy(file.getInputStream(), targetLocation, REPLACE_EXISTING);
56-
String relativePath = this.rootLocation.relativize(targetLocation).toString();
61+
62+
File originalFile = targetLocation.toFile();
63+
64+
String contentType = file.getContentType();
65+
if (contentType != null && contentType.startsWith(IMAGE_CONTENT_TYPE_PREFIX)) {
66+
imageResizingService.imageResize(originalFile, 800, 600, 1);
67+
}
68+
69+
String relativePath = this.rootLocation.relativize(originalFile.toPath()).toString();
5770
return url + "/" + relativePath.replace(File.separator, "/");
5871
} catch (Exception e) {
5972
throw new FileStoreFailedException();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package kgu.developers.domain.file.infrastructure;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public interface ImageResizingService {
7+
File imageResize(File inputFile, int width, int height, double quality) throws IOException;
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kgu.developers.domain.file.infrastructure;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
import net.coobird.thumbnailator.Thumbnails;
9+
10+
@Service
11+
public class ImageResizingServiceImpl implements ImageResizingService {
12+
public File imageResize(File inputFile, int width, int height, double quality) throws IOException {
13+
File outputFile = new File(inputFile.getParent(), inputFile.getName());
14+
15+
Thumbnails.of(inputFile)
16+
.size(width, height)
17+
.outputQuality(quality)
18+
.keepAspectRatio(true)
19+
.toFile(outputFile);
20+
21+
return outputFile;
22+
}
23+
}

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ subprojects {
5454
implementation 'io.jsonwebtoken:jjwt:0.9.1'
5555
implementation 'javax.xml.bind:jaxb-api:2.3.1'
5656

57+
implementation 'net.coobird:thumbnailator:0.4.20'
58+
5759
testFixturesImplementation 'org.springframework.boot:spring-boot-starter-test'
5860
testFixturesRuntimeOnly 'org.junit.platform:junit-platform-launcher'
5961
testFixturesImplementation 'org.springframework.boot:spring-boot-starter-security'

0 commit comments

Comments
 (0)