import moment = require("moment");
import { FILEDIRECTORY, OOSCODE } from "../config/enum";
import { changeEnumValue, eccEnumValue } from "../util/verificationEnum";
const Crypto = require('crypto');

const AssertsKey = 'sbPR0AdDPfjm7v2NSffk5apPm6UClV';
const OSSAccessKeyId = 'LTAI5tD7Finu3fxFMCi3415S';
export function getOss({userId, type, fileName}) {
    let limitMstime = moment().add(5, 'minute').utc().format();
    eccEnumValue("获取OSS授权", "类型", OOSCODE, type );
    var policyText = {
        "expiration": limitMstime, //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
        "conditions": [
        ["content-length-range", 0, 524288000] // 设置上传文件的大小限制
        ]
    };
    
    let policyBase64 = Buffer.from(JSON.stringify(policyText)).toString('base64');
    const FirName = FILEDIRECTORY[changeEnumValue(OOSCODE, type)];
    let message = policyBase64;
    let hmac = Crypto.createHmac('sha1', AssertsKey);
    hmac.update(message);
    let signature = hmac.digest('base64');
    // let bytes = Crypto.HMAC(Crypto.SHA1, message, AssertsKey, { asBytes: true });
    // var signature = Crypto.util.bytesToBase64(hmacDigest);

    return {
        OSSAccessKeyId:OSSAccessKeyId,
        dirName:`${userId}/${FirName}/${new Date().valueOf()}`,
        policyBase64,
        host:'https://wenhuabu-files.oss-cn-beijing.aliyuncs.com',
        signature
    };
}