From 47e2d4ba3bcd51d50655cfbdd1e8defe0dd264a2 Mon Sep 17 00:00:00 2001 From: SeoJin Kim Date: Tue, 27 Jul 2021 01:22:16 +0900 Subject: [PATCH] =?UTF-8?q?[Spring][=EC=87=BC=ED=95=91=EB=AA=B0=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=EC=A0=9D=ED=8A=B8][29]=20=EC=83=81=ED=92=88?= =?UTF-8?q?=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=88=98=EC=A0=95=20-=208(?= =?UTF-8?q?=EB=B0=B0=EC=B9=98=20=ED=94=84=EB=A1=9C=EA=B7=B8=EB=9E=A8=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=205)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://kimvampa.tistory.com/242 --- .../com/vam/task/AttachFileCheckTask.java | 94 +++++++++++++++++++ .../src/main/java/com/vam/task/testBatch.java | 2 +- .../com/vam/task/AttachFileCheckTask.java | 94 +++++++++++++++++++ 3 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 VamPa/src/main/java/com/vam/task/AttachFileCheckTask.java create mode 100644 VamPa_MySQL/src/test/java/com/vam/task/AttachFileCheckTask.java diff --git a/VamPa/src/main/java/com/vam/task/AttachFileCheckTask.java b/VamPa/src/main/java/com/vam/task/AttachFileCheckTask.java new file mode 100644 index 0000000..a6bd63b --- /dev/null +++ b/VamPa/src/main/java/com/vam/task/AttachFileCheckTask.java @@ -0,0 +1,94 @@ +package com.vam.task; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import com.vam.mapper.AdminMapper; +import com.vam.model.AttachImageVO; + +import lombok.extern.log4j.Log4j; + +@Component +@Log4j +public class AttachFileCheckTask { + + @Autowired + private AdminMapper mapper; + + private String getFolderYesterDay() { + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar cal = Calendar.getInstance(); + + cal.add(Calendar.DATE, -1); + + String str = sdf.format(cal.getTime()); + + return str.replace("-", File.separator); + } + + @Scheduled(cron="0 * * * * *") + public void checkFiles() throws Exception{ + + log.warn("File Check Task Run.........."); + log.warn(new Date()); + log.warn("========================================"); + + // DB에 저장된 파일 리스트 + List fileList = mapper.checkFileList(); + + + // 비교 기준 파일 리스트(Path객체) + List checkFilePath = new ArrayList(); + //원본 이미지 + fileList.forEach(vo -> { + Path path = Paths.get("C:\\upload", vo.getUploadPath(), vo.getUuid() + "_" + vo.getFileName()); + checkFilePath.add(path); + }); + //썸네일 이미지 + fileList.forEach(vo -> { + Path path = Paths.get("C:\\upload", vo.getUploadPath(), "s_" + vo.getUuid() + "_" + vo.getFileName()); + checkFilePath.add(path); + }); + + + // 디렉토리 파일 리스트 + File targetDir = Paths.get("C:\\upload", getFolderYesterDay()).toFile(); + File[] targetFile = targetDir.listFiles(); + + + // 삭제 대상 파일 리스트(분류) + List removeFileList = new ArrayList(Arrays.asList(targetFile)); + for(File file : targetFile){ + checkFilePath.forEach(checkFile ->{ + if(file.toPath().equals(checkFile)) + removeFileList.remove(file); + }); + } + + + // 삭제 대상 파일 제거 + log.warn("file Delete : "); + for(File file : removeFileList) { + log.warn(file); + file.delete(); + } + + log.warn("========================================"); + + } + + +} diff --git a/VamPa/src/main/java/com/vam/task/testBatch.java b/VamPa/src/main/java/com/vam/task/testBatch.java index 9b7291d..b2dac4d 100644 --- a/VamPa/src/main/java/com/vam/task/testBatch.java +++ b/VamPa/src/main/java/com/vam/task/testBatch.java @@ -5,7 +5,7 @@ import org.springframework.stereotype.Component; import lombok.extern.log4j.Log4j; -@Component + @Log4j public class testBatch { diff --git a/VamPa_MySQL/src/test/java/com/vam/task/AttachFileCheckTask.java b/VamPa_MySQL/src/test/java/com/vam/task/AttachFileCheckTask.java new file mode 100644 index 0000000..a6bd63b --- /dev/null +++ b/VamPa_MySQL/src/test/java/com/vam/task/AttachFileCheckTask.java @@ -0,0 +1,94 @@ +package com.vam.task; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import com.vam.mapper.AdminMapper; +import com.vam.model.AttachImageVO; + +import lombok.extern.log4j.Log4j; + +@Component +@Log4j +public class AttachFileCheckTask { + + @Autowired + private AdminMapper mapper; + + private String getFolderYesterDay() { + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar cal = Calendar.getInstance(); + + cal.add(Calendar.DATE, -1); + + String str = sdf.format(cal.getTime()); + + return str.replace("-", File.separator); + } + + @Scheduled(cron="0 * * * * *") + public void checkFiles() throws Exception{ + + log.warn("File Check Task Run.........."); + log.warn(new Date()); + log.warn("========================================"); + + // DB에 저장된 파일 리스트 + List fileList = mapper.checkFileList(); + + + // 비교 기준 파일 리스트(Path객체) + List checkFilePath = new ArrayList(); + //원본 이미지 + fileList.forEach(vo -> { + Path path = Paths.get("C:\\upload", vo.getUploadPath(), vo.getUuid() + "_" + vo.getFileName()); + checkFilePath.add(path); + }); + //썸네일 이미지 + fileList.forEach(vo -> { + Path path = Paths.get("C:\\upload", vo.getUploadPath(), "s_" + vo.getUuid() + "_" + vo.getFileName()); + checkFilePath.add(path); + }); + + + // 디렉토리 파일 리스트 + File targetDir = Paths.get("C:\\upload", getFolderYesterDay()).toFile(); + File[] targetFile = targetDir.listFiles(); + + + // 삭제 대상 파일 리스트(분류) + List removeFileList = new ArrayList(Arrays.asList(targetFile)); + for(File file : targetFile){ + checkFilePath.forEach(checkFile ->{ + if(file.toPath().equals(checkFile)) + removeFileList.remove(file); + }); + } + + + // 삭제 대상 파일 제거 + log.warn("file Delete : "); + for(File file : removeFileList) { + log.warn(file); + file.delete(); + } + + log.warn("========================================"); + + } + + +}