diff --git a/backend/src/main/java/ervu/client/fileupload/ErvuFileUploadClient.java b/backend/src/main/java/ervu/client/fileupload/ErvuFileUploadClient.java new file mode 100644 index 00000000..20f492e6 --- /dev/null +++ b/backend/src/main/java/ervu/client/fileupload/ErvuFileUploadClient.java @@ -0,0 +1,10 @@ +package ervu.client.fileupload; + +import org.springframework.web.multipart.MultipartFile; + +/** + * @author Alexandr Shalaginov + */ +public interface ErvuFileUploadClient { + boolean uploadFile(MultipartFile multipartFile); +} diff --git a/backend/src/main/java/ervu/client/fileupload/impl/ErvuFileUploadClientImpl.java b/backend/src/main/java/ervu/client/fileupload/impl/ErvuFileUploadClientImpl.java new file mode 100644 index 00000000..56ab9289 --- /dev/null +++ b/backend/src/main/java/ervu/client/fileupload/impl/ErvuFileUploadClientImpl.java @@ -0,0 +1,51 @@ +package ervu.client.fileupload.impl; + +import ervu.client.fileupload.ErvuFileUploadClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.multipart.MultipartFile; + +/** + * @author Alexandr Shalaginov + */ +@Component +public class ErvuFileUploadClientImpl implements ErvuFileUploadClient { + @Value("${file.upload.url:#{null}}") + private String url; + private final RestTemplate restTemplate; + private static final Logger logger = LoggerFactory.getLogger(ErvuFileUploadClientImpl.class); + + public ErvuFileUploadClientImpl() { + this.restTemplate = new RestTemplate(); + } + + @Override + public boolean uploadFile(MultipartFile multipartFile) { + if (this.url == null) { + throw new RuntimeException("Property file.upload.url is null"); + } + try { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("file", multipartFile.getResource()); + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + ResponseEntity response = this.restTemplate.postForEntity(url, requestEntity, Object.class); + return response.getStatusCode().is2xxSuccessful(); + } + catch (RestClientException e) { + logger.error("Upload file failed", e); + return false; + } + } +} diff --git a/backend/src/main/java/ervu/service/fileupload/impl/ErvuFileUploadV2ServiceImpl.java b/backend/src/main/java/ervu/service/fileupload/impl/ErvuFileUploadV2ServiceImpl.java new file mode 100644 index 00000000..18d1a1ba --- /dev/null +++ b/backend/src/main/java/ervu/service/fileupload/impl/ErvuFileUploadV2ServiceImpl.java @@ -0,0 +1,23 @@ +package ervu.service.fileupload.impl; + +import ervu.client.fileupload.ErvuFileUploadClient; +import ervu.service.fileupload.FileUploadV2Service; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +/** + * @author Alexandr Shalaginov + */ +@Service +public class ErvuFileUploadV2ServiceImpl implements FileUploadV2Service { + private final ErvuFileUploadClient ervuFileUploadClient; + + public ErvuFileUploadV2ServiceImpl(ErvuFileUploadClient ervuFileUploadClient) { + this.ervuFileUploadClient = ervuFileUploadClient; + } + + @Override + public boolean saveFile(MultipartFile multipartFile) { + return this.ervuFileUploadClient.uploadFile(multipartFile); + } +}