package com.dcsoft.file.config;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
public class FileListener extends FileAlterationListenerAdaptor {

    public  String rootDir;
    public  String copyDir;

    public FileListener(String root, String copy) {
        this.rootDir=root;
        this.copyDir=copy;
    }

    /**
     * 文件创建执行
     */
    public void onFileCreate(File file) {
        log.info("[新建]:" + file.getPath());
        //文件同步到另一个文件夹中
        String path=file.getPath().replace(rootDir,copyDir);
        File dest=new File(path);
        if (!(dest.exists() && dest.isDirectory())) {
            new File(path.substring(0, path.lastIndexOf(File.separator))).mkdirs();
        }
        try {
            Files.copy(file.toPath(), dest.toPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 文件修改
     */
    public void onFileChange(File file) {
        log.info("[修改]:" + file.getAbsolutePath());
    }
    /**
     * 文件删除
     */
    public void onFileDelete(File file) {
        log.info("[删除]:" + file.getAbsolutePath());
    }
    /**
     * 目录创建
     */
    public void onDirectoryCreate(File directory) {
        log.info("[新建]:" + directory.getAbsolutePath());
    }
    /**
     * 目录修改
     */
    public void onDirectoryChange(File directory) {
        log.info("[修改]:" + directory.getAbsolutePath());
    }
    /**
     * 目录删除
     */
    public void onDirectoryDelete(File directory) {
        log.info("[删除]:" + directory.getAbsolutePath());
    }
    public void onStart(FileAlterationObserver observer) {
        super.onStart(observer);
    }
    public void onStop(FileAlterationObserver observer) {
        super.onStop(observer);
    }



}
