package thyyxxk.webserver.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
public class DeleteVoiceFiles {

    @Scheduled(cron = "0 0 0 * * ?")
    public void deleteAllVoiceFiles() {
        final String path = "D:\\apache-tomcat-9.0.30\\webapps\\voices\\";
        log.info("准备清空文件夹 >>> {}", path);
        File file = new File(path);
        if (!file.exists()) {
            log.info("目标文件夹不存在 >>> {}", path);
            return;
        }
        if (!file.isDirectory()) {
            log.info("目标地址不是文件夹 >>> {}", path);
            return;
        }
        String[] tempList = file.list();
        if (null == tempList || tempList.length == 0) {
            log.info("目标文件夹为空 >>> {}", path);
            return;
        }
        File temp;
        for (String s : tempList) {
            temp = new File(path + s);
            if (temp.delete()) {
                log.info("删除文件 {} 成功。", s);
            } else {
                log.info("删除文件 {} 失败，请手动删除。", s);
            }
        }
    }

}
