Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] 이미지 압축방식 개선 #43

Merged
merged 2 commits into from
Jan 26, 2024
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
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ dependencies {
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'
// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.7.14'
// spring cache
// Spring cache
implementation 'org.springframework.boot:spring-boot-starter-cache:2.7.14'
// Snappy Java
implementation 'org.xerial.snappy:snappy-java:1.1.10.5'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import server.inuappcenter.kr.data.utils.ImageUtils;

import javax.persistence.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Entity
@Getter
Expand Down Expand Up @@ -73,11 +72,6 @@ public void isThumbnail() {
this.isThumbnail = true;
}

// 자원의 현재 위치를 반환하는 메소드
public String getLocation(HttpServletRequest request, Image image) {
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/image/photo/" + image.getId().toString();
}

public Image returnMultipartToEntity(MultipartFile multipartFile) throws IOException {
return new Image(multipartFile.getOriginalFilename(), ImageUtils.compressImage(multipartFile.getBytes()), multipartFile.getSize());
}
Expand Down
45 changes: 9 additions & 36 deletions src/main/java/server/inuappcenter/kr/data/utils/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,21 @@
package server.inuappcenter.kr.data.utils;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import org.xerial.snappy.Snappy;
import java.io.IOException;

public class ImageUtils {

public static byte[] compressImage(byte[] data) {
Deflater deflater = new Deflater();
deflater.setLevel(Deflater.BEST_COMPRESSION);
deflater.setInput(data);
deflater.finish();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] tmp = new byte[4*1024];

while (!deflater.finished()) {
int size = deflater.deflate(tmp);
outputStream.write(tmp, 0, size);
}

try {
outputStream.close();
} catch (Exception ignored) {

}
public class ImageUtils {

return outputStream.toByteArray();
public static byte[] compressImage(byte[] data) throws IOException {
return Snappy.compress(data);
}

public static byte[] decompressImage(byte[] data) {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputSteam = new ByteArrayOutputStream(data.length);
byte[] tmp = new byte[4*1024];

try {
while (!inflater.finished()) {
int count = inflater.inflate(tmp);
outputSteam.write(tmp, 0, count);
}
outputSteam.close();
} catch (Exception ignored) {
return Snappy.uncompress(data);
} catch(Exception e) {
throw new RuntimeException(e.getMessage());
}
return outputSteam.toByteArray();
}

}
10 changes: 5 additions & 5 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
spring:
redis:
port: 5379
host: na2ru2.me
port: 6379
host: 192.168.55.22
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: dev
url: jdbc:mysql://na2ru2.me:6306/appcenter
url: jdbc:mysql://192.168.55.22/appcenter
password: test1234
jpa:
hibernate:
Expand All @@ -20,5 +20,5 @@ spring:
mode: always
servlet:
multipart:
maxFileSize: 50MB
maxRequestSize: 50MB
maxFileSize: 15MB
maxRequestSize: 15MB
Loading