package com.trade.common.utils.file;

import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.trade.common.utils.LoggerUtil;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;

import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;
import java.io.*;
import java.net.URLEncoder;

/**
 * @description:
 * @Author: wanglang
 * @Date: 2024/9/7 16:20
 * @Version: 1.0
 */
public class FileDownUtils {

    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);;

    public static void downloadFile(HttpServletResponse res, File file) {
        OutputStream out = null;
        InputStream in = null;
        try {
            if (file.exists()) {
                String fileName = file.getName();
                in = new FileInputStream(file);
                fileName = URLEncoder.encode(fileName, "UTF-8");
                res.setHeader("content-disposition", "attachment;fileName=" + fileName);
                out = res.getOutputStream();
                byte[] b = new byte[1024];
                int len = -1;
                while ((len = in.read(b)) != -1) {
                    out.write(b, 0, len);
                }
            }
        } catch (Exception e) {
            log.error("下载文件失败:" + e.getMessage());
            throw new ServiceException("下载合同失败：" + e.getMessage(), HttpStatus.ERROR);
        } finally {
            try {
                IOUtils.close(out);
                IOUtils.close(in);
            } catch (IOException e) {
                log.error("关闭流失败:" + e.getMessage());
            }
        }
    }

    public static void downloadFile(HttpServletResponse res, InputStream in, String fileName) {
        OutputStream out = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            StringBuilder contentDispositionValue = new StringBuilder();
            contentDispositionValue.append("attachment; filename=").append(fileName).append(";").append("filename*=")
                    .append("utf-8''").append(fileName);
            res.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename");
            res.setHeader("Content-disposition", contentDispositionValue.toString());
            res.setHeader("download-filename", fileName);
            // res.setHeader("content-disposition", "attachment;fileName=" + fileName);
            out = res.getOutputStream();
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
        } catch (Exception e) {
            log.error("下载文件失败:" + e.getMessage());
            throw new ServiceException("下载合同失败：" + e.getMessage(), HttpStatus.ERROR);
        } finally {
            try {
                IOUtils.close(out);
                IOUtils.close(in);
            } catch (IOException e) {
                log.error("关闭流失败:" + e.getMessage());
            }
        }
    }
}
