SUPPORT-8381: add file upload service impl and file upload client
This commit is contained in:
parent
f244f6a0e1
commit
1d137809e9
3 changed files with 84 additions and 0 deletions
|
|
@ -0,0 +1,10 @@
|
|||
package ervu.client.fileupload;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* @author Alexandr Shalaginov
|
||||
*/
|
||||
public interface ErvuFileUploadClient {
|
||||
boolean uploadFile(MultipartFile multipartFile);
|
||||
}
|
||||
|
|
@ -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<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("file", multipartFile.getResource());
|
||||
HttpEntity<MultiValueMap<String, Object>> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue