resolved conflicts

This commit is contained in:
rajesh
2024-10-23 00:14:01 +05:30
parent 5ef5cecef3
commit 4386aaafdd
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 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)) { ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
for (DocumentEntity document : documents) { for (DocumentEntity document : documents) {
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFileName())) { try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFilePath())) {
ZipEntry zipEntry = new ZipEntry(document.getFileName()); String fileName = Utils.extractFileName(document.getFilePath());
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry); zos.putNextEntry(zipEntry);
IOUtils.copy(fileInputStream, zos); IOUtils.copy(fileInputStream, zos);
zos.closeEntry(); zos.closeEntry();

View File

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

View File

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

View File

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

View File

@@ -303,4 +303,11 @@ public class Utils {
return text.replace(target, replacement != null ? replacement : ""); 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. delete.signed.document.file.success=Signed document deleted successfully.
dashboard.widget.fetched.successfully=Dasboard widget fetched sucessfully. 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. delete.signed.document.file.success=Documento firmato eliminato con successo.
dashboard.widget.fetched.successfully=Widget dashboard recuperato correttamente. dashboard.widget.fetched.successfully=Widget dashboard recuperato correttamente.
get.error.s3=Impossibile recuperare il file da S3.