수입/지출 데이터 삭제시 OCR의 경우 파일 실삭제
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package app.module.core.config.application.util;
|
||||
|
||||
import app.module.core.usecase.file.getFile.adapters.persistence.GetFileMapper;
|
||||
import app.module.core.usecase.file.getFile.ports.application.domain.GetFile;
|
||||
import app.module.core.usecase.file.saveToStorage.ports.application.strategy.SftpConnector;
|
||||
import app.module.core.usecase.file.updateFile.adapters.persistence.UpdateFileMapper;
|
||||
import app.module.core.usecase.file.updateFile.ports.application.domain.UploadFileDto;
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
@@ -18,6 +21,8 @@ import java.nio.file.Paths;
|
||||
public class SftpFileDeleteUtil {
|
||||
|
||||
private final SftpConnector sftpConnector;
|
||||
private final GetFileMapper getFileMapper;
|
||||
private final UpdateFileMapper updateFileMapper;
|
||||
|
||||
@Value("${path.upload.sftp.public}")
|
||||
private String publicPath;
|
||||
@@ -64,6 +69,47 @@ public class SftpFileDeleteUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteRealFile(Long deleteFileId) throws JSchException, SftpException {
|
||||
GetFile deleteFile = getFileMapper.findById(deleteFileId).orElseThrow();
|
||||
updateFileMapper.deleteReceiptFile(deleteFileId,CommonUtil.getLoginUsername());
|
||||
|
||||
if (deleteFile == null || deleteFile.getPath() == null || deleteFile.getName() == null) {
|
||||
log.error("Invalid file information: {}", deleteFile);
|
||||
throw new IllegalArgumentException("Invalid file information");
|
||||
}
|
||||
|
||||
// 경로 검증 및 생성
|
||||
String filePath = constructSafeFilePath(deleteFile.getPath(), deleteFile.getName());
|
||||
log.info("Attempting to delete file from SFTP: {}", filePath);
|
||||
|
||||
// SFTP 연결 확인 및 재연결
|
||||
if (!sftpConnector.isConnected()) {
|
||||
log.warn("SFTP connection lost, attempting to reconnect");
|
||||
sftpConnector.connect();
|
||||
}
|
||||
ChannelSftp channelSftp = sftpConnector.getChannelSftp();
|
||||
if (channelSftp == null) {
|
||||
log.error("SFTP channel is not initialized");
|
||||
throw new SftpException(ChannelSftp.SSH_FX_NO_CONNECTION, "SFTP channel is not initialized");
|
||||
}
|
||||
|
||||
// 파일 삭제
|
||||
try {
|
||||
channelSftp.stat(filePath); // 파일 존재 여부 확인
|
||||
channelSftp.rm(filePath);
|
||||
log.info("File deleted successfully from SFTP: {}", filePath);
|
||||
} catch (SftpException e) {
|
||||
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
|
||||
log.warn("File does not exist on SFTP: {}", filePath);
|
||||
} else {
|
||||
log.error("Failed to delete file from SFTP: {}, error: {}", filePath, e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 경로 조작 방지를 위해 안전한 파일 경로 생성
|
||||
*/
|
||||
|
||||
@@ -3,6 +3,7 @@ package app.module.core.usecase.file.updateFile.adapters.persistence;
|
||||
import app.module.core.usecase.file.updateFile.ports.application.domain.UpdateUploadFileParam;
|
||||
import app.module.core.usecase.file.updateFile.ports.application.domain.UploadFileDto;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -10,5 +11,6 @@ import java.util.List;
|
||||
public interface UpdateFileMapper {
|
||||
List<UploadFileDto> findDeleteFileList(UpdateUploadFileParam uploadFile);
|
||||
void deleteFile(UpdateUploadFileParam uploadFile);
|
||||
void deleteReceiptFile(@Param("fileId") Long fileId,@Param("deletedUserId") String deletedUserId);
|
||||
void updateFile(UploadFileDto uploadFile);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,15 @@
|
||||
</if>
|
||||
</update>
|
||||
|
||||
<update id="deleteReceiptFile">
|
||||
UPDATE upload_file
|
||||
SET deleted_by = #{deletedUserId},
|
||||
deleted_at = now(),
|
||||
is_deleted = true
|
||||
WHERE is_deleted = false
|
||||
and id = #{fileId}
|
||||
</update>
|
||||
|
||||
<update id="updateFile" >
|
||||
UPDATE upload_file
|
||||
SET mapping_id = #{mappingId},
|
||||
|
||||
Reference in New Issue
Block a user