nextcloud-node-client
Version:
Nextcloud client API for node.js TypeScript applications
250 lines (249 loc) • 8.35 kB
JavaScript
"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 });
// tslint:disable-next-line:no-var-requires
const debug = require("debug").debug("Folder");
const error_1 = __importDefault(require("./error"));
class Folder {
constructor(client, name, baseName, lastmod, id = -1) {
this.client = client;
this.memento = {
baseName,
deleted: false,
id,
lastmod: new Date(lastmod),
name,
};
}
get name() {
this.assertExistence();
return this.memento.name;
}
get baseName() {
this.assertExistence();
return this.memento.baseName;
}
get lastmod() {
this.assertExistence();
return this.memento.lastmod;
}
get id() {
this.assertExistence();
return this.memento.id;
}
/**
* @returns an array of subfolders
* @throws Error
*/
getSubFolders() {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
return yield this.client.getSubFolders(this.name);
});
}
/**
* returns true if the current folder has a subfolder with the given base name
* @param subFolderBaseName the base name of the subfolder like "products"
*/
hasSubFolder(subFolderBaseName) {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
const subFolder = yield this.client.getFolder(this.name + "/" + subFolderBaseName);
if (subFolder) {
return true;
}
return false;
});
}
/**
* @returns all files of the folder
*/
getFiles(options) {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
return yield this.client.getFiles(this.name, options);
});
}
/**
* creates a subfolder
* @param subFolderBaseName name of the subfolder basename
*/
createSubFolder(subFolderBaseName) {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
return yield this.client.createFolder(this.name + "/" + subFolderBaseName);
});
}
/**
* get a file by basename
* @param fileBaseName the base name of the file
* @returns the file of null
* @throws Error
*/
getFile(fileBaseName) {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
return this.client.getFile(this.name + "/" + fileBaseName);
});
}
/**
* creates a file in the folder
* @param fileBaseName the base name of the file
* @param data the buffer or stream with file content
* @returns the new file or null
* @throws Error
*/
createFile(fileBaseName, data) {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
// must not contain :/\*"<>?
debug("createFile fileBaseName = %s", fileBaseName);
const invalidChars = [":", "*", "/", "\\", "\"", "?", "<", ">"];
// debug("createFile invalidChars = %O", invalidChars);
for (const invalidChar of invalidChars) {
if (fileBaseName.indexOf(invalidChar) !== -1) {
throw new error_1.default("Filename contains an invalid character '" + invalidChar + "'", "ERR_INVALID_CHAR_IN_FILE_NAME", { fileBaseName });
}
}
return this.client.createFile(this.name + "/" + fileBaseName.replace(/^\/+/g, ""), data);
});
}
/**
* deletes the folder and the contained files
* @throws Error
*/
delete() {
return __awaiter(this, void 0, void 0, function* () {
debug("delete");
this.memento.deleted = true;
return yield this.client.deleteFolder(this.memento.name);
});
}
/**
* renames or moves the current folder to the new location
* target folder must exists
* @param targetFolderName the name of the target folder /f1/f2/target
* @throws Error
*/
move(targetFolderName) {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
const folder = yield this.client.moveFolder(this.name, targetFolderName);
this.memento.name = folder.name;
this.memento.baseName = folder.baseName;
this.memento.lastmod = folder.lastmod;
return this;
});
}
/**
* @returns the url of the folder
* @throws Error
*/
getUrl() {
this.assertExistence();
return this.client.getLink(this.name);
}
/**
* @returns the url of the folder in the UI
* @throws Error
*/
getUIUrl() {
this.assertExistence();
return this.client.getUILink(this.id);
}
/**
* @returns true if the folder contains a file with the given basename
* @param fileBaseName file basename
* @throws Error
*/
containsFile(fileBaseName) {
return __awaiter(this, void 0, void 0, function* () {
this.assertExistence();
let file;
file = yield this.getFile(fileBaseName);
if (file) {
return true;
}
return false;
});
}
/**
* adds a tag name to the folder
* @param tagName name of the tag
*/
addTag(tagName) {
return __awaiter(this, void 0, void 0, function* () {
return yield 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 tagNames = [];
const tagId = map.get(tagName);
if (tagId) {
yield this.client.removeTagOfFile(this.id, tagId);
}
});
}
/**
* add comment to folder
* @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 folder
* @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 error_1.default("Folder does not exist", "ERR_FOLDER_NOT_EXISTING");
}
}
}
exports.default = Folder;