"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

require("core-js/modules/es6.regexp.to-string");

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));

var _request = _interopRequireDefault(require("./request"));

/*
 * @Author: lijianzhang
 * @Date: 2018-11-01 16:25:11
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2018-12-18 11:59:46
 * oss 服务封装
 */
var namespace = 'gaoding/';

var OSS =
/*#__PURE__*/
function () {
  function OSS() {}

  var _proto = OSS.prototype;

  _proto.getToken =
  /*#__PURE__*/
  function () {
    var _getToken = (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee(file) {
      var promise;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              promise = _request.default.post('/api/tokens', {
                type: 'oss',
                file_type: 'image',
                key: this.getKey(file)
              });
              this._cancel = promise.cancel;
              return _context.abrupt("return", promise);

            case 3:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    function getToken(_x) {
      return _getToken.apply(this, arguments);
    }

    return getToken;
  }();

  _proto.getKey = function getKey(file) {
    var now = new Date();
    var filename = now.getFullYear();
    filename += ('00' + (now.getMonth() + 1)).slice(-2);
    filename += ('00' + now.getDate()).slice(-2);
    filename += '-';
    filename += ('00' + now.getHours()).slice(-2);
    filename += ('00' + now.getMinutes()).slice(-2);
    filename += ('00' + now.getSeconds()).slice(-2);
    filename += ('00' + now.getTime()).slice(-3);
    filename += `-${Math.random().toString(16).slice(-6)}`;
    filename += '.' + file.type.slice(file.type.lastIndexOf('/') + 1);
    return namespace + filename;
  };

  _proto.uploadFile =
  /*#__PURE__*/
  function () {
    var _uploadFile = (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3(file, options) {
      var _this = this;

      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              return _context3.abrupt("return", this.getToken(file).then(
              /*#__PURE__*/
              function () {
                var _ref2 = (0, _asyncToGenerator2.default)(
                /*#__PURE__*/
                _regenerator.default.mark(function _callee2(_ref) {
                  var token, formData, promise;
                  return _regenerator.default.wrap(function _callee2$(_context2) {
                    while (1) {
                      switch (_context2.prev = _context2.next) {
                        case 0:
                          token = _ref.data;
                          formData = new FormData();
                          formData.append('key', token.key);
                          formData.append('policy', token.policy);
                          formData.append('Signature', token.signature);
                          formData.append('OSSAccessKeyId', token.access_key);
                          formData.append('success_action_status', 200);
                          formData.append('OSSUploadAction', token.oss_action);
                          formData.append('file', file);
                          promise = _request.default.post('//image-uploader.dancf.com/', formData, {
                            onUploadProgress(e) {
                              e.percent = 100 * e.loaded / e.total;

                              if (options.onProgress) {
                                options.onProgress(e);
                              }
                            },

                            withCredentials: false
                          });
                          _this._cancel = promise.cancel;
                          return _context2.abrupt("return", promise.then(function () {
                            return token.oss_action + '/' + token.key;
                          }));

                        case 12:
                        case "end":
                          return _context2.stop();
                      }
                    }
                  }, _callee2, this);
                }));

                return function (_x4) {
                  return _ref2.apply(this, arguments);
                };
              }()).catch(function (error) {
                throw error;
              }));

            case 1:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3, this);
    }));

    function uploadFile(_x2, _x3) {
      return _uploadFile.apply(this, arguments);
    }

    return uploadFile;
  }();

  _proto.cancel = function cancel(message) {
    if (this._cancel) {
      this._cancel(message);

      this._cancel = null;
    }
  };

  return OSS;
}();

exports.default = OSS;