@apillon/sdk
Version:
▶◀ Apillon SDK for NodeJS ▶◀
88 lines • 3.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Directory = void 0;
const storage_1 = require("../../types/storage");
const file_1 = require("./file");
const common_1 = require("../../lib/common");
const apillon_api_1 = require("../../lib/apillon-api");
const apillon_1 = require("../../lib/apillon");
const apillon_logger_1 = require("../../lib/apillon-logger");
class Directory extends apillon_1.ApillonModel {
/**
* Constructor which should only be called via StorageBucket class.
* @param bucketUuid Unique identifier of the directory's bucket.
* @param directoryUuid Unique identifier of the directory.
* @param data Data to populate the directory with.
*/
constructor(bucketUuid, directoryUuid, data) {
super(directoryUuid);
/**
* Unique identifier of the bucket.
*/
this.bucketUuid = null;
/**
* Directory name.
*/
this.name = null;
/**
* Directory unique IPFS content identifier.
*/
this.CID = null;
/**
* Id of the directory in which the file resides.
*/
this.parentDirectoryUuid = null;
/**
* Type of content.
*/
this.type = storage_1.StorageContentType.DIRECTORY;
/**
* Link on IPFS gateway.
*/
this.link = null;
this.content = [];
this.bucketUuid = bucketUuid;
this.API_PREFIX = `/storage/buckets/${bucketUuid}`;
this.populate(data);
}
/**
* Gets contents of a directory.
* @returns Directory data and content (files and subfolders)
*/
async get(params = {}) {
this.content = [];
params.directoryUuid = this.uuid;
const url = (0, common_1.constructUrlWithQueryParams)(`${this.API_PREFIX}/content`, params);
const data = await apillon_api_1.ApillonApi.get(url);
for (const item of data.items) {
if (item.type == storage_1.StorageContentType.FILE) {
const file = item;
this.content.push(new file_1.File(this.bucketUuid, file.directoryUuid, file.uuid, file));
}
else {
this.content.push(new Directory(this.bucketUuid, item.uuid, item));
}
}
return this.content;
}
/**
* Deletes a directory from the bucket.
*/
async delete() {
await apillon_api_1.ApillonApi.delete(`${this.API_PREFIX}/directories/${this.uuid}`);
apillon_logger_1.ApillonLogger.log('Directory deleted successfully');
}
serializeFilter(key, value) {
const serialized = super.serializeFilter(key, value);
const enums = {
type: storage_1.StorageContentType[value],
};
if (Object.keys(enums).includes(key)) {
return enums[key];
}
const excludedKeys = ['content'];
return excludedKeys.includes(key) ? undefined : serialized;
}
}
exports.Directory = Directory;
//# sourceMappingURL=directory.js.map