Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ out/
/.nb-gradle/

### VS Code ###
.vscode/
.vscode/

## file
/cloud
8 changes: 4 additions & 4 deletions aics-api/src/main/resources/db/data.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- about
INSERT INTO about (content, category, description, created_at, updated_at)
VALUES ('학과 소개 내용입니다.', 'DEPT_INTRO', "경기대학교 AI컴퓨터공학부를 소개해요.", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('경기대학교 수원캠퍼스 8강의동에 있습니다.', 'DIRECTIONS', "경기대학교 AI컴퓨터공학부에 찾아오시는 길을 알려드려요.", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('이런 저런 동아리가 있습니다.', 'CLUB', "경기대학교 AI컴퓨터공학부에 어떤 동아리가 있는지 알려드려요.", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
INSERT INTO about (content, category, created_at, updated_at)
VALUES ('학과 소개 내용입니다.', 'DEPT_INTRO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('경기대학교 수원캠퍼스 8강의동에 있습니다.', 'DIRECTIONS', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('이런 저런 동아리가 있습니다.', 'CLUB', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),

-- club
INSERT INTO club (name, description, site, created_at, updated_at)
Expand Down
3 changes: 1 addition & 2 deletions aics-api/src/main/resources/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ CREATE TABLE about
updated_at TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP(6) DEFAULT NULL,
content TEXT NOT NULL,
description VARCHAR(100),
category VARCHAR(50) NOT NULL UNIQUE
CONSTRAINT about_category_check
CHECK ((category)::TEXT = ANY (ARRAY ['DEPT_INTRO', 'DIRECTIONS', 'CLUB']))
CHECK ((category)::TEXT = ANY (ARRAY ['DEPT_INTRO', 'DIRECTIONS']))
);

-- club
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@

@Service
public class FileStorageServiceImpl implements FileStorageService {
private static final String IMAGE_CONTENT_TYPE_PREFIX = "image/";

private final Path rootLocation;
private final String url;
private final Set<String> disallowedExtensions;
private final ImageResizingService imageResizingService;

@Autowired
public FileStorageServiceImpl(FilePathProperties filePathProperties) {
public FileStorageServiceImpl(FilePathProperties filePathProperties,
ImageResizingService imageResizingService) {
this.rootLocation = Paths.get(filePathProperties.getUploadPath())
.toAbsolutePath().normalize();
this.url = filePathProperties.getUrl();
Expand All @@ -41,6 +45,7 @@ public FileStorageServiceImpl(FilePathProperties filePathProperties) {
} catch (Exception e) {
throw new FileDirectoryCreationFailedException();
}
this.imageResizingService = imageResizingService;
}

@Override
Expand All @@ -51,7 +56,15 @@ public String store(MultipartFile file, FileDomain fileDomain, Long directoryId)
try {
Path targetLocation = this.rootLocation.resolve(path);
Files.copy(file.getInputStream(), targetLocation, REPLACE_EXISTING);
String relativePath = this.rootLocation.relativize(targetLocation).toString();

File originalFile = targetLocation.toFile();

String contentType = file.getContentType();
if (contentType != null && contentType.startsWith(IMAGE_CONTENT_TYPE_PREFIX)) {
imageResizingService.imageResize(originalFile, 800, 600, 1);
}

String relativePath = this.rootLocation.relativize(originalFile.toPath()).toString();
return url + "/" + relativePath.replace(File.separator, "/");
} catch (Exception e) {
throw new FileStoreFailedException();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kgu.developers.domain.file.infrastructure;

import java.io.File;
import java.io.IOException;

public interface ImageResizingService {
File imageResize(File inputFile, int width, int height, double quality) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kgu.developers.domain.file.infrastructure;

import java.io.File;
import java.io.IOException;

import org.springframework.stereotype.Service;

import net.coobird.thumbnailator.Thumbnails;

@Service
public class ImageResizingServiceImpl implements ImageResizingService {
public File imageResize(File inputFile, int width, int height, double quality) throws IOException {
File outputFile = new File(inputFile.getParent(), inputFile.getName());

Thumbnails.of(inputFile)
.size(width, height)
.outputQuality(quality)
.keepAspectRatio(true)
.toFile(outputFile);

return outputFile;
}
Comment on lines +12 to +22
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

이미지 리사이징 구현 시 몇 가지 개선이 필요합니다.

  1. 입력 파일을 출력 파일로 덮어쓰고 있어 원본 이미지가 손실될 수 있습니다.
  2. 입력 매개변수에 대한 유효성 검사가 없습니다.

다음과 같이 수정하는 것을 제안합니다:

 public File imageResize(File inputFile, int width, int height, double quality) throws IOException {
+    if (width <= 0 || height <= 0 || quality <= 0 || quality > 1.0) {
+        throw new IllegalArgumentException("Invalid parameters: width and height must be positive, quality must be between 0 and 1");
+    }
-    File outputFile = new File(inputFile.getParent(), inputFile.getName());
+    String fileName = inputFile.getName();
+    String name = fileName.substring(0, fileName.lastIndexOf('.'));
+    String ext = fileName.substring(fileName.lastIndexOf('.'));
+    File outputFile = new File(inputFile.getParent(), name + "_resized" + ext);

     Thumbnails.of(inputFile)
         .size(width, height)
         .outputQuality(quality)
         .keepAspectRatio(true)
         .toFile(outputFile);

     return outputFile;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public File imageResize(File inputFile, int width, int height, double quality) throws IOException {
File outputFile = new File(inputFile.getParent(), inputFile.getName());
Thumbnails.of(inputFile)
.size(width, height)
.outputQuality(quality)
.keepAspectRatio(true)
.toFile(outputFile);
return outputFile;
}
public File imageResize(File inputFile, int width, int height, double quality) throws IOException {
if (width <= 0 || height <= 0 || quality <= 0 || quality > 1.0) {
throw new IllegalArgumentException("Invalid parameters: width and height must be positive, quality must be between 0 and 1");
}
String fileName = inputFile.getName();
String name = fileName.substring(0, fileName.lastIndexOf('.'));
String ext = fileName.substring(fileName.lastIndexOf('.'));
File outputFile = new File(inputFile.getParent(), name + "_resized" + ext);
Thumbnails.of(inputFile)
.size(width, height)
.outputQuality(quality)
.keepAspectRatio(true)
.toFile(outputFile);
return outputFile;
}

}
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ subprojects {
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'javax.xml.bind:jaxb-api:2.3.1'

implementation 'net.coobird:thumbnailator:0.4.20'

testFixturesImplementation 'org.springframework.boot:spring-boot-starter-test'
testFixturesRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testFixturesImplementation 'org.springframework.boot:spring-boot-starter-security'
Expand Down