// ==========================================================================
// 所属模块：Upload/Token
// 生成日期：2024-02-08 17:09:21 +0800 CST
// 生成路径: upload/token/function.js
// 生成人：xScript_Engine
// 数据表：sys_files - 文件上传(又拍云)
// ==========================================================================

response.headers.add('X-Script-Version', 'upload-1.0');

function main() {
    // 验证用户权限
    'use api/user/acl/excerpt.js'

    let content_md5 = payload.get().path.split("/").pop()
    const sql = `SELECT * FROM sys_files WHERE file_hash =? LIMIT 1;`;
    let res = SQL.query('system_sql', sql, content_md5);
    if (res.length > 0) {
        res = res[0]
        okMsg({
            path: res.file_path,
            type: res.file_type,
            size: res.file_size,
        })
    }

    let password = crypto.hash.md5("v5JuNat8fget0184tFTTRH2Nyap35DiS").toHEX.encoding().toString();
    // console.log(password);
    let method = `PUT`
    let file = `/${os.getEnv('BUCKET_NAME')}/${ex.suid().base58()}`
    let now = new Date(Date.now()).toUTCString();

    let digest = crypto.hash.hmac.sha1(password, `${method}&${file}&${now}&${content_md5}`).toBase64.encoding().toString();

    // console.log(digest);
    return okMsg({
        file: file,
        domain: 'cdn.p-q.co',
        token: `UPYUN xmagic:${digest}`,
        content_md5: content_md5
    });
}
