UNPKG

nextcloud-node-client

Version:

Nextcloud client API for node.js applications

218 lines (217 loc) 6.62 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const client_1 = require("./client"); /** * The file class represents a file in nextcloud. * It exposes file properties and content handling, commenting and tagging */ class File { constructor(client, name, baseName, lastmod, size, mime, id) { this.memento = { baseName, deleted: false, id, lastmod: new Date(lastmod), mime, name, size, }; this.client = client; } /** * The name of the file including the path * The name is readonly */ get name() { this.assertExistence(); return this.memento.name; } /** * The base name of the file (file name without path) * The base name is readonly */ get baseName() { this.assertExistence(); return this.memento.baseName; } /** * The timestamp of the last file change * readonly */ get lastmod() { this.assertExistence(); return this.memento.lastmod; } /** * The file size in bytes * readonly */ get size() { this.assertExistence(); return this.memento.size; } /** * The mime type (content type) of the file */ get mime() { this.assertExistence(); return this.memento.mime; } /** * The unique id of the file. */ get id() { this.assertExistence(); return this.memento.id; } /** * deletes a file * @throws Error */ delete() { return __awaiter(this, void 0, void 0, function* () { this.memento.deleted = true; return yield this.client.deleteFile(this.memento.name); }); } /** * get folder of the file * @throws ClientError * @returns the parent folder */ getFolder() { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); const folder = yield this.client.getFolder(path_1.default.dirname((this.memento.name))); if (folder) { return folder; } throw new client_1.ClientError("Error, the folder of the file does not exist anymore", "ERR_FILE_FOLDER_DOES_NOT_EXIST"); }); } /** * moves or renames the current file to the new location * target folder must exists * @param targetFileName the name of the target file /f1/f2/myfile.txt * @throws Error */ move(targetFileName) { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); const file = yield this.client.moveFile(this.name, targetFileName); this.memento.name = file.name; this.memento.baseName = file.baseName; this.memento.lastmod = file.lastmod; this.memento.mime = file.mime; this.memento.size = file.size; return this; }); } /** * @returns the buffer of the file content * @throws Error */ getContent() { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); return this.client.getContent(this.name); }); } /** * @returns the url of the file * @throws Error */ getUrl() { this.assertExistence(); return this.client.getLink(this.name); } /** * @returns the url of the file in the UI * @throws Error */ getUIUrl() { this.assertExistence(); return this.client.getUILink(this.id); } /** * adds a tag name to the file * @param tagName name of the tag */ addTag(tagName) { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); return this.client.addTagToFile(this.id, tagName); }); } /** * get tag names * @returns array of tag names */ getTags() { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); const map = yield this.client.getTagsOfFile(this.id); const tagNames = []; for (const tagName of map) { tagNames.push(tagName[0]); } return tagNames; }); } /** * removes a tag of the file * @param tagName the name of the tag */ removeTag(tagName) { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); const map = yield this.client.getTagsOfFile(this.id); const tagId = map.get(tagName); if (tagId) { yield this.client.removeTagOfFile(this.id, tagId); } }); } /** * add comment to file * @param comment the comment */ addComment(comment) { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); return yield this.client.addCommentToFile(this.id, comment); }); } /** * get list of comments of file * @param top number of comments to return * @param skip the offset * @returns array of comment strings * @throws Exception */ getComments(top, skip) { return __awaiter(this, void 0, void 0, function* () { this.assertExistence(); return yield this.client.getFileComments(this.id, top, skip); }); } assertExistence() { if (this.memento.deleted) { throw new client_1.ClientError("File does not exist", "ERR_FILE_NOT_EXISTING"); } } } exports.default = File;