package com.yihu.jw.util.common;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.fastdfs.FastDFSUtil;
import it.sauronsoftware.jave.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

/**
 * 文件处理工具类
 * @author huangwenjie
 */
@Component
public class FileUtil {
	
	@Autowired
	public FastDFSUtil fastDFSHelper;
	
	public void changeToMp3(String sourcePath, String targetPath) {
		File source = new File(sourcePath);
		File target = new File(targetPath);
		AudioAttributes audio = new AudioAttributes();
		Encoder encoder = new Encoder();
		audio.setCodec("libmp3lame");
		EncodingAttributes attrs = new EncodingAttributes();
		attrs.setFormat("mp3");
		attrs.setAudioAttributes(audio);
		try {
			encoder.encode(source, target, attrs);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InputFormatException e) {
			e.printStackTrace();
		} catch (EncoderException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 拷贝临时语音文件到存储目录
	 * @param voices_Path
	 * @param fastdfs_file_url
	 * @return
	 * @throws IOException
	 */
	
	
	public  String copyTempVoice(String voices_Path,String fastdfs_file_url) throws Exception {
		// 文件保存的临时路径
		String fileUrls = "";
		File f = new File(voices_Path);
		if (f.exists()) {
			String fileName = f.getName();
			InputStream in = new FileInputStream(f);
			//得到文件类型
			String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
			ObjectNode result = fastDFSHelper.upload(in, fileType, "");
			in.close();
			if (result != null) {
				fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
						+ result.get("groupName").toString().replaceAll("\"", "") + "/"
						+ result.get("remoteFileName").toString().replaceAll("\"", "");
				f.delete();
			}
		}
		return fileUrls;
	}
	
	/**
	 * 上传文件到FastDFS
	 *
	 * @param files 临时文件
	 * @return
	 * @throws Exception
	 */
	public String copyTempImage(String files,String tempPath) throws Exception {
		System.out.println("files====="+files);
		System.out.println("tempPath====="+tempPath);
		// 文件保存的临时路径
		String[] fileArray = files.split(",");
		String fileUrls = "";
		for (String file : fileArray) {
			System.out.println("file+++"+file);
			File f = new File(tempPath + "/"+file);
			File fs = new File(tempPath +"/"+ file + "_small");
			if (f.exists()) {
				String fileName = f.getName();
				InputStream in = new FileInputStream(f);
				ObjectNode result = fastDFSHelper.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
				System.out.println(result.toString());
				in.close();
				if (result != null) {
					//1.3.7去掉前缀
					fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",")
							+ result.get("groupName").toString().replaceAll("\"", "") + "/"
							+ result.get("remoteFileName").toString().replaceAll("\"", "");
					f.delete();
					if (fs.exists()) {
						fs.delete();
					}
				}
			}
		}
		
		return fileUrls;
	}
}
