package com.platform.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

/**
 * 文件的工具类
 *
 * @author yikaihong
 */
public class FileUtils {
	
	public static String BASE_PATH = "/data/pluralism/media/";
//	public static String BASE_PATH = "D:/pluralism/media/";
	
	public static String PAGE_PATH = "/data/pluralism/page/";

    /**
     * 创建文件名称
     *
     * @return
     */
    public static String createFileName() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取文件后缀名称
     *
     * @param file
     * @return
     */
    public static String getSuffix(File file) {
        if (file == null) {
            return null;
        }
        String fileName = file.getName();
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 获取文件的后缀
     *
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
       if(StringUtils.isEmpty(fileName)){
           return null;
       }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 文件复制
     *
     * @param source
     * @param target
     */
    public static void copy(File source, File target) {
        if (source == null || target == null) {
            return;
        }
        if (!target.getParentFile().exists()) {
            target.getParentFile().mkdirs();
        }
        if (target.isDirectory() && !target.exists()) {
            target.mkdir();
        }
        if (target.isFile() && !target.exists()) {
            try {
                target.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(target);
            byte[] byteArray = new byte[1024];
            int length = 0;
            while ((length = in.read(byteArray)) != -1) {
                out.write(byteArray, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }


    /**
     * 文件复制
     *
     * @param source
     * @param targetPath
     */
    public static void copy(File source, String targetPath) {
        if (source == null || StringUtils.isEmpty(targetPath)) {
            return;
        }
        File target = new File(targetPath);
        if (!target.getParentFile().exists()) {
            target.getParentFile().mkdirs();
        }
        if (target.isDirectory() && !target.exists()) {
            target.mkdir();
        }
        if (target.isFile() && !target.exists()) {
            try {
                target.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(target);
            byte[] byteArray = new byte[1024];
            int length = 0;
            while ((length = in.read(byteArray)) != -1) {
                out.write(byteArray, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 文件复制
     *
     * @param source
     * @param targetPath
     */
    public static void copy(InputStream source, String targetPath) {
        if (source == null || StringUtils.isEmpty(targetPath)) {
            return;
        }
        System.out.println(targetPath);
        File target = new File(targetPath);
        if (!target.getParentFile().exists()) {
            target.getParentFile().mkdirs();
        }
        if (target.isDirectory() && !target.exists()) {
            target.mkdir();
        }
        if (target.isFile() && !target.exists()) {
            try {
                target.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(target);
            byte[] byteArray = new byte[1024];
            int length = 0;
            while ((length = source.read(byteArray)) != -1) {
                out.write(byteArray, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (source != null) {
                try {
                    source.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
