Merge pull request #58 from Kitzanos/s3-document-issue-prod

Cherry-pick(Fixed S3 duplicate documnet issue)
This commit is contained in:
rbonazzo-KZ
2024-10-22 20:58:09 +02:00
committed by GitHub
9 changed files with 67 additions and 92 deletions

View File

@@ -226,5 +226,8 @@ public class GepafinConstant {
public static final String DASHBOARD_WIDGET_FETCHED_SUCCESSFULLY="dashboard.widget.fetched.successfully";
public static final String GET_ERROR_S3 = "get.error.s3";
}

View File

@@ -127,8 +127,9 @@ public class CallDao {
ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
for (DocumentEntity document : documents) {
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFileName())) {
ZipEntry zipEntry = new ZipEntry(document.getFileName());
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFilePath())) {
String fileName = Utils.extractFileName(document.getFilePath());
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
IOUtils.copy(fileInputStream, zos);
zos.closeEntry();

View File

@@ -7,7 +7,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +23,7 @@ import net.gepafin.tendermanagement.entities.UserEntity;
import net.gepafin.tendermanagement.enums.UserCompanyDelegationStatusEnum;
import net.gepafin.tendermanagement.model.request.CompanyDelegationRequest;
import net.gepafin.tendermanagement.model.response.CompanyDelegationResponse;
import net.gepafin.tendermanagement.model.response.UploadFileOnAmazonS3Response;
import net.gepafin.tendermanagement.model.response.UserResponseBean;
import net.gepafin.tendermanagement.repositories.DocumentRepository;
import net.gepafin.tendermanagement.repositories.UserCompanyDelegationRepository;
@@ -96,7 +96,7 @@ public class DelegationDao {
companyDao.getUserWithCompany(userEntity.getId(), companyId);
updatePlaceholdersForDelegation(user, companyEntity, placeholders, companyDelegationRequest);
DocumentEntity documentEntity = documentRepository.findBySource(GepafinConstant.DELEGATION_TEMPLATE).get(0);
return generateDocument(placeholders, documentEntity.getFileName());
return generateDocument(placeholders, documentEntity.getFilePath());
}
private Map<String, String> updatePlaceholdersForDelegation(UserResponseBean user, CompanyEntity companyEntity,
@@ -179,7 +179,7 @@ public class DelegationDao {
userCompanyDelegationEntity.setStatus(UserCompanyDelegationStatusEnum.INACTIVE.getValue());
userCompanyDelegationRepository.save(userCompanyDelegationEntity);
}
UploadFileOnAmazonS3 uploadFileOnAmazonS3 = uploadFileOnAmazonS3(file);
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = amazonS3Service.uploadFileOnAmazonS3(s3Folder, file);
userCompanyDelegationEntity = new UserCompanyDelegationEntity();
userCompanyDelegationEntity.setCompanyId(companyId);
userCompanyDelegationEntity.setUserId(userEntity.getId());
@@ -187,8 +187,8 @@ public class DelegationDao {
userCompanyDelegationEntity.setBeneficiaryId(userEntity.getBeneficiary().getId());
}
userCompanyDelegationEntity.setStatus(UserCompanyDelegationStatusEnum.ACTIVE.getValue());
userCompanyDelegationEntity.setFileName(uploadFileOnAmazonS3.fileName());
userCompanyDelegationEntity.setFilePath(uploadFileOnAmazonS3.filepath());
userCompanyDelegationEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
userCompanyDelegationEntity.setFilePath(uploadFileOnAmazonS3Response.getFilePath());
userCompanyDelegationRepository.save(userCompanyDelegationEntity);
return convertUserCompanyDelegationToCompanyDelegationResponse(userCompanyDelegationEntity);
}
@@ -198,25 +198,6 @@ public class DelegationDao {
return Utils.convertSourceObjectToDestinationObject(userCompanyDelegationEntity, CompanyDelegationResponse.class);
}
private UploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file){
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String fileName = org.springframework.util.StringUtils.cleanPath(file.getOriginalFilename());
String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.'));
firstNameContain+=Utils.randomKey(5);
fileName = (firstNameContain + "." + extension);
try {
String filepath = amazonS3Service.upload(fileName, s3Folder, file);
return new UploadFileOnAmazonS3(fileName, filepath);
} catch (Exception e) {
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
}
}
private record UploadFileOnAmazonS3(String fileName, String filepath) {
}
private void validateFileType(MultipartFile file) {
if (file.isEmpty()) {
throw new CustomValidationException(Status.VALIDATION_ERROR,

View File

@@ -1,14 +1,11 @@
package net.gepafin.tendermanagement.dao;
import java.io.IOException;
import java.util.stream.Collectors;
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import net.gepafin.tendermanagement.config.Translator;
@@ -17,10 +14,10 @@ import net.gepafin.tendermanagement.entities.CallEntity;
import net.gepafin.tendermanagement.entities.DocumentEntity;
import net.gepafin.tendermanagement.enums.DocumentTypeEnum;
import net.gepafin.tendermanagement.model.response.DocumentResponseBean;
import net.gepafin.tendermanagement.model.response.UploadFileOnAmazonS3Response;
import net.gepafin.tendermanagement.repositories.DocumentRepository;
import net.gepafin.tendermanagement.service.AmazonS3Service;
import net.gepafin.tendermanagement.service.CallService;
import net.gepafin.tendermanagement.util.Utils;
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
@@ -49,19 +46,17 @@ public class DocumentDao {
List<DocumentEntity> documentEntities = new ArrayList<>();
Long source = resolveSourceId(sourceId, sourceType);
for (MultipartFile file : files) {
try {
uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file);
if (result != null) {
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setFileName(result.fileName());
documentEntity.setSource(sourceType.getValue());
documentEntity.setSourceId(source);
documentEntity.setType(fileType.getValue());
documentEntity.setFilePath(result.filepath());
documentEntity.setIsDeleted(false);
documentEntities.add(documentEntity);
}
} catch (IOException e) {
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = amazonS3Service.uploadFileOnAmazonS3(s3Folder,
file);
if (uploadFileOnAmazonS3Response != null) {
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
documentEntity.setSource(sourceType.getValue());
documentEntity.setSourceId(source);
documentEntity.setType(fileType.getValue());
documentEntity.setFilePath(uploadFileOnAmazonS3Response.getFilePath());
documentEntity.setIsDeleted(false);
documentEntities.add(documentEntity);
}
}
documentRepository.saveAll(documentEntities);
@@ -80,19 +75,6 @@ public class DocumentDao {
return sourceId;
}
private uploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file) throws IOException {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.'));
fileName = (firstNameContain + "." + extension);
String filepath = amazonS3Service.upload(fileName, s3Folder, file);
uploadFileOnAmazonS3 result = new uploadFileOnAmazonS3(fileName, filepath);
return result;
}
private record uploadFileOnAmazonS3(String fileName, String filepath) {
}
public void deleteFile(Long documentId) {
DocumentEntity documentEntity = validateDocument(documentId);
// String fileName= Utils.extractFileName(documentEntity.getFilePath());
@@ -101,13 +83,6 @@ public class DocumentDao {
documentRepository.save(documentEntity);
}
private DocumentEntity deleteFileOnAmazonS3(String fileName) {
try {
amazonS3Service.delete(s3Folder, fileName);
} catch (Exception e) {
}
return null;
}
public DocumentEntity validateDocument(Long id) {
return documentRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
@@ -116,16 +91,10 @@ public class DocumentDao {
public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) {
DocumentEntity documentEntity = validateDocument(documentId);
String fileName = Utils.extractFileName(documentEntity.getFilePath());
deleteFileOnAmazonS3(fileName);
uploadFileOnAmazonS3 result = null;
try {
result = uploadFileOnAmazonS3(file);
} catch (IOException e) {
}
if (result != null) {
documentEntity.setFilePath(result.filepath);
documentEntity.setFileName(result.fileName);
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = amazonS3Service.uploadFileOnAmazonS3(s3Folder, file);
if (uploadFileOnAmazonS3Response != null) {
documentEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
documentEntity.setFilePath(uploadFileOnAmazonS3Response.getFilePath());
documentEntity.setType(documentTypeEnum.getValue());
documentEntity.setSource(documentEntity.getSource());
documentEntity.setSourceId(documentEntity.getSourceId());

View File

@@ -11,11 +11,10 @@ import java.io.InputStream;
@Component
public interface AmazonS3Service {
public String upload(String fileName, String s3Folder, MultipartFile file) throws IOException;
UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file);
public Boolean delete(String s3Folder, String fileName);
Boolean delete(String s3Folder, String filePath);
InputStream getFile(String s3Folder, String filePath) throws IOException;
public UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file);
}

View File

@@ -12,6 +12,8 @@ import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationExceptio
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
@@ -29,6 +31,8 @@ import java.util.Optional;
@Service
public class AmazonS3ServiceImpl implements AmazonS3Service {
private final Logger log = LoggerFactory.getLogger(AmazonS3ServiceImpl.class);
@Autowired
private AmazonS3 amazonS3;
@@ -42,11 +46,9 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
private String s3Url;
@Override
public String upload(String fileName, String s3Folder,
private String upload(String fileName, String s3Folder,
MultipartFile file) throws IOException {
// String path = bucketName+"/"+s3Folder;
String path = s3Folder +"/"+fileName;
InputStream inputStream = file.getInputStream();
@@ -66,17 +68,23 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
if(Boolean.FALSE.equals(isTestProfileActivated())) {
amazonS3.putObject(bucketName, path, inputStream, objectMetadata);
}
return s3Url + s3Folder +"/"+ fileName;
path =s3Url + s3Folder +"/"+ fileName;
log.info("File '{}' uploaded successfully to Amazon S3 with URL: {}", fileName, path);
return path;
}
@Override
public Boolean delete(String s3Folder, String fileName) {
public Boolean delete(String s3Folder, String filePath) {
String fileName = Utils.extractFileName(filePath);
String path = s3Folder +"/"+fileName;
final DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, path);
if(Boolean.FALSE.equals(isTestProfileActivated())) {
amazonS3.deleteObject(deleteObjectRequest);
}
log.info("File '{}' deleted successfully from Amazon S3", fileName);
return true;
}
public Boolean isTestProfileActivated() {
@@ -85,28 +93,34 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
}
@Override
public InputStream getFile(String s3Folder, String filePath) throws IOException {
public InputStream getFile(String s3Folder, String filePath) {
try {
String path = s3Folder +"/"+filePath;
String fileName = Utils.extractFileName(filePath);
String path = s3Folder + "/" + fileName;
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, path);
S3Object s3Object = amazonS3.getObject(getObjectRequest);
log.info("File fetched successfully from Amazon S3: {}", fileName);
return s3Object.getObjectContent();
} catch (AmazonS3Exception e) {
throw new IOException("Error getting file from Amazon S3", e);
log.error("Error occurred while getting file from Amazon S3: {}", e);
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.GET_ERROR_S3));
}
}
@Override
public UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file) {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String fileName = org.springframework.util.StringUtils.cleanPath(file.getOriginalFilename());
String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.'));
firstNameContain+=Utils.randomKey(5);
fileName = (firstNameContain + "." + extension);
String originalFileName = org.springframework.util.StringUtils.cleanPath(file.getOriginalFilename());
String firstNameContain = originalFileName.substring(0, originalFileName.lastIndexOf('.'));
firstNameContain = Utils.replaceSpacesWithUnderscores(firstNameContain);
firstNameContain += "_" + Utils.randomKey(7);
String fileName = (firstNameContain + "." + extension);
try {
String filepath = upload(fileName, s3Folder, file);
return UploadFileOnAmazonS3Response.builder().fileName(fileName).filePath(filepath).build();
return UploadFileOnAmazonS3Response.builder().fileName(originalFileName).filePath(filepath).build();
} catch (Exception e) {
log.error("Error occurred while uploading file from Amazon S3: {}", e);
throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
}

View File

@@ -303,4 +303,11 @@ public class Utils {
return text.replace(target, replacement != null ? replacement : "");
}
public static String replaceSpacesWithUnderscores(String content) {
if (content == null) {
return null;
}
return content.trim().replace(" ", "_");
}
}

View File

@@ -248,5 +248,5 @@ application.signed.document.not.found=Signed document for the application not fo
delete.signed.document.file.success=Signed document deleted successfully.
dashboard.widget.fetched.successfully=Dasboard widget fetched sucessfully.
get.error.s3=Failed to fetch the file from S3.

View File

@@ -244,4 +244,5 @@ application.signed.document.not.found=Documento firmato per l'applicazione non t
delete.signed.document.file.success=Documento firmato eliminato con successo.
dashboard.widget.fetched.successfully=Widget dashboard recuperato correttamente.
get.error.s3=Impossibile recuperare il file da S3.