package ink.wgink.util.request;

import ink.wgink.exceptions.ParamsException;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

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

/**
 * @ClassName: StaticResourceRequestUtil
 * @Description: 静态资源请求
 * @Author: wanggeng
 * @Date: 2021/9/1 10:50 上午
 * @Version: 1.0
 */
public class StaticResourceRequestUtil {

    public static void download(HttpServletResponse response, InputStream inputStream, String outFileName) throws IOException {
        if (StringUtils.isBlank(outFileName)) {
            throw new ParamsException("文件不能名为空");
        }
        String[] fileNameArray = outFileName.split("\\.");
        if (fileNameArray.length < 1) {
            throw new ParamsException("文件无后缀名");
        }
        try (OutputStream outputStream = response.getOutputStream()) {
//            response.setHeader("Content-Length", String.valueOf(outFile.length()));
            response.setContentType(getContentType(fileNameArray[fileNameArray.length - 1]));
            response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(outFileName, "UTF-8"));
            response.setHeader("Expires", DateTime.now().plusDays(7).toDateTime(DateTimeZone.forID("GMT")).toString());
            // 一小时之内不发送新请求
            response.setHeader("max-age", "3600");
            byte[] buf = new byte[1024];
            for (int readLength; (readLength = inputStream.read(buf)) > 0; ) {
                outputStream.write(buf, 0, readLength);
            }
            outputStream.flush();
        }
    }

    /**
     * 下载静态资源
     *
     * @param response    响应
     * @param outFile     输出的文件
     * @param outFileName 输出的名字
     */
    public static void download(HttpServletResponse response, File outFile, String outFileName) throws IOException {
        if (outFile == null) {
            throw new ParamsException("输出文件不能为空");
        }
        if (StringUtils.isBlank(outFileName)) {
            throw new ParamsException("文件不能名为空");
        }
        String[] fileNameArray = outFileName.split("\\.");
        if (fileNameArray.length < 1) {
            throw new ParamsException("文件无后缀名");
        }
        FileInputStream fileInputStream = new FileInputStream(outFile);
        download(response, fileInputStream, outFileName);
    }

    /**
     * 文件类型
     *
     * @param fileType
     * @return
     */
    public static String getContentType(String fileType) {
        if (StringUtils.isBlank(fileType)) {
            throw new ParamsException("文件类型不能为空");
        }
        String contentType;
        switch (fileType) {
            case "png":
                contentType = "image/png";
                break;
            case "blob":
            case "jpg":
            case "jpeg":
                contentType = "image/jpeg";
                break;
            case "gif":
                contentType = "image/gif";
                break;
            case "mp4":
                contentType = "video/mp4";
                break;
            case "rmvb":
                contentType = "application/vnd.rn-realmedia-vbr";
                break;
            case "mp3":
                contentType = "audio/mp3";
                break;
            case "wmv":
                contentType = "video/x-ms-wmv";
                break;
            case "wav":
                contentType = "audio/wav";
                break;
            case "doc":
                contentType = "application/msword";
                break;
            case "docx":
                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                break;
            case "xls":
                contentType = "application/vnd.ms-excel";
                break;
            case "xlsx":
                contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
            case "ppt":
                contentType = "application/vnd.ms-powerpoint";
                break;
            case "pptx":
                contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                break;
            case "txt":
                contentType = "text/plain";
                break;
            case "apk":
                contentType = "application/vnd.android.package-archive";
                break;
            case "pdf":
                contentType = "application/pdf";
                break;
            case "css":
                contentType = "text/css";
                break;
            case "js":
                contentType = "application/x-javascript";
                break;
            case "htm":
            case "htx":
            case "html":
                contentType = "text/html";
                break;
            default:
                contentType = "application/octet-stream";
        }
        return contentType;
    }

}
