package com.njzscloud.common.mvc.util;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * 响应文件工具
 * （操作完成后输入输出流会被关闭）
 */
@Slf4j
public class FileResponseUtil {
    /**
     * 预览文件
     *
     * @param response    HttpServletResponse
     * @param inputStream 文件流
     * @param contentType MIME
     * @param fileName    文件名
     */
    public static void preview(HttpServletResponse response, InputStream inputStream, String contentType, String fileName) {
        FileResponseUtil.writeFile(response, inputStream, contentType, "inline" + (StrUtil.isNotBlank(fileName) ? ";filename=" + URLUtil.encode(fileName) : ""));
    }

    /**
     * 预览文件
     *
     * @param response    HttpServletResponse
     * @param bytes       文件数据
     * @param contentType MIME
     * @param fileName    文件名
     */
    public static void preview(HttpServletResponse response, byte[] bytes, String contentType, String fileName) {
        FileResponseUtil.writeFile(response, bytes, contentType, "inline" + (StrUtil.isNotBlank(fileName) ? ";filename=" + URLUtil.encode(fileName) : ""));
    }

    /**
     * 下载文件
     *
     * @param response    HttpServletResponse
     * @param inputStream 文件流
     * @param contentType MIME
     * @param fileName    文件名
     */
    public static void download(HttpServletResponse response, InputStream inputStream, String contentType, String fileName) {
        FileResponseUtil.writeFile(response, inputStream, contentType, "attachment" + (StrUtil.isNotBlank(fileName) ? ";filename=" + URLUtil.encode(fileName) : ""));
    }

    /**
     * 下载文件
     *
     * @param response    HttpServletResponse
     * @param bytes       文件数据
     * @param contentType MIME
     * @param fileName    文件名
     */
    public static void download(HttpServletResponse response, byte[] bytes, String contentType, String fileName) {
        FileResponseUtil.writeFile(response, bytes, contentType, "attachment" + (StrUtil.isNotBlank(fileName) ? ";filename=" + URLUtil.encode(fileName) : ""));
    }

    /**
     * 向客户端响应文件
     *
     * @param response           HttpServletResponse
     * @param data               文件内容, 只能是 InputStream 或 byte[]
     * @param contentType        MIME
     * @param contentDisposition 请求头 Content-Disposition
     */
    public static void writeFile(HttpServletResponse response, Object data, String contentType, String contentDisposition) {
        ServletOutputStream outputStream = null;
        try {
            response.setContentType(contentType);
            response.setCharacterEncoding(CharsetUtil.UTF_8);
            response.setHeader("Content-Disposition", contentDisposition);
            outputStream = response.getOutputStream();
            if (data instanceof InputStream) {
                IoUtil.copy((InputStream) data, outputStream);
            } else if (data instanceof byte[]) {
                IoUtil.write(outputStream, false, (byte[]) data);
            }
        } catch (Exception e) {
            log.error("文件响应失败", e);
        } finally {
            IoUtil.close(outputStream);
            if (data instanceof InputStream) {
                IoUtil.close((InputStream) data);
            }
        }
    }
}
