ecoledirecte.js
Version:
Good-looking client for EcoleDirecte's private API.
182 lines (181 loc) • 6.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFullTree = exports.cleanPath = exports.getExtension = exports.File = exports.Folder = exports.Cloud = void 0;
const util_1 = require("../util");
class Cloud {
constructor(root, owner) {
this._root = root;
this._owner = owner;
if (root.quota === undefined)
throw new Error("This doesn't seem to be the cloud's root.");
}
get quota() {
return this._root.quota;
}
get size() {
return this._root.taille;
}
root() {
return new Folder(this._root, this);
}
/**
* @description Will fetch the desired path
*/
async get(path) {
const steps = parsePath(path);
let lastStep = await this.root().load();
for (const step of steps) {
if (lastStep && lastStep.type === "folder") {
const child = lastStep.children.find(c => c.name === step);
lastStep =
child && child.type === "folder" ? await child.load() : child;
}
else {
return null;
}
}
return lastStep !== null && lastStep !== void 0 ? lastStep : null;
}
}
exports.Cloud = Cloud;
class Folder {
constructor(o, cloud) {
this.type = "folder";
const pathPrefix = cloud._root.id;
this._cloud = cloud;
this._raw = o;
this.name = o.libelle;
this.path = cleanPath(o.id, pathPrefix);
this.size = o.taille;
this.children = o.children.map(c => {
if (!["folder", "file"].includes(c.type))
throw new Error(`Unexpected cloud type: ${c.type}`);
return c.type === "folder" ? new Folder(c, cloud) : new File(c, cloud);
});
}
async load(force = false) {
if (!this._raw.isLoaded || force) {
const res = await (0, util_1.getCloudFolder)(this._cloud._owner, encodeURI(cleanPath(this._raw.id, this._cloud._root.id).replace(/\//g, "\\")));
this._cloud._owner.token = res.token;
const folder = new Folder(res.data[0], this._cloud);
return folder;
}
else {
return this;
}
}
/**
* @description Loads every children recursively. Non-destructive.
* @returns New Folder instance with every children. The files aren't downloaded.
*/
async loadFullTree() {
const fullTree = await getFullTree(this._raw, this._cloud);
return new Folder(fullTree, this._cloud);
}
/**
* @description Returns the requested path from its children (doesn't fetch anything)
*/
get(path) {
/**
* @description To assign `this` to a value
*/
function that(that) {
return that;
}
const steps = parsePath(path);
let lastStep = that(this);
for (const step of steps) {
if (lastStep && lastStep.type === "folder") {
const child = lastStep.children.find(c => c.name === step);
lastStep = child;
}
else {
return null;
}
}
return lastStep !== null && lastStep !== void 0 ? lastStep : null;
}
}
exports.Folder = Folder;
class File {
constructor(o, cloud) {
this.type = "file";
const pathPrefix = cloud._root.id;
this._cloud = cloud;
this._raw = o;
this.name = o.libelle;
this.path = cleanPath(o.id, pathPrefix);
this.date = new Date(o.date);
this.owner = o.proprietaire
? {
id: o.proprietaire.id,
type: o.proprietaire.type,
firstName: o.proprietaire.prenom,
lastName: o.proprietaire.nom,
particle: o.proprietaire.particule || undefined,
}
: undefined;
this.extension = getExtension(o.libelle);
}
async download(token) {
const [buf, str] = await (0, util_1.fetchFile)(this._raw.id, token || this._cloud._owner.token);
this._downloaded = buf;
this._downloadedUri = str;
return buf;
}
get downloaded() {
return {
buffer: this._downloaded,
uri: this._downloadedUri,
};
}
}
exports.File = File;
function getExtension(name) {
const dotSplit = name.split("."), dotLast = dotSplit[dotSplit.length - 1], extension = dotLast !== name && !!dotLast ? dotLast : "";
return extension;
}
exports.getExtension = getExtension;
function cleanPath(dirty, prefix) {
if (dirty === prefix)
return "/";
if (dirty.indexOf(prefix) === -1)
throw new Error("Prefix expected");
const clean = "/" +
dirty
.substr(dirty.indexOf(prefix) + (prefix + "\\").length)
.replace(/\\/g, "/");
return clean;
}
exports.cleanPath = cleanPath;
function parsePath(path) {
const steps = [];
const slashes = [];
if (path && !path.startsWith("/"))
path = "/" + path;
for (let i = 0; i < path.length; i++) {
const isSlash = path[i] === "/" && path[i - 1] !== "\\";
if (isSlash)
slashes.push(i);
}
for (let i = 0; i < slashes.length; i++) {
const start = slashes[i] + 1;
const end = slashes[i + 1] || path.length;
if (start < end)
steps.push(path.substring(start, end));
}
return steps;
}
async function getFullTree(folder, cloud) {
let newFolder = folder;
// Load folder
if (!folder.isLoaded) {
const folderRes = await (0, util_1.getCloudFolder)(cloud._owner, encodeURI(cleanPath(folder.id, cloud._root.id).replace(/\//g, "\\")));
cloud._owner.token = folderRes.token;
newFolder = folderRes.data[0];
}
// Recursively call function
newFolder.children = await Promise.all(newFolder.children.map(async (c) => c.type === "folder" ? getFullTree(c, cloud) : c));
return newFolder;
}
exports.getFullTree = getFullTree;