Script for migrating deleted Documents
This commit is contained in:
@@ -61,9 +61,9 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
@Value("${aws.s3.region}")
|
||||
private String region;
|
||||
|
||||
private String getBucketUrlPrefix() {
|
||||
return "https://" + bucketName + ".s3." + region + ".amazonaws.com/";
|
||||
}
|
||||
@Autowired
|
||||
S3ReUploadMigrationService s3ReUploadMigrationService;
|
||||
|
||||
|
||||
private String upload(String fileName, String s3Folder,
|
||||
MultipartFile file) throws IOException {
|
||||
@@ -146,32 +146,10 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateS3PathForDeletedDocument(DocOtherSourceTypeEnum typeOfDocument, Long callId, Long applicationId,Long amendmentId) {
|
||||
try {
|
||||
return s3ConfigBean.generateDocumentPathForOther(typeOfDocument, callId, applicationId,amendmentId);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new CustomValidationException(
|
||||
Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generateS3PathForDeletedDocumentForOther() {
|
||||
try {
|
||||
return s3ConfigBean.generateDocumentPathForDelegationAndSignedDocument(DocOtherSourceTypeEnum.DELETED_USER_DELEGATION);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new CustomValidationException(
|
||||
Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.S3_PATH_GENERATION_ERROR_MSG)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveFile(String bucketName, String oldPath, String newPath) {
|
||||
public UploadFileOnAmazonS3Response moveFile(String fileName, String oldPath, String newPath) {
|
||||
try {
|
||||
newPath = cleanNewPath(oldPath, newPath);
|
||||
oldPath = cleanOldPath(oldPath);
|
||||
log.info("Moving file from {} to {} in bucket {}", oldPath, newPath, bucketName);
|
||||
|
||||
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketName, oldPath, bucketName, newPath);
|
||||
@@ -180,6 +158,8 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
|
||||
s3Client.deleteObject(bucketName, oldPath);
|
||||
log.info("Original file deleted successfully: {}", oldPath);
|
||||
String filePath = s3Url + newPath;
|
||||
return UploadFileOnAmazonS3Response.builder().fileName(fileName).filePath(filePath).build();
|
||||
} catch (AmazonServiceException e) {
|
||||
log.error("AWS service error while moving file: {}", e.getErrorMessage(), e);
|
||||
throw e;
|
||||
@@ -192,36 +172,11 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteDelegationfromS3(UserCompanyDelegationEntity userCompanyDelegationEntity) {
|
||||
String oldS3Path = userCompanyDelegationEntity.getFilePath();
|
||||
String newS3Path = generateS3PathForDeletedDocumentForOther()
|
||||
+ "/" + oldS3Path.substring(oldS3Path.lastIndexOf("/") + 1);
|
||||
|
||||
String bucketUrlPrefix = getBucketUrlPrefix();
|
||||
if (oldS3Path.startsWith(bucketUrlPrefix)) {
|
||||
oldS3Path = oldS3Path.replace(bucketUrlPrefix, "");
|
||||
}
|
||||
moveFile(bucketName, oldS3Path, newS3Path);
|
||||
log.info("File for company ID {} successfully moved to deleted folder.", userCompanyDelegationEntity.getId());
|
||||
private String cleanNewPath(String oldPath, String newPath) {
|
||||
return newPath + "/" + oldPath.substring(oldPath.lastIndexOf("/") + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFileFromS3(DocumentEntity documentEntity, Long callId, Long applicationId,Long amendmentId) {
|
||||
try {
|
||||
String oldS3Path = documentEntity.getFilePath();
|
||||
String newS3Path = generateS3PathForDeletedDocument(DocOtherSourceTypeEnum.valueOf("DELETED_" + documentEntity.getSource().toUpperCase()), callId, applicationId,amendmentId)
|
||||
+ "/" + oldS3Path.substring(oldS3Path.lastIndexOf("/") + 1);
|
||||
String bucketUrlPrefix = getBucketUrlPrefix();
|
||||
if (oldS3Path.startsWith(bucketUrlPrefix)) {
|
||||
oldS3Path = oldS3Path.replace(bucketUrlPrefix, "");
|
||||
}
|
||||
|
||||
moveFile(bucketName, oldS3Path, newS3Path);
|
||||
log.info("File for document ID {} successfully moved to deleted folder.", documentEntity.getId());
|
||||
} catch (Exception e) {
|
||||
log.error("Error moving file for document ID {} to deleted folder: {}", documentEntity.getId(), e.getMessage());
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR, "Error occurred while moving file to deleted folder.");
|
||||
}
|
||||
private String cleanOldPath(String oldPath) {
|
||||
return oldPath.replace(s3Url, "");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user