const system = require("../../../system");
var settings = require("../../../../config/settings");
const crypto = require('crypto');
const cryptoJS = require("crypto-js");
var fs = require("fs");
// var AWS = require('aws-sdk');
var ak = "D0784B08541791E175B544D5317281B0";
var sk = "93AB12B3BFADB1356EC078D52B064A33";
var ossurl = "https://hangtang.s3.cn-north-1.jdcloud-oss.com";
class JdossService {
  constructor() {
    this.execClient = system.getObject("util.execClient");
  }
  
  async getJdOssConfig(app, opStr) {
    var date =  new Date();
    var month = date.getMonth()+1;
    month=month.toString();
    if(month<10){
      month="0"+month;
    }
    var day = date.getDate().toString();
    if(day<10){
      day="0"+day;
    }
    var time = date.getFullYear().toString()+month+day;
    let timestamp = date.getTime();//当前的时间戳
    timestamp = timestamp + 6 * 60 * 60 * 1000;
    //格式化时间获取年月日
    var dateAfter = new Date(timestamp);
    var policyText = {
      "expiration": dateAfter,
      "conditions": [
        {"bucket": "hangtang"},
        ["starts-with", "$key", "zc"], 
        {"Content-Type": "image/jpeg"},
        {"X-Amz-Credential": ak+"/"+time+"/cn-north-1/s3/aws4_request"}, 
        {"X-Amz-Algorithm": "AWS4-HMAC-SHA256"}, 
        {"X-Amz-Date": date}
      ]
    };
    var b = new Buffer(JSON.stringify(policyText));
    var policyBase64 = b.toString('base64');
    var signature = crypto.createHmac('sha256', sk).update(policyBase64).digest("hex");
    var data = {
      "OSSAccessKeyId":ak,
      "Bucket":"hangtang",
      "Signature":signature,
      "policy": policyBase64,
      "success_action_status": 201,
      "x-amz-algorithm":"AWS4-HMAC-SHA256",
      "x-amz-credential":ak+"/"+time+"/cn-north-1/s3/aws4_request",
      "x-amz-date":date,
      "x-amz-signature":signature,
      "url": ossurl
    };
    return system.getResultSuccess(data);
  }
}
module.exports = JdossService;

