package com.jkcredit.invoice.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * SpringBoot上传文件工具类
 * @author LinkinStar
 */
@Slf4j
public class FileHandleUtil {

	/** 绝对路径 **/
//	private static String absolutePath = "/Users/sunzhaoning/Downloads";

	private static String absolutePath ="";
//	private static String absolutePath ="/Users/Administrator/Desktop/excel/";

	/** 静态目录 **/
	private static String staticDir = "static";

	/** 文件存放的目录 **/
	private static String fileDir = "/upload/";

	/**
	 * 上传单个文件

	 * 该方法返回值为：/upload/image/test.jpg
	 * @param inputStream 文件流
	 * @param path 文件路径，如：image/
	 * @param filename 文件名，如：test.jpg
	 * @return 成功：上传后的文件访问路径，失败返回：null
	 */
	public static String upload(InputStream inputStream, String path, String filename) {
		//第一次会创建文件夹
		createDirIfNotExists();

		String resultPath = fileDir + path + filename;

		//存文件
		File uploadFile = new File(absolutePath, staticDir + resultPath);
		try {
			FileUtils.copyInputStreamToFile(inputStream, uploadFile);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		return resultPath;
	}

	/**
	 * 创建文件夹路径
	 */
	private static void createDirIfNotExists() {
		if (!absolutePath.isEmpty()) {return;}

		//获取跟目录
		File file;
		try {
			file = new File(ResourceUtils.getURL("classpath:").getPath());
		} catch (FileNotFoundException e) {
			throw new RuntimeException("获取根目录失败，无法创建上传目录！");
		}
		if(!file.exists()) {
			file = new File("");
		}

		absolutePath = file.getAbsolutePath();

		File upload = new File(absolutePath, staticDir + fileDir);
		if(!upload.exists()) {
			upload.mkdirs();
		}
	}

	/**
	 * 删除文件
	 * @param path 文件访问的路径upload开始 如： /upload/image/test.jpg
	 * @return true 删除成功； false 删除失败
	 */
	public static boolean delete(String path) {
		File file = new File(absolutePath, staticDir + path);
		if (file.exists()) {
			return file.delete();
		}
		return false;
	}
}
