Fixed s3 document issue

This commit is contained in:
rajesh
2024-10-23 00:14:01 +05:30
parent d02283519b
commit 19a54b6f75
9 changed files with 66 additions and 91 deletions

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());