package com.fdkankan.web.util;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @author MeepoGuan
 *
 * <p>Description: 上传问题件</p>
 *
 * 2017年4月30日
 *
 */
@Slf4j
public class FileUpload {

	/**
	 * @param file 			//文件对象
	 * @param filePath		//上传路径
	 * @param fileName		//文件名
	 * @return  文件名
	 */
	public static String fileUp(MultipartFile file, String filePath, String fileName) throws IOException {
		String extName = ""; // 扩展名格式：

		if (file.getOriginalFilename().lastIndexOf(".") >= 0){
			extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		}
		copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", "");

		return fileName+extName;
	}
	
	/**
	 * 写文件到当前目录的upload目录中
	 * 
	 * @param in
	 * @param dir
	 * @param realName
	 * @throws IOException
	 */
	private static String copyFile(InputStream in, String dir, String realName){
		FileUtil.writeFromStream(in,dir);
		return realName;
	}

	/**
	 * 断点续传
	 * @param
     */
	public static String fileUpAgain(InputStream in, String filePath, String fileName, String realSavePath) throws IOException {
		try{
			File realFile = new File(realSavePath +  fileName);
			File tempFile = new File(filePath + fileName);
			if(!realFile.getParentFile().exists()){
				realFile.getParentFile().mkdirs();
			}
			if(!tempFile.getParentFile().exists()){
				tempFile.getParentFile().mkdirs();
			}

//			InputStream in = file.getInputStream();
			long needSkipBytes = 0;
			if (tempFile.exists()) {//续传
				needSkipBytes = tempFile.length();
			} else {//第一次传
				tempFile.createNewFile();
			}
			log.info("跳过的字节数为：" + needSkipBytes);
			in.skip(needSkipBytes);
			RandomAccessFile tempRandAccessFile = new RandomAccessFile(tempFile, "rw");
			tempRandAccessFile.seek(needSkipBytes);
			byte[] buffer = new byte[1024];
			int len = 0;
			int count = 0;
			while ((len = in.read(buffer)) > 0) {
				tempRandAccessFile.write(buffer);
				count++;
			}
			in.close();
			tempRandAccessFile.close();
			realFile.createNewFile();
			if (fileCopy(tempFile, realFile)) {
				tempFile.delete();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return realSavePath + fileName;
	}

	private static boolean fileCopy(File sourceFile, File targetFile) {
		boolean success = true;
		try {
			FileInputStream in = new FileInputStream(sourceFile);
			FileOutputStream out = new FileOutputStream(targetFile);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			success = false;
		} catch (IOException e) {
			success = false;
		}
		return success;
	}


	public static void main(String[] args) {
		try{
			String path = "F:\\桌面\\20190925151119-T.h264";
			FileInputStream f = new FileInputStream(path);
			fileUpAgain(f, "G:\\javaProject\\9b918c802c3e40282267a89b5231f9a8_201905101446434643\\videos\\", "test123-T.h264", "G:\\javaProject\\9b918c802c3e40282267a89b5231f9a8_201905101446434643\\capture\\");
//			copyFile(f, "G:\\javaProject\\zhoushan-system\\zhoushan-system-api\\src\\main\\resources\\static\\head", "test.h264");
		}catch (Exception e){
			e.printStackTrace();
		}

	}
}
