"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _lodash = _interopRequireDefault(require("lodash"));

var _ftp = _interopRequireDefault(require("ftp"));

var _fs = _interopRequireDefault(require("fs"));

var ftp =
/**
 *
 * @param config
 */
function ftp(config) {
  var _this = this;

  (0, _classCallCheck2["default"])(this, ftp);
  (0, _defineProperty2["default"])(this, "buffers", []);
  (0, _defineProperty2["default"])(this, "Upload", /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(files, destFile) {
      return _regenerator["default"].wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              return _context4.abrupt("return", new Promise( /*#__PURE__*/function () {
                var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(resolve, rejected) {
                  return _regenerator["default"].wrap(function _callee3$(_context3) {
                    while (1) {
                      switch (_context3.prev = _context3.next) {
                        case 0:
                          _this.Instance.on('ready', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
                            return _regenerator["default"].wrap(function _callee2$(_context2) {
                              while (1) {
                                switch (_context2.prev = _context2.next) {
                                  case 0:
                                    _this.Instance.put(files, destFile, /*#__PURE__*/function () {
                                      var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(err) {
                                        return _regenerator["default"].wrap(function _callee$(_context) {
                                          while (1) {
                                            switch (_context.prev = _context.next) {
                                              case 0:
                                                if (err) {
                                                  _context.next = 6;
                                                  break;
                                                }

                                                _this.ReturnData = {
                                                  status: true,
                                                  msg: "Berhasil Upload Data ".concat(files)
                                                };
                                                _context.next = 4;
                                                return resolve(_this.ReturnData);

                                              case 4:
                                                _context.next = 11;
                                                break;

                                              case 6:
                                                _this.ReturnData = {
                                                  status: true,
                                                  msg: "Gagal Upload Data ".concat(files),
                                                  error: err
                                                };
                                                _context.next = 9;
                                                return rejected(_this.ReturnData);

                                              case 9:
                                                _context.next = 11;
                                                return _this.Instance.end();

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

                                      return function (_x5) {
                                        return _ref4.apply(this, arguments);
                                      };
                                    }());

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

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

                return function (_x3, _x4) {
                  return _ref2.apply(this, arguments);
                };
              }()));

            case 1:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    }));

    return function (_x, _x2) {
      return _ref.apply(this, arguments);
    };
  }());
  (0, _defineProperty2["default"])(this, "Get", /*#__PURE__*/function () {
    var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12(files, settings) {
      return _regenerator["default"].wrap(function _callee12$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              return _context12.abrupt("return", new Promise( /*#__PURE__*/function () {
                var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11(resolve, rejected) {
                  return _regenerator["default"].wrap(function _callee11$(_context11) {
                    while (1) {
                      switch (_context11.prev = _context11.next) {
                        case 0:
                          _this.GetSettings = _lodash["default"].extend({
                            saveFile: true,
                            convertToBase64: false,
                            targetDir: "/",
                            compressed: false
                          }, settings);
                          _context11.prev = 1;

                          _this.Instance.on('ready', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10() {
                            return _regenerator["default"].wrap(function _callee10$(_context10) {
                              while (1) {
                                switch (_context10.prev = _context10.next) {
                                  case 0:
                                    _this.Instance.get(files, _this.GetSettings.compressed, /*#__PURE__*/function () {
                                      var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(err, stream) {
                                        return _regenerator["default"].wrap(function _callee9$(_context9) {
                                          while (1) {
                                            switch (_context9.prev = _context9.next) {
                                              case 0:
                                                if (err) {
                                                  _context9.next = 16;
                                                  break;
                                                }

                                                _this.ReturnData = {
                                                  status: true,
                                                  msg: "Berhasil Mendapatkan Data ".concat(files),
                                                  data: {}
                                                };

                                                if (!_this.GetSettings.saveFile) {
                                                  _context9.next = 5;
                                                  break;
                                                }

                                                _context9.next = 5;
                                                return stream.pipe(_fs["default"].createWriteStream(_this.GetSettings.targetDir));

                                              case 5:
                                                //const mb = new Base64Encode()
                                                _this.buffers = [];
                                                _context9.next = 8;
                                                return stream.on('data', /*#__PURE__*/function () {
                                                  var _ref9 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(chunk) {
                                                    return _regenerator["default"].wrap(function _callee5$(_context5) {
                                                      while (1) {
                                                        switch (_context5.prev = _context5.next) {
                                                          case 0:
                                                            if (_this.GetSettings.convertToBase64) {
                                                              _this.buffers.push(chunk);
                                                            }

                                                          case 1:
                                                          case "end":
                                                            return _context5.stop();
                                                        }
                                                      }
                                                    }, _callee5);
                                                  }));

                                                  return function (_x12) {
                                                    return _ref9.apply(this, arguments);
                                                  };
                                                }());

                                              case 8:
                                                _context9.next = 10;
                                                return stream.on('finish', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
                                                  var mb;
                                                  return _regenerator["default"].wrap(function _callee6$(_context6) {
                                                    while (1) {
                                                      switch (_context6.prev = _context6.next) {
                                                        case 0:
                                                          if (_this.GetSettings.convertToBase64) {
                                                            mb = Buffer.concat(_this.buffers);
                                                            _this.ReturnData.data.base64 = mb.toString('base64');
                                                          }

                                                          _context6.next = 3;
                                                          return resolve(_this.ReturnData);

                                                        case 3:
                                                        case "end":
                                                          return _context6.stop();
                                                      }
                                                    }
                                                  }, _callee6);
                                                })));

                                              case 10:
                                                _context9.next = 12;
                                                return stream.once('close', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
                                                  return _regenerator["default"].wrap(function _callee7$(_context7) {
                                                    while (1) {
                                                      switch (_context7.prev = _context7.next) {
                                                        case 0:
                                                          _context7.next = 2;
                                                          return _this.Instance.end();

                                                        case 2:
                                                        case "end":
                                                          return _context7.stop();
                                                      }
                                                    }
                                                  }, _callee7);
                                                })));

                                              case 12:
                                                _context9.next = 14;
                                                return stream.once('error', /*#__PURE__*/function () {
                                                  var _ref12 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(err) {
                                                    return _regenerator["default"].wrap(function _callee8$(_context8) {
                                                      while (1) {
                                                        switch (_context8.prev = _context8.next) {
                                                          case 0:
                                                            _this.ReturnData = {
                                                              status: true,
                                                              msg: "Terjadi Error",
                                                              error: err
                                                            };
                                                            _context8.next = 3;
                                                            return rejected(_this.ReturnData);

                                                          case 3:
                                                            _context8.next = 5;
                                                            return _this.Instance.end();

                                                          case 5:
                                                          case "end":
                                                            return _context8.stop();
                                                        }
                                                      }
                                                    }, _callee8);
                                                  }));

                                                  return function (_x13) {
                                                    return _ref12.apply(this, arguments);
                                                  };
                                                }());

                                              case 14:
                                                _context9.next = 21;
                                                break;

                                              case 16:
                                                _this.ReturnData = {
                                                  status: true,
                                                  msg: "Gagal Mendapatkan Data ".concat(files),
                                                  error: err
                                                };
                                                _context9.next = 19;
                                                return rejected(_this.ReturnData);

                                              case 19:
                                                _context9.next = 21;
                                                return _this.Instance.end();

                                              case 21:
                                              case "end":
                                                return _context9.stop();
                                            }
                                          }
                                        }, _callee9);
                                      }));

                                      return function (_x10, _x11) {
                                        return _ref8.apply(this, arguments);
                                      };
                                    }());

                                  case 1:
                                  case "end":
                                    return _context10.stop();
                                }
                              }
                            }, _callee10);
                          })));

                          _context11.next = 12;
                          break;

                        case 5:
                          _context11.prev = 5;
                          _context11.t0 = _context11["catch"](1);
                          _this.ReturnData = {
                            status: true,
                            msg: "Terjadi Error",
                            error: _context11.t0
                          };
                          _context11.next = 10;
                          return rejected(_this.ReturnData);

                        case 10:
                          _context11.next = 12;
                          return _this.Instance.end();

                        case 12:
                        case "end":
                          return _context11.stop();
                      }
                    }
                  }, _callee11, null, [[1, 5]]);
                }));

                return function (_x8, _x9) {
                  return _ref6.apply(this, arguments);
                };
              }()));

            case 1:
            case "end":
              return _context12.stop();
          }
        }
      }, _callee12);
    }));

    return function (_x6, _x7) {
      return _ref5.apply(this, arguments);
    };
  }());
  this.config = _lodash["default"].extend({
    host: "127.0.0.1",
    user: "",
    password: "",
    connTimeout: 2000,
    keepalive: 1000,
    port: 21,
    secure: false
  }, config);
  this.Instance = new _ftp["default"]();
  this.Instance.connect(this.config);
};

var _default = ftp;
exports["default"] = _default;
//# sourceMappingURL=index.js.map