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] 이미지 비동기 설정 수정 #473

Merged
merged 2 commits into from
Oct 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,6 +21,7 @@
@RequiredArgsConstructor
public class FileService {
private final String tmpLocation = System.getProperty("java.io.tmpdir");
private static final int BUFFER_SIZE = 8192;

public File convertMultipartFileToFile(MultipartFile multipartFile) {
String originalFilename = multipartFile.getOriginalFilename();
Expand All @@ -29,8 +32,13 @@ public File convertMultipartFileToFile(MultipartFile multipartFile) {
File convertFile = new File(
tmpLocation + File.separator + UUID.randomUUID() + "_" + fileName);

try (FileOutputStream fileOutputStream = new FileOutputStream(convertFile)) {
fileOutputStream.write(multipartFile.getBytes());
try (InputStream inputStream = multipartFile.getInputStream();
OutputStream outputStream = new FileOutputStream(convertFile)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new ServerErrorException(FILE_FAIL_ERROR.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public Executor imageUploadExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

executor.setThreadGroupName("imageUploadExecutor");
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(50);
executor.setCorePoolSize(2);
executor.setMaxPoolSize(5);
executor.setQueueCapacity(10);
executor.setKeepAliveSeconds(20);
executor.setThreadNamePrefix("Async ImageUploadExecutor-");
executor.setRejectedExecutionHandler((r, exec) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.jeju.nanaland.global.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import lombok.AllArgsConstructor;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -15,6 +19,7 @@ public class CustomMultipartFile implements MultipartFile {
private final String name;
private final String originalFilename;
private final String contentType;
private static final int BUFFER_SIZE = 8192;

@Override
public String getName() {
Expand Down Expand Up @@ -48,11 +53,18 @@ public byte[] getBytes() throws IOException {

@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
return new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
}

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
Files.copy(file.toPath(), dest.toPath());
try (InputStream in = getInputStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(dest), BUFFER_SIZE)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}