SUPPORT-8381: some changes (2)
This commit is contained in:
parent
35dbbccf3e
commit
0bb98990e2
5 changed files with 58 additions and 16 deletions
|
|
@ -1,3 +1,5 @@
|
|||
package ervu;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -1,10 +1,8 @@
|
|||
package ervu.client.fileupload;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* @author Alexandr Shalaginov
|
||||
*/
|
||||
public interface ErvuFileUploadClient {
|
||||
boolean uploadFile(MultipartFile multipartFile);
|
||||
boolean uploadFile(String filename, byte[] content);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import ervu.client.fileupload.ErvuFileUploadClient;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
|
|
@ -13,7 +14,6 @@ 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
|
||||
|
|
@ -30,7 +30,7 @@ public class ErvuFileUploadClientImpl implements ErvuFileUploadClient {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean uploadFile(MultipartFile multipartFile) {
|
||||
public boolean uploadFile(String filename, byte[] content) {
|
||||
if (this.url == null) {
|
||||
throw new RuntimeException("Property file.upload.url is null");
|
||||
}
|
||||
|
|
@ -38,7 +38,13 @@ public class ErvuFileUploadClientImpl implements ErvuFileUploadClient {
|
|||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
||||
body.add("file", multipartFile.getResource());
|
||||
ByteArrayResource resource = new ByteArrayResource(content) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
};
|
||||
body.add("file", resource);
|
||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
|
||||
ResponseEntity<?> response = this.restTemplate.postForEntity(url, requestEntity, Object.class);
|
||||
return response.getStatusCode().is2xxSuccessful();
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.kafka.support.SendResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
|
||||
/**
|
||||
* @author Alexandr Shalaginov
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
package ervu.service.fileupload.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import ervu.client.fileupload.ErvuFileUploadClient;
|
||||
import ervu.client.kafka.KafkaClient;
|
||||
import ervu.service.fileupload.FileUploadV2Service;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
|
@ -18,8 +22,7 @@ public class ErvuFileUploadV2ServiceImpl implements FileUploadV2Service {
|
|||
private final ErvuFileUploadClient ervuFileUploadClient;
|
||||
|
||||
public ErvuFileUploadV2ServiceImpl(ErvuFileUploadClient ervuFileUploadClient,
|
||||
KafkaClient kafkaClient,
|
||||
SecurityContext securityContext) {
|
||||
KafkaClient kafkaClient, SecurityContext securityContext) {
|
||||
this.ervuFileUploadClient = ervuFileUploadClient;
|
||||
this.kafkaClient = kafkaClient;
|
||||
this.securityContext = securityContext;
|
||||
|
|
@ -27,13 +30,48 @@ public class ErvuFileUploadV2ServiceImpl implements FileUploadV2Service {
|
|||
|
||||
@Override
|
||||
public boolean saveFile(MultipartFile multipartFile) {
|
||||
if (this.ervuFileUploadClient.uploadFile(multipartFile)) {
|
||||
String message = "{username='" + this.securityContext.getCurrentUsername()
|
||||
+ "',filename='" + multipartFile.getName() + "'}";
|
||||
return this.kafkaClient.sendMessage(message);
|
||||
try {
|
||||
String newFilename = getNewFilename(multipartFile.getOriginalFilename());
|
||||
byte[] content = multipartFile.getBytes();
|
||||
|
||||
if (this.ervuFileUploadClient.uploadFile(newFilename, content)) {
|
||||
return this.kafkaClient.sendMessage(new KafkaMessage(
|
||||
securityContext.getCurrentUsername(),
|
||||
newFilename,
|
||||
multipartFile.getOriginalFilename()
|
||||
).toString());
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String getNewFilename(String oldFilename) {
|
||||
return UUID.randomUUID() + "." + FilenameUtils.getExtension(oldFilename);
|
||||
}
|
||||
}
|
||||
|
||||
class KafkaMessage {
|
||||
private String username;
|
||||
private String filename;
|
||||
private String originalFilename;
|
||||
|
||||
public KafkaMessage(String username, String filename, String originalFilename) {
|
||||
this.username = username;
|
||||
this.filename = filename;
|
||||
this.originalFilename = originalFilename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "KafkaMessage{" +
|
||||
"username='" + username + '\'' +
|
||||
", filename='" + filename + '\'' +
|
||||
", originalFilename='" + originalFilename + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue