UNPKG

@azteam/google-api

Version:

N/A

989 lines (986 loc) 50.9 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.DRIVE_FILE_TYPE = void 0; var _GoogleOAuth2API2 = _interopRequireDefault(require("./GoogleOAuth2API")); var _scope = require("./scope"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return r; }; var t, r = {}, e = Object.prototype, n = e.hasOwnProperty, o = "function" == typeof Symbol ? Symbol : {}, i = o.iterator || "@@iterator", a = o.asyncIterator || "@@asyncIterator", u = o.toStringTag || "@@toStringTag"; function c(t, r, e, n) { Object.defineProperty(t, r, { value: e, enumerable: !n, configurable: !n, writable: !n }); } try { c({}, ""); } catch (t) { c = function c(t, r, e) { return t[r] = e; }; } function h(r, e, n, o) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype); return c(a, "_invoke", function (r, e, n) { var o = 1; return function (i, a) { if (3 === o) throw Error("Generator is already running"); if (4 === o) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var u = n.delegate; if (u) { var c = d(u, n); if (c) { if (c === f) continue; return c; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (1 === o) throw o = 4, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = 3; var h = s(r, e, n); if ("normal" === h.type) { if (o = n.done ? 4 : 2, h.arg === f) continue; return { value: h.arg, done: n.done }; } "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg); } }; }(r, n, new Context(o || [])), !0), a; } function s(t, r, e) { try { return { type: "normal", arg: t.call(r, e) }; } catch (t) { return { type: "throw", arg: t }; } } r.wrap = h; var f = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var l = {}; c(l, i, function () { return this; }); var p = Object.getPrototypeOf, y = p && p(p(x([]))); y && y !== e && n.call(y, i) && (l = y); var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l); function g(t) { ["next", "throw", "return"].forEach(function (r) { c(t, r, function (t) { return this._invoke(r, t); }); }); } function AsyncIterator(t, r) { function e(o, i, a, u) { var c = s(t[o], t, i); if ("throw" !== c.type) { var h = c.arg, f = h.value; return f && "object" == _typeof(f) && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) { e("next", t, a, u); }, function (t) { e("throw", t, a, u); }) : r.resolve(f).then(function (t) { h.value = t, a(h); }, function (t) { return e("throw", t, a, u); }); } u(c.arg); } var o; c(this, "_invoke", function (t, n) { function i() { return new r(function (r, o) { e(t, n, r, o); }); } return o = o ? o.then(i, i) : i(); }, !0); } function d(r, e) { var n = e.method, o = r.i[n]; if (o === t) return e.delegate = null, "throw" === n && r.i["return"] && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f; var i = s(o, r.i, e.arg); if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f; var a = i.arg; return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f); } function w(t) { this.tryEntries.push(t); } function m(r) { var e = r[4] || {}; e.type = "normal", e.arg = t, r[4] = e; } function Context(t) { this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0); } function x(r) { if (null != r) { var e = r[i]; if (e) return e.call(r); if ("function" == typeof r.next) return r; if (!isNaN(r.length)) { var o = -1, a = function e() { for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e; return e.value = t, e.done = !0, e; }; return a.next = a; } } throw new TypeError(_typeof(r) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), c(GeneratorFunctionPrototype, u, GeneratorFunction.displayName = "GeneratorFunction"), r.isGeneratorFunction = function (t) { var r = "function" == typeof t && t.constructor; return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name)); }, r.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t; }, r.awrap = function (t) { return { __await: t }; }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () { return this; }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(h(t, e, n, o), i); return r.isGeneratorFunction(e) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, g(v), c(v, u, "Generator"), c(v, i, function () { return this; }), c(v, "toString", function () { return "[object Generator]"; }), r.keys = function (t) { var r = Object(t), e = []; for (var n in r) e.unshift(n); return function t() { for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t; return t.done = !0, t; }; }, r.values = x, Context.prototype = { constructor: Context, reset: function reset(r) { if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0][4]; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(r) { if (this.done) throw r; var e = this; function n(t) { a.type = "throw", a.arg = r, e.next = t; } for (var o = e.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i[4], u = this.prev, c = i[1], h = i[2]; if (-1 === i[0]) return n("end"), !1; if (!c && !h) throw Error("try statement without catch or finally"); if (null != i[0] && i[0] <= u) { if (u < c) return this.method = "next", this.arg = t, n(c), !0; if (u < h) return n(h), !1; } } }, abrupt: function abrupt(t, r) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var n = this.tryEntries[e]; if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) { var o = n; break; } } o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null); var i = o ? o[4] : {}; return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i); }, complete: function complete(t, r) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && r && (this.next = r), f; }, finish: function finish(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[2] === t) return this.complete(e[4], e[3]), m(e), f; } }, "catch": function _catch(t) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var e = this.tryEntries[r]; if (e[0] === t) { var n = e[4]; if ("throw" === n.type) { var o = n.arg; m(e); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(r, e, n) { return this.delegate = { i: x(r), r: e, n: n }, "next" === this.method && (this.arg = t), f; } }, r; } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; } function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); } function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } // const UPLOAD_ENDPOINT = 'https://www.googleapis.com/upload'; // const UPLOAD_DRIVE_V3_ENDPOINT = `${UPLOAD_ENDPOINT}/drive/v3/files`; var DRIVE_V3_ENDPOINT = 'https://www.googleapis.com/drive/v3', DRIVE_V2_ENDPOINT = 'https://www.googleapis.com/drive/v2', TEAM_DRIVE_V3_ENDPOINT = "".concat(DRIVE_V3_ENDPOINT, "/drives"); var DRIVE_FILE_TYPE = exports.DRIVE_FILE_TYPE = { FOLDER: 'application/vnd.google-apps.folder', FILE: 'application/vnd.google-apps.file', SHEET: 'application/vnd.google-apps.spreadsheet' }; var GoogleDriveAPI = /*#__PURE__*/function (_GoogleOAuth2API) { function GoogleDriveAPI(accountType, credential) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; _classCallCheck(this, GoogleDriveAPI); return _callSuper(this, GoogleDriveAPI, [accountType, _objectSpread(_objectSpread({}, credential), {}, { scope: _scope.DRIVE_SCOPE }), options]); } _inherits(GoogleDriveAPI, _GoogleOAuth2API); return _createClass(GoogleDriveAPI, [{ key: "getAuthLink", value: function getAuthLink(redirectURI, state) { var arrayScope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [_scope.DRIVE_SCOPE]; return _superPropGet(GoogleDriveAPI, "getAuthLink", this, 3)([redirectURI, state, arrayScope]); } }, { key: "hasScope", value: function () { var _hasScope = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() { var arrayScope, _args = arguments; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: arrayScope = _args.length > 0 && _args[0] !== undefined ? _args[0] : [_scope.DRIVE_SCOPE]; return _context.abrupt("return", _superPropGet(GoogleDriveAPI, "hasScope", this, 3)([arrayScope])); case 2: case "end": return _context.stop(); } }, _callee, this); })); function hasScope() { return _hasScope.apply(this, arguments); } return hasScope; }() }, { key: "_getAllItem", value: function () { var _getAllItem2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(metadata) { var client, data, pageToken, res; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return this._getAuthClient(); case 2: client = _context2.sent; data = [], pageToken = null; case 4: _context2.next = 6; return client.get("".concat(DRIVE_V3_ENDPOINT, "/files"), _objectSpread({ pageToken: pageToken, pageSize: 1000, supportsAllDrives: true, includeTeamDriveItems: true }, metadata)); case 6: res = _context2.sent; pageToken = res.nextPageToken; if (res.files && res.files.length > 0) { data = [].concat(_toConsumableArray(data), _toConsumableArray(res.files)); } else { pageToken = undefined; } case 9: if (pageToken) { _context2.next = 4; break; } case 10: return _context2.abrupt("return", data); case 11: case "end": return _context2.stop(); } }, _callee2, this); })); function _getAllItem(_x) { return _getAllItem2.apply(this, arguments); } return _getAllItem; }() }, { key: "getFileInfo", value: function () { var _getFileInfo = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(driveId) { var client; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this._getAuthClient(); case 2: client = _context3.sent; return _context3.abrupt("return", client.get("".concat(DRIVE_V2_ENDPOINT, "/files/").concat(driveId, "?supportsAllDrives=true"))); case 4: case "end": return _context3.stop(); } }, _callee3, this); })); function getFileInfo(_x2) { return _getFileInfo.apply(this, arguments); } return getFileInfo; }() }, { key: "checkFileLive", value: function () { var _checkFileLive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(driveId) { var res; return _regeneratorRuntime().wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return this.getFileInfo(driveId); case 2: res = _context4.sent; return _context4.abrupt("return", !res.error); case 4: case "end": return _context4.stop(); } }, _callee4, this); })); function checkFileLive(_x3) { return _checkFileLive.apply(this, arguments); } return checkFileLive; }() }, { key: "getAllFolder", value: function () { var _getAllFolder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5() { var containsName, parentId, query, _args5 = arguments; return _regeneratorRuntime().wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: containsName = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : null; parentId = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : 'root'; query = "mimeType='".concat(DRIVE_FILE_TYPE.FOLDER, "' and trashed=false and '").concat(parentId, "' in parents"); if (containsName) { query = "name contains '".concat(containsName, "' and ").concat(query); } return _context5.abrupt("return", this._getAllItem({ q: query })); case 5: case "end": return _context5.stop(); } }, _callee5, this); })); function getAllFolder() { return _getAllFolder.apply(this, arguments); } return getAllFolder; }() }, { key: "getFolderByName", value: function () { var _getFolderByName = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(name) { var parentId, query, res, _args6 = arguments; return _regeneratorRuntime().wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: parentId = _args6.length > 1 && _args6[1] !== undefined ? _args6[1] : 'root'; query = "name='".concat(name, "' and mimeType='").concat(DRIVE_FILE_TYPE.FOLDER, "' and trashed=false and '").concat(parentId, "' in parents"); _context6.next = 4; return this._getAllItem({ q: query }); case 4: res = _context6.sent; if (!(res.length > 0)) { _context6.next = 7; break; } return _context6.abrupt("return", res[0]); case 7: return _context6.abrupt("return", null); case 8: case "end": return _context6.stop(); } }, _callee6, this); })); function getFolderByName(_x4) { return _getFolderByName.apply(this, arguments); } return getFolderByName; }() }, { key: "getAllFile", value: function () { var _getAllFile = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7() { var containsName, parentId, query, _args7 = arguments; return _regeneratorRuntime().wrap(function _callee7$(_context7) { while (1) switch (_context7.prev = _context7.next) { case 0: containsName = _args7.length > 0 && _args7[0] !== undefined ? _args7[0] : null; parentId = _args7.length > 1 && _args7[1] !== undefined ? _args7[1] : 'root'; query = "mimeType!='".concat(DRIVE_FILE_TYPE.FOLDER, "' and trashed=false and '").concat(parentId, "' in parents"); if (containsName) { query = "name contains '".concat(containsName, "' and ").concat(query); } return _context7.abrupt("return", this._getAllItem({ q: query })); case 5: case "end": return _context7.stop(); } }, _callee7, this); })); function getAllFile() { return _getAllFile.apply(this, arguments); } return getAllFile; }() }, { key: "getFileByName", value: function () { var _getFileByName = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(name) { var parentId, query, res, _args8 = arguments; return _regeneratorRuntime().wrap(function _callee8$(_context8) { while (1) switch (_context8.prev = _context8.next) { case 0: parentId = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : 'root'; query = "name='".concat(name, "' and mimeType!='").concat(DRIVE_FILE_TYPE.FOLDER, "' and trashed=false and '").concat(parentId, "' in parents"); _context8.next = 4; return this._getAllItem({ q: query }); case 4: res = _context8.sent; if (!(res.length > 0)) { _context8.next = 7; break; } return _context8.abrupt("return", res[0]); case 7: return _context8.abrupt("return", null); case 8: case "end": return _context8.stop(); } }, _callee8, this); })); function getFileByName(_x5) { return _getFileByName.apply(this, arguments); } return getFileByName; }() }, { key: "createFolder", value: function () { var _createFolder = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(name) { var parentId, client, metadata, _args9 = arguments; return _regeneratorRuntime().wrap(function _callee9$(_context9) { while (1) switch (_context9.prev = _context9.next) { case 0: parentId = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : null; _context9.next = 3; return this._getAuthClient(); case 3: client = _context9.sent; metadata = { name: name, mimeType: DRIVE_FILE_TYPE.FOLDER }; if (parentId) { metadata.parents = [parentId]; } return _context9.abrupt("return", client.post("".concat(DRIVE_V3_ENDPOINT, "/files?supportsAllDrives=true"), metadata, false)); case 7: case "end": return _context9.stop(); } }, _callee9, this); })); function createFolder(_x6) { return _createFolder.apply(this, arguments); } return createFolder; }() }, { key: "getAllTeamDrive", value: function () { var _getAllTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0() { var containsName, notContainsName, client, data, query, pageToken, res, _args0 = arguments; return _regeneratorRuntime().wrap(function _callee0$(_context0) { while (1) switch (_context0.prev = _context0.next) { case 0: containsName = _args0.length > 0 && _args0[0] !== undefined ? _args0[0] : null; notContainsName = _args0.length > 1 && _args0[1] !== undefined ? _args0[1] : null; _context0.next = 4; return this._getAuthClient(); case 4: client = _context0.sent; data = []; query = null; if (containsName) { query = "name contains '".concat(containsName, "'"); } if (notContainsName) { if (containsName) { query += ' and '; } query += "not name contains '".concat(notContainsName, "'"); } pageToken = null; case 10: _context0.next = 12; return client.get(TEAM_DRIVE_V3_ENDPOINT, { pageSize: 100, pageToken: pageToken, q: query }); case 12: res = _context0.sent; pageToken = res.nextPageToken; if (res.drives && res.drives.length > 0) { data.push.apply(data, _toConsumableArray(res.drives)); } else { pageToken = undefined; } case 15: if (pageToken) { _context0.next = 10; break; } case 16: return _context0.abrupt("return", data); case 17: case "end": return _context0.stop(); } }, _callee0, this); })); function getAllTeamDrive() { return _getAllTeamDrive.apply(this, arguments); } return getAllTeamDrive; }() }, { key: "getTeamDriveByName", value: function () { var _getTeamDriveByName = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee1(name) { var client, res; return _regeneratorRuntime().wrap(function _callee1$(_context1) { while (1) switch (_context1.prev = _context1.next) { case 0: _context1.next = 2; return this._getAuthClient(); case 2: client = _context1.sent; _context1.next = 5; return client.get(TEAM_DRIVE_V3_ENDPOINT, { q: "name='".concat(name, "'") }); case 5: res = _context1.sent; if (!(res.drives.length > 0)) { _context1.next = 8; break; } return _context1.abrupt("return", res.drives[0]); case 8: return _context1.abrupt("return", null); case 9: case "end": return _context1.stop(); } }, _callee1, this); })); function getTeamDriveByName(_x7) { return _getTeamDriveByName.apply(this, arguments); } return getTeamDriveByName; }() }, { key: "cloneFile", value: function () { var _cloneFile = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(driveId, name) { var parentId, client, data, _args10 = arguments; return _regeneratorRuntime().wrap(function _callee10$(_context10) { while (1) switch (_context10.prev = _context10.next) { case 0: parentId = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : null; _context10.next = 3; return this._getAuthClient(); case 3: client = _context10.sent; data = { name: name }; if (parentId) { data.parents = [parentId]; } return _context10.abrupt("return", client.post("".concat(DRIVE_V3_ENDPOINT, "/files/").concat(driveId, "/copy?supportsAllDrives=true"), data, false)); case 7: case "end": return _context10.stop(); } }, _callee10, this); })); function cloneFile(_x8, _x9) { return _cloneFile.apply(this, arguments); } return cloneFile; }() }, { key: "moveItem", value: function () { var _moveItem = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(driveId, toParentId) { var moveParentId, client, moveParent, res, _args11 = arguments; return _regeneratorRuntime().wrap(function _callee11$(_context11) { while (1) switch (_context11.prev = _context11.next) { case 0: moveParentId = _args11.length > 2 && _args11[2] !== undefined ? _args11[2] : null; _context11.next = 3; return this._getAuthClient(); case 3: client = _context11.sent; moveParent = ''; if (moveParentId) { moveParent = "&removeParents=".concat(moveParentId); } _context11.next = 8; return client.patch("".concat(DRIVE_V3_ENDPOINT, "/files/").concat(driveId, "?supportsAllDrives=true&addParents=").concat(toParentId).concat(moveParent), {}, false); case 8: res = _context11.sent; return _context11.abrupt("return", res.id === driveId); case 10: case "end": return _context11.stop(); } }, _callee11, this); })); function moveItem(_x0, _x1) { return _moveItem.apply(this, arguments); } return moveItem; }() }, { key: "rename", value: function () { var _rename = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(driveId, newName) { var client, res; return _regeneratorRuntime().wrap(function _callee12$(_context12) { while (1) switch (_context12.prev = _context12.next) { case 0: _context12.next = 2; return this._getAuthClient(); case 2: client = _context12.sent; _context12.next = 5; return client.patch("".concat(DRIVE_V3_ENDPOINT, "/files/").concat(driveId, "?supportsAllDrives=true"), { name: newName }, false); case 5: res = _context12.sent; return _context12.abrupt("return", res.id === driveId); case 7: case "end": return _context12.stop(); } }, _callee12, this); })); function rename(_x10, _x11) { return _rename.apply(this, arguments); } return rename; }() }, { key: "deleteItem", value: function () { var _deleteItem = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13(driveId) { var client, res; return _regeneratorRuntime().wrap(function _callee13$(_context13) { while (1) switch (_context13.prev = _context13.next) { case 0: _context13.next = 2; return this._getAuthClient(); case 2: client = _context13.sent; _context13.next = 5; return client["delete"]("".concat(DRIVE_V3_ENDPOINT, "/files/").concat(driveId, "?supportsAllDrives=true"), {}, false); case 5: res = _context13.sent; return _context13.abrupt("return", !res.error); case 7: case "end": return _context13.stop(); } }, _callee13, this); })); function deleteItem(_x12) { return _deleteItem.apply(this, arguments); } return deleteItem; }() }, { key: "findFileInTrash", value: function () { var _findFileInTrash = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee14(name) { return _regeneratorRuntime().wrap(function _callee14$(_context14) { while (1) switch (_context14.prev = _context14.next) { case 0: return _context14.abrupt("return", this._getAllItem({ q: "name ='".concat(name, "' and trashed = true") })); case 1: case "end": return _context14.stop(); } }, _callee14, this); })); function findFileInTrash(_x13) { return _findFileInTrash.apply(this, arguments); } return findFileInTrash; }() }, { key: "findFileAllTeamDrive", value: function () { var _findFileAllTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee15(name) { return _regeneratorRuntime().wrap(function _callee15$(_context15) { while (1) switch (_context15.prev = _context15.next) { case 0: return _context15.abrupt("return", this._getAllItem({ q: "name ='".concat(name, "' and trashed = false") })); case 1: case "end": return _context15.stop(); } }, _callee15, this); })); function findFileAllTeamDrive(_x14) { return _findFileAllTeamDrive.apply(this, arguments); } return findFileAllTeamDrive; }() }, { key: "unTrashItem", value: function () { var _unTrashItem = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee16(driveId) { var client, res; return _regeneratorRuntime().wrap(function _callee16$(_context16) { while (1) switch (_context16.prev = _context16.next) { case 0: _context16.next = 2; return this._getAuthClient(); case 2: client = _context16.sent; _context16.next = 5; return client.post("".concat(DRIVE_V2_ENDPOINT, "/files/").concat(driveId, "/untrash?supportsAllDrives=true"), {}, false); case 5: res = _context16.sent; return _context16.abrupt("return", !res.error); case 7: case "end": return _context16.stop(); } }, _callee16, this); })); function unTrashItem(_x15) { return _unTrashItem.apply(this, arguments); } return unTrashItem; }() }, { key: "hideTeamDrive", value: function () { var _hideTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee17(driveId) { var client, res; return _regeneratorRuntime().wrap(function _callee17$(_context17) { while (1) switch (_context17.prev = _context17.next) { case 0: _context17.next = 2; return this._getAuthClient(); case 2: client = _context17.sent; _context17.next = 5; return client.post("".concat(TEAM_DRIVE_V3_ENDPOINT, "/").concat(driveId, "/hide")); case 5: res = _context17.sent; return _context17.abrupt("return", res.id === driveId); case 7: case "end": return _context17.stop(); } }, _callee17, this); })); function hideTeamDrive(_x16) { return _hideTeamDrive.apply(this, arguments); } return hideTeamDrive; }() }, { key: "showTeamDrive", value: function () { var _showTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee18(driveId) { var client, res; return _regeneratorRuntime().wrap(function _callee18$(_context18) { while (1) switch (_context18.prev = _context18.next) { case 0: _context18.next = 2; return this._getAuthClient(); case 2: client = _context18.sent; _context18.next = 5; return client.post("".concat(TEAM_DRIVE_V3_ENDPOINT, "/").concat(driveId, "/unhide")); case 5: res = _context18.sent; return _context18.abrupt("return", res.id === driveId); case 7: case "end": return _context18.stop(); } }, _callee18, this); })); function showTeamDrive(_x17) { return _showTeamDrive.apply(this, arguments); } return showTeamDrive; }() }, { key: "changeNameTeamDrive", value: function () { var _changeNameTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee19(driveId, name) { var client, res; return _regeneratorRuntime().wrap(function _callee19$(_context19) { while (1) switch (_context19.prev = _context19.next) { case 0: _context19.next = 2; return this._getAuthClient(); case 2: client = _context19.sent; _context19.next = 5; return client.patch("".concat(TEAM_DRIVE_V3_ENDPOINT, "/").concat(driveId), { name: name }, false); case 5: res = _context19.sent; return _context19.abrupt("return", res.id === driveId && res.name === name); case 7: case "end": return _context19.stop(); } }, _callee19, this); })); function changeNameTeamDrive(_x18, _x19) { return _changeNameTeamDrive.apply(this, arguments); } return changeNameTeamDrive; }() }, { key: "createTeamDrive", value: function () { var _createTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee20(name) { var uuid, client, res, _args20 = arguments; return _regeneratorRuntime().wrap(function _callee20$(_context20) { while (1) switch (_context20.prev = _context20.next) { case 0: uuid = _args20.length > 1 && _args20[1] !== undefined ? _args20[1] : null; _context20.next = 3; return this._getAuthClient(); case 3: client = _context20.sent; _context20.next = 6; return client.post("".concat(TEAM_DRIVE_V3_ENDPOINT, "?requestId=").concat(uuid || Date.now()), { name: name }, false); case 6: res = _context20.sent; if (!res.id) { _context20.next = 9; break; } return _context20.abrupt("return", res.id); case 9: return _context20.abrupt("return", null); case 10: case "end": return _context20.stop(); } }, _callee20, this); })); function createTeamDrive(_x20) { return _createTeamDrive.apply(this, arguments); } return createTeamDrive; }() }, { key: "deleteTeamDrive", value: function () { var _deleteTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee21(teamDriveId) { var client, res; return _regeneratorRuntime().wrap(function _callee21$(_context21) { while (1) switch (_context21.prev = _context21.next) { case 0: _context21.next = 2; return this._getAuthClient(); case 2: client = _context21.sent; _context21.next = 5; return client["delete"]("".concat(DRIVE_V3_ENDPOINT, "/drives/").concat(teamDriveId), {}, false); case 5: res = _context21.sent; return _context21.abrupt("return", !res.error); case 7: case "end": return _context21.stop(); } }, _callee21, this); })); function deleteTeamDrive(_x21) { return _deleteTeamDrive.apply(this, arguments); } return deleteTeamDrive; }() }, { key: "addUserToTeamDrive", value: function () { var _addUserToTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee22(driveId, email, role) { var client; return _regeneratorRuntime().wrap(function _callee22$(_context22) { while (1) switch (_context22.prev = _context22.next) { case 0: _context22.next = 2; return this._getAuthClient(); case 2: client = _context22.sent; return _context22.abrupt("return", client.post("".concat(DRIVE_V3_ENDPOINT, "/files/").concat(driveId, "/permissions?supportsAllDrives=true"), { role: role, type: 'user', emailAddress: email }, false)); case 4: case "end": return _context22.stop(); } }, _callee22, this); })); function addUserToTeamDrive(_x22, _x23, _x24) { return _addUserToTeamDrive.apply(this, arguments); } return addUserToTeamDrive; }() }, { key: "addOrganizerToTeamDrive", value: function () { var _addOrganizerToTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee23(driveId, email) { return _regeneratorRuntime().wrap(function _callee23$(_context23) { while (1) switch (_context23.prev = _context23.next) { case 0: return _context23.abrupt("return", this.addUserToTeamDrive(driveId, email, 'organizer')); case 1: case "end": return _context23.stop(); } }, _callee23, this); })); function addOrganizerToTeamDrive(_x25, _x26) { return _addOrganizerToTeamDrive.apply(this, arguments); } return addOrganizerToTeamDrive; }() }, { key: "addFileOrganizerToTeamDrive", value: function () { var _addFileOrganizerToTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee24(driveId, email) { return _regeneratorRuntime().wrap(function _callee24$(_context24) { while (1) switch (_context24.prev = _context24.next) { case 0: return _context24.abrupt("return", this.addUserToTeamDrive(driveId, email, 'fileOrganizer')); case 1: case "end": return _context24.stop(); } }, _callee24, this); })); function addFileOrganizerToTeamDrive(_x27, _x28) { return _addFileOrganizerToTeamDrive.apply(this, arguments); } return addFileOrganizerToTeamDrive; }() }, { key: "addReaderToTeamDrive", value: function () { var _addReaderToTeamDrive = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee25(driveId, email) { return _regeneratorRuntime().wrap(function _callee25$(_context25) { while (1) switch (_context25.prev = _context25.next) { case 0: return _context25.abrupt("return", this.addUserToTeamDrive(driveId, email, 'reader')); case 1: case "end": return _context25.stop(); } }, _callee25, this); })); function addReaderToTeamDrive(_x29, _x30) { return _addReaderToTeamDrive.apply(this, arguments); } return addReaderToTeamDrive; }() // async moveFileToFolder(file_id, folder_id) { // const client = await this._getAuthClient(); // const json = await client.patchQueryUrl(`${DRIVE_V3_ENDPOINT}/${file_id}`, { // addParents: folder_id, // supportsAllDrives: true, // supportsTeamDrives: true, // alt: 'json' // }); // return json; // } // // async upload(file, options = {}) { // // if (fs.existsSync(file)) { // options.file_size = fs.statSync(file).size; // options.mime_type = mime.contentType(path.extname(file)); // options.name = options.name ? options.name : path.basename(file); // // if (options.file_size >= 4000000) { // return this._resumableUpload(file, options); // } else { // return this._multipartUpload(file, options); // } // } // // throw new ErrorException('GOOGLE_DRIVE_UPLOAD', `File not ${file} exists`); // } // // async _multipartUpload(file, options = {}) { // // const metadata = { // name: options.name, // mimeType: options.mime_type // }; // if (options.parent) { // metadata.parents = [options.parent]; // } // // const data = [ // { // 'Content-Disposition': 'application/json; charset=UTF-8', // body: JSON.stringify(metadata) // }, // { // 'content-type': options.mime_type, // body: fs.createReadStream(file) // } // ]; // const client = await this._getAuthClient(); // return await client.postMultipart(UPLOAD_DRIVE_V3_ENDPOINT + '?uploadType=multipart', data); // } // // async _resumableUpload(file, options = {}) { // const metadata = { // name: options.name, // mimeType: options.mime_type // }; // if (options.parent) { // metadata.parents = [options.parent]; // } // const client = await this._getAuthClient(); // let res = await client.noFollow() // .responseFull() // .addHeader('Content-Type', 'application/json; charset=UTF-8') // .addHeader('X-Upload-Content-Type', options.mime_type) // .addHeader('X-Upload-Content-Length', options.file_size) // .postJSON(UPLOAD_DRIVE_V3_ENDPOINT + '?uploadType=resumable', metadata); // // if (res.headers.location) { // const resumable_url = res.headers.location; // const chunk_size = 4 * 1024 * 1024; // let start = 0; // // while (start < options.file_size) { // const chunk = readChunk.sync(file, start, chunk_size); // const end = start + chunk.length - 1; // const range = `bytes ${start}-${end}/${options.file_size}`; // start = end + 1; // // let retry = 0; // let res = null; // do { // res = await client.responseFull() // .addHeader('Content-Length', chunk.length) // .addHeader('Content-Range', range) // .putRaw(resumable_url, chunk); // if (res.headers.range) { // retry = 100; // const current_upload = Number(res.headers.range.replace('bytes=0-', '')); // const process = 100 * current_upload / options.file_size; // // console.log('Uploading: ', `${process.toFixed(2)}%`); // } else if (res.statusCode === 200) { // retry = 100; // } else { // console.log(`Error ${res.statusCode}: wait retry ${retry}`); // await timeout(1000); // retry++;