diff --git a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java index 8111ed64..38e3969a 100644 --- a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java +++ b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java @@ -248,5 +248,6 @@ public class GepafinConstant { public static final String HUB_DELETE_SUCCESS = "hub_delete_success"; public static final String HUB_NOT_FOUND = "hub_not_found"; public static final String EVALUATIONCRITERIA_INVALID = "evaluationCriteria.invalid"; + public static final String GET_ERROR_S3 = "get.error.s3"; } diff --git a/src/main/java/net/gepafin/tendermanagement/dao/CallDao.java b/src/main/java/net/gepafin/tendermanagement/dao/CallDao.java index ec6ef678..8cee9587 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/CallDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/CallDao.java @@ -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(); diff --git a/src/main/java/net/gepafin/tendermanagement/dao/DelegationDao.java b/src/main/java/net/gepafin/tendermanagement/dao/DelegationDao.java index ca58f8be..f609086f 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/DelegationDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/DelegationDao.java @@ -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 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); } @@ -197,25 +197,6 @@ public class DelegationDao { UserCompanyDelegationEntity userCompanyDelegationEntity) { 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()) { diff --git a/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java b/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java index 224a21ec..90725964 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/DocumentDao.java @@ -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 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()); diff --git a/src/main/java/net/gepafin/tendermanagement/service/AmazonS3Service.java b/src/main/java/net/gepafin/tendermanagement/service/AmazonS3Service.java index ea3937a3..6cc27000 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/AmazonS3Service.java +++ b/src/main/java/net/gepafin/tendermanagement/service/AmazonS3Service.java @@ -11,11 +11,10 @@ import java.io.InputStream; @Component public interface AmazonS3Service { - public String upload(String fileName, String s3Folder, MultipartFile file) throws IOException; - - public Boolean delete(String s3Folder, String fileName); - + UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file); + + Boolean delete(String s3Folder, String filePath); + InputStream getFile(String s3Folder, String filePath) throws IOException; - public UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file); } \ No newline at end of file diff --git a/src/main/java/net/gepafin/tendermanagement/service/impl/AmazonS3ServiceImpl.java b/src/main/java/net/gepafin/tendermanagement/service/impl/AmazonS3ServiceImpl.java index a0470c15..56141be2 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/impl/AmazonS3ServiceImpl.java +++ b/src/main/java/net/gepafin/tendermanagement/service/impl/AmazonS3ServiceImpl.java @@ -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; @@ -28,6 +30,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)); } diff --git a/src/main/java/net/gepafin/tendermanagement/util/Utils.java b/src/main/java/net/gepafin/tendermanagement/util/Utils.java index 767872ce..bc8aff6c 100644 --- a/src/main/java/net/gepafin/tendermanagement/util/Utils.java +++ b/src/main/java/net/gepafin/tendermanagement/util/Utils.java @@ -308,4 +308,11 @@ public class Utils { return new StringTokenizer(header, ",").nextToken().trim(); } + + public static String replaceSpacesWithUnderscores(String content) { + if (content == null) { + return null; + } + return content.trim().replace(" ", "_"); + } } diff --git a/src/main/resources/message_en.properties b/src/main/resources/message_en.properties index 15b83fb3..7061921f 100644 --- a/src/main/resources/message_en.properties +++ b/src/main/resources/message_en.properties @@ -272,3 +272,5 @@ aasigned.application.not.found=Assigned Application not found with the given ID. assigned.application.deleted.success=Assigned Application successfully deleted. assigned.application.get.success=Assigned Application details fetched successfully. assigned.application.update.successfully=Assigned Application updated successfully. +get.error.s3=Failed to fetch the file from S3. + diff --git a/src/main/resources/message_it.properties b/src/main/resources/message_it.properties index b8d2dad3..22096e98 100644 --- a/src/main/resources/message_it.properties +++ b/src/main/resources/message_it.properties @@ -267,3 +267,4 @@ hub_get_success=Hub recuperato con successo hub_get_all_success=Hub recuperati con successo hub_delete_success=Hub eliminato con successo hub_not_found=Hub non trovato +get.error.s3=Impossibile recuperare il file da S3.