/*
 * @Author: your name
 * @Date: 2022-05-03 13:37:34
 * @LastEditTime: 2022-05-03 20:45:51
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \platform-master-web\src\components\common\upload\ossUpload.js
 */
import Crypto from './lib/crypto/crypto'
import Base64 from  './lib/base64'
var accessid= 'LTAI5tGkF1G93gWDE1yrvWYS';
var accesskey= 'V9bgxeYq5f89noXZ9wP91Jv2rjAftx';
var host = 'http://guli123.oss-cn-beijing.aliyuncs.com';

var g_dirname = ''
var g_object_name = ''
var suffix=''
var policyText = {
    "expiration": "2025-01-01T12:00:00.000Z", //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
    "conditions": [
    ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
    ]
};
try{
    var policyBase64 = Base64.encode(JSON.stringify(policyText))
    var message = policyBase64
    var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, { asBytes: true }) ;
    var signature = Crypto.util.bytesToBase64(bytes);
}
catch(err){
    console.error(err)
}


//获取后缀名
function get_suffix(filename) {
    var pos = filename.lastIndexOf('.')
    suffix = ''
    if (pos != -1) {
        suffix = filename.substring(pos)
    }
    return suffix;
}


//随机字符串
function random_string(len) {
    　　len = len || 32;
    　　var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';   
    　　var maxPos = chars.length;
    　　var pwd = '';
    　　for (let i = 0; i < len; i++) {
        　　pwd += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd+Date.now();
    }

//获取文件名
function calculate_object_name(filename)
{
    suffix = get_suffix(filename)
    g_object_name = g_dirname + random_string(10) + suffix

    return ''
}

export function setUploadParam(obj,filename)
{
    g_object_name = g_dirname;
    try{
        suffix = get_suffix(filename)
        calculate_object_name(filename)
        

        let new_multipart_params = {
            'key' : g_object_name,
            'policy': policyBase64,
            'OSSAccessKeyId': accessid, 
            'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
            'signature': signature,
            'name':g_object_name,
        };
        obj.url=host
        for(let i in new_multipart_params){
            obj.data.append(i,new_multipart_params[i])
        }
        obj.resUrl=host+'/'+g_object_name
    }catch(err){
        console.error(err)
    }
    //获取随机文件名
    
}
