'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tencentCloud_cos_helper = require('./helper.js');
require('../../bite/format-bite.js');
require('../../third-party/cos-nodejs-sdk-v5.js');

var pushFiles = function pushFiles(_ref) {
  var files = _ref.files,
    bucket = _ref.bucket,
    region = _ref.region;
  return files.map(function (file) {
    return {
      Bucket: bucket,
      Region: region,
      Key: file.key,
      FilePath: file.path
    };
  });
};
/**
 * COS上传
 * @param {object} config 配置信息
 * @param {Array<object>} config.files 文件列表
 * @param {string} config.files.key 文件key
 * @param {string} config.files.path 文件路径
 * @param {string} config.secretId COS secretId
 * @param {string} config.secretKey COS secretKey
 * @param {string} config.bucket COS bucket
 * @param {string} config.region COS region
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 *
 * uploadCOSFile({
 *   files: [{
 *     key: 'key1',
 *     path: 'path1',
 *   }, {
 *     key: 'key2',
 *     path: 'path2',
 *   }],
 *   secretId: 'xxx',
 *   secretKey: 'xxx',
 *   bucket: 'xxx',
 *   region: 'xxx',
 * })
 */
function uploadCOSFile(_ref2) {
  var files = _ref2.files,
    secretId = _ref2.secretId,
    secretKey = _ref2.secretKey,
    bucket = _ref2.bucket,
    region = _ref2.region;
  return new Promise(function (resolve, reject) {
    if (!secretId || !secretKey || !bucket || !region || !files) {
      reject('[Upload COS Error] 参数不全');
      return;
    }
    if (!files.length) {
      reject('[Upload COS Error] 上传文件不能为空');
      return;
    }
    var cos = tencentCloud_cos_helper.getCOSInstance(secretId, secretKey);
    var fileList = pushFiles({
      files: files,
      bucket: bucket,
      region: region
    });
    cos.uploadFiles({
      files: fileList,
      SliceSize: 1024 * 1024,
      onProgress: tencentCloud_cos_helper.onUploadCOSProgress,
      onFileFinish: function onFileFinish(err, data, options) {
        resolve(data);
        console.log("[uploadCOSFile] \u4E0A\u4F20".concat(err ? '失败' : '完成', ": ").concat(options.Key));
      }
    }, function (err, data) {
      reject(err || data);
    });
  });
}
/**
 * 获取 COS 存储桶中的文件列表
 * @param {object} config 配置信息
 * @param {string} config.secretId COS secretId
 * @param {string} config.secretKey COS secretKey
 * @param {string} config.bucket COS bucket
 * @param {string} config.region COS region
 * @param {string} config.prefix 文件前缀过滤
 * @returns {Promise<Array<ICosMeta>>} 文件元信息列表
 * @example
 * ```ts
 * const list = await getCOSBucketList({
 *   secretId: 'xxx',
 *   secretKey: 'xxx',
 *   bucket: 'my-bucket-1250000000',
 *   region: 'ap-guangzhou',
 *   prefix: 'static/',
 * });
 * ```
 */
function getCOSBucketList(_ref3) {
  var secretId = _ref3.secretId,
    secretKey = _ref3.secretKey,
    bucket = _ref3.bucket,
    region = _ref3.region,
    prefix = _ref3.prefix;
  return new Promise(function (resolve, reject) {
    if (!secretId || !secretKey || !bucket || !region) {
      reject('[Get COS List Error] 参数不全');
      return;
    }
    var cos = tencentCloud_cos_helper.getCOSInstance(secretId, secretKey);
    cos.getBucket({
      Bucket: bucket,
      Region: region,
      Prefix: prefix /* 非必须 */
    }, function (err, data) {
      if (err) {
        reject(err);
      } else {
        resolve(data.Contents);
      }
    });
  });
}
/**
 * 批量删除 COS 存储桶中的多个对象
 * @param {object} config 配置信息
 * @param {string} config.secretId COS secretId
 * @param {string} config.secretKey COS secretKey
 * @param {Array<string>} config.keys 要删除的对象 key 列表
 * @param {string} config.bucket COS bucket
 * @param {string} config.region COS region
 * @returns {Promise<any>} 删除结果
 * @example
 * ```ts
 * await deleteCOSMultipleObject({
 *   secretId: 'xxx',
 *   secretKey: 'xxx',
 *   keys: ['static/old-file1.js', 'static/old-file2.js'],
 *   bucket: 'my-bucket-1250000000',
 *   region: 'ap-guangzhou',
 * });
 * ```
 */
function deleteCOSMultipleObject(_ref4) {
  var secretId = _ref4.secretId,
    secretKey = _ref4.secretKey,
    keys = _ref4.keys,
    bucket = _ref4.bucket,
    region = _ref4.region;
  return new Promise(function (resolve, reject) {
    if (!secretId || !secretKey || !bucket || !keys) {
      reject('[Delete COS Object Error] 参数不全');
      return;
    }
    if (!keys.length) {
      reject('[Delete COS Object Error] 删除keys不能为空');
      return;
    }
    var cos = tencentCloud_cos_helper.getCOSInstance(secretId, secretKey);
    cos.deleteMultipleObject({
      Bucket: bucket,
      Region: region,
      /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
      Objects: keys.map(function (item) {
        return {
          Key: item
        };
      })
    }, function (err, data) {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

exports.deleteCOSMultipleObject = deleteCOSMultipleObject;
exports.getCOSBucketList = getCOSBucketList;
exports.uploadCOSFile = uploadCOSFile;
