var APIBase = require("../../api.base");
var system = require("../../../system");
var settings = require("../../../../config/settings");
const crypto = require('crypto');
var fs=require("fs");
var accesskey='3KV9nIwW8qkTGlrPmAe3HnR3fzM6r5';
var accessKeyId='LTAI4GC5tSKvqsH2hMqj6pvd';   
var url="https://gsb-zc.oss-cn-beijing.aliyuncs.com";
class OSSAPI extends APIBase{
  constructor(){
    super()
  }
   async getOssConfig(){
    var policyText = {
        "expiration":"2119-12-31T16:00:00.000Z",
        "conditions":[
          ["content-length-range",0,1048576000],
          ["starts-with","$key","zc"]
        ]
    };
    var b = new Buffer(JSON.stringify(policyText));
    var policyBase64 = b.toString('base64');
    var signature= crypto.createHmac('sha1',accesskey).update(policyBase64).digest().toString('base64'); //base64

    var data={
        OSSAccessKeyId:accessKeyId,
        policy:policyBase64,
        Signature:signature,
        Bucket:'gsb-zc',
        success_action_status:201,
        url:url
   };

   return system.getResult(data);
  };
   async upfile(srckey,dest){
     var oss=System.getObject("util.ossClient");
     var result=await oss.upfile(srckey,"/tmp/"+dest);
     return result;
   };
   async downfile(srckey){
     var oss=System.getObject("util.ossClient");
     var downfile=await oss.downfile(srckey).then(function(){
       downfile="/tmp/"+srckey;
       return downfile;
     });
     return downfile;
   };
}
module.exports=OSSAPI;
