package ink.wgink.util.request;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

/**
 * @ClassName: RequestUtil
 * @Description: 请求工具类
 * @Author: WangGeng
 * @Date: 2019/3/14 11:38 AM
 * @Version: 1.0
 **/
public class RequestUtil {

    public static final String UNKNOWN = "unknown";

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取IP
     *
     * @return
     */
    public static String getRequestIp() {
        return getRequestIp(getRequest());
    }

    /**
     * 请求IP
     *
     * @return
     */
    public static String getRequestIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "127.0.0.1";
        }
        // 多次反向代理后会有多个ip值，第一个ip才是真实ip
        if (ip.indexOf(",") != -1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }

    /**
     * 下载
     *
     * @param response    响应
     * @param outFile     输出的文件
     * @param outFileName 输出的名字
     */
    public static void download(HttpServletResponse response, File outFile, String outFileName) throws IOException {
        try (FileInputStream fileInputStream = new FileInputStream(outFile);
             OutputStream outputStream = response.getOutputStream()) {
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(outFileName, "UTF-8"));
            byte[] buf = new byte[1024];
            for (int readLength; (readLength = fileInputStream.read(buf)) > 0; ) {
                outputStream.write(buf, 0, readLength);
            }
            outputStream.flush();
        }
    }

    /**
     * 下载
     *
     * @param response
     * @param inputStream
     * @param outFileName
     * @throws IOException
     */
    public static void download(HttpServletResponse response, InputStream inputStream, String outFileName) throws IOException {
        try (OutputStream outputStream = response.getOutputStream()) {
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(outFileName, "UTF-8"));
            byte[] buf = new byte[1024];
            for (int readLength; (readLength = inputStream.read(buf)) > 0; ) {
                outputStream.write(buf, 0, readLength);
            }
            outputStream.flush();
        }
    }

}
