Skip to content

Commit

Permalink
refactor: 채팅방 파일 다운로드 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
kjungw1025 committed Feb 8, 2024
1 parent 351d29f commit 120e51d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public WebClient plainWebClient() {

return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(client))
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(10 * 1024 * 1024))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,33 @@

import com.dku.council.infra.nhn.global.service.service.NHNAuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class ObjectDownloadService {

private final WebClient webClient;
private final NHNAuthService nhnAuthService;

public ResponseEntity<byte[]> downloadObject(String fileName, String fileUrl) {

// RestTemplate 생성
RestTemplate restTemplate = new RestTemplate();

// 헤더 생성
HttpHeaders headers = new HttpHeaders();
headers.add("X-Auth-Token", nhnAuthService.requestToken());
headers.setAccept(List.of(MediaType.APPLICATION_OCTET_STREAM));
headers.setContentDispositionFormData("attachment", fileName);

HttpEntity<String> requestHttpEntity = new HttpEntity<String>(null, headers);

// API 호출, 데이터를 바이트 배열로 받음
ResponseEntity<byte[]> response = restTemplate.exchange(fileUrl, HttpMethod.GET, requestHttpEntity, byte[].class);

return response;
return webClient.get()
.uri(fileUrl)
.headers(httpHeaders -> httpHeaders.addAll(headers))
.exchangeToMono(value -> value.toEntity(new ParameterizedTypeReference<byte[]>() {
}))
.block();
}

}

0 comments on commit 120e51d

Please sign in to comment.