package com.emr.util;

import com.sun.org.apache.xalan.internal.lib.ExsltBase;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Path;

public class FileHelper {
    public static String getAssemblyFolder() throws Exception {
        String ret = System.getProperty("user.dir");
        return ret;
    }

    public static boolean File_IsExist(String file) {
        File f = new File(file);
        return f.exists();
    }

    public static boolean Fold_IsExist(String folder) {
        File f = new File(folder);
        if (f.isDirectory()) {
            return f.exists();
        }
        return false;
    }

    public static String GetTempFile() throws Exception {
        String folder = getAssemblyFolder();
        folder = String.format("%s/%s", folder, "TmpFiles");
        File fFolder = new File(folder);
        fFolder.mkdirs();

        String file = String.format("%s/%s", folder, IDHelper.NewID()+"");
        File ffile = new File(file);
        ffile.createNewFile();
        return file;
    }

    public static String GetTempFile_TempFolder(String tempFolder) throws Exception {
        File folder = new File(tempFolder);
        if (!folder.isDirectory()) {
            throw new ExSys("无效的文件路径 %s", tempFolder);
        }
        if (!folder.exists()) {
            folder.mkdirs();
        }

        String tmpFile = String.format("%s/%l", tempFolder, IDHelper.NewID());
        DeleteFile(tmpFile);
        return tmpFile;
    }

    public static void DeleteFile(String filePath) throws Exception {
        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }
        boolean ret = file.delete();
        int timeout = 45*1000;
        while(true)
        {
            if(timeout<0)
            {
                throw new ExSys("无法删除文件 %s", filePath);
            }
            if(ret)
            {
                break;
            }
            Thread.sleep(1000);
            timeout-=1000;
            ret = file.delete();
        }
    }

    public static void CopyFile(String old, String newFle) throws Exception {
        File nf = new File(newFle);
        if (nf.isDirectory()) {
            throw new ExSys("无效的目标文件路径  %s", newFle);
        }
        if (nf.exists()) {
            throw new ExSys("目标文件路径存在  %s", newFle);
        }
        File of = new File(old);
        if (of.isDirectory()) {
            throw new ExSys("无效的源文件路径  %s", old);
        }
        if (!of.exists()) {
            throw new ExSys("源文件路径不存在  %s", old);
        }

        File pfile = nf.getParentFile();
        if (!pfile.exists()) {
            pfile.mkdirs();
        }

        FileInputStream fis = new FileInputStream(old);//创建输入流对象
        FileOutputStream fos = new FileOutputStream(newFle); //创建输出流对象
        try {
            byte datas[] = new byte[1024 * 8];//创建搬运工具
            int len = 0;//创建长度
            while ((len = fis.read(datas)) != -1)//循环读取数据
            {
                fos.write(datas, 0, len);
            }
        }
        finally {
            fis.close();//释放资源
            fos.close();//释放资源
        }
    }
}
