UNPKG

pterowrap

Version:

A node.js wrapper for Pterodactyl API

97 lines (96 loc) 3.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const File_1 = __importDefault(require("../../../structures/client/server/File")); class FileManager { constructor(client, _parentServer) { this.client = client; this._parentServer = _parentServer; } list(options = {}) { return new Promise(async (resolve, reject) => { var _a, _b; try { const list = await this.client.call({ endpoint: `servers/${this._parentServer.identifier}/files/list`, parameters: options }); const r = []; for (let i = 0; i < list.data.length; i++) { const dir = ((_b = (_a = options.other) === null || _a === void 0 ? void 0 : _a.find((o) => o.key == "directory")) === null || _b === void 0 ? void 0 : _b.value) || "/"; r.push(new File_1.default(this.client, list.data[i], dir, this._parentServer)); } resolve(r); } catch (e) { reject(e); } }); } get(path) { return new Promise(async (resolve, reject) => { try { const pathtoarr = path.split("/"); pathtoarr.pop(); let pathto = pathtoarr.join("/") + "/"; pathto = pathto.replace(/\//g, "%2F"); const files = await this.list({ other: [{ key: "directory", value: pathto }] }); const filename = path.substring(path.lastIndexOf("/") + 1); const found = files.find((f) => f.name == filename) || null; resolve(found); } catch (e) { reject(e); } }); } create(path, data) { return new Promise(async (resolve, reject) => { try { await this.client.call({ endpoint: `servers/${this._parentServer.identifier}/files/write?file=${path.replace(/\//g, "%2F")}`, body: data, method: "POST", }); const f = await this.get(path); resolve(f); } catch (e) { reject(e); } }); } createFolder(path) { return new Promise(async (resolve, reject) => { try { const pathtoarr = path.split("/"); pathtoarr.pop(); await this.client.call({ endpoint: `servers/${this._parentServer.identifier}/files/create-folder`, method: "POST", body: { root: pathtoarr.join("/") + "/", name: path.substring(path.lastIndexOf("/") + 1), }, }); const f = await this.get(path); resolve(f); } catch (e) { reject(e); } }); } retrieveUploadURL() { return new Promise(async (resolve, reject) => { try { resolve((await this.client.call({ endpoint: `servers/${this._parentServer.identifier}/files/upload`, })).attributes.url); } catch (e) { reject(e); } }); } } exports.default = FileManager;