Done ticket GEPAFINBE-8
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.DocumentEntity;
|
||||
import net.gepafin.tendermanagement.enums.DocumentTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.response.DocumentResponseBean;
|
||||
import net.gepafin.tendermanagement.repositories.DocumentRepository;
|
||||
import net.gepafin.tendermanagement.service.AmazonS3Service;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
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 java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class DocumentDao {
|
||||
|
||||
@Autowired
|
||||
private AmazonS3Service amazonS3Service;
|
||||
|
||||
@Autowired
|
||||
private DocumentRepository documentRepository;
|
||||
|
||||
@Autowired
|
||||
private CallDao callDao;
|
||||
|
||||
@Value("${aws.s3.bucket.name}")
|
||||
private String bucketName;
|
||||
|
||||
@Value("${aws.s3.url.folder}")
|
||||
private String s3Folder;
|
||||
|
||||
@Value("${aws.s3.url}")
|
||||
private String s3Url;
|
||||
|
||||
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, DocumentTypeEnum fileType) {
|
||||
List<DocumentEntity> documentEntities = new ArrayList<>();
|
||||
|
||||
for (MultipartFile file : files) {
|
||||
try {
|
||||
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,file);
|
||||
DocumentEntity documentEntity = new DocumentEntity();
|
||||
documentEntity.setFileName(fileName);
|
||||
documentEntity.setType(fileType.getValue());
|
||||
documentEntity.setFilePath(filepath);
|
||||
documentEntities.add(documentEntity);
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
documentRepository.saveAll(documentEntities);
|
||||
return documentEntities.stream()
|
||||
.map(callDao::convertToDocumentResponseBean)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
public Void deleteFile(Long documentId){
|
||||
DocumentEntity documentEntity= documentRepository.findById(documentId);
|
||||
if(documentEntity==null){
|
||||
new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
||||
}
|
||||
String fileName= Utils.extractFileName(documentEntity.getFilePath());
|
||||
try {
|
||||
amazonS3Service.delete(bucketName, fileName);
|
||||
documentRepository.delete(documentEntity);
|
||||
}catch (Exception e){}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user