package com.ruoyi.file.request;

import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

@Data
public final class UploadRedirectReq
{
    @NotBlank(message = "重定向地址不能为空")
    private String              redirectUri;

    // 身份授权, 从请求中读取
    private String              authorization;
    // 重定向携带身份授权的参数名, 置空则不携带
    private String              authorizationName = "api_token";
    // 重定向参数数据
    private Map<String, Object> formData;

    public void ParseToken(HttpServletRequest request)
    {
        authorization = request.getHeader("Authorization");
    }

    public void SetupToken(String authorizationName, HttpServletRequest request)
    {
        this.authorizationName = authorizationName;
        ParseToken(request);
    }

    public void ParseFormData(HttpServletRequest request)
    {
        if(null == formData)
            formData = new LinkedHashMap<>();

        Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames.hasMoreElements())
        {
            String s = parameterNames.nextElement();
            if("redirectUri".equals(s))
                continue;
            formData.put(s, request.getParameter(s));
        }
    }
}
