kura
Version:
The FileSystem API abstraction library.
230 lines • 8.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractDirectoryEntry = void 0;
const AbstractEntry_1 = require("./AbstractEntry");
const DefaultDirectoryReader_1 = require("./DefaultDirectoryReader");
const FileError_1 = require("./FileError");
const FileSystemConstants_1 = require("./FileSystemConstants");
const FileSystemUtil_1 = require("./FileSystemUtil");
class AbstractDirectoryEntry extends AbstractEntry_1.AbstractEntry {
constructor(params) {
super(params);
this.isDirectory = true;
this.isFile = false;
}
copyTo(parent, newName, successCallback, errorCallback) {
if (!this.canCopy(parent, newName, errorCallback)) {
return;
}
parent.getDirectory(newName || this.name, { create: true }, (dirEntry) => {
const reader = this.createReader();
reader.readEntries((entries) => {
const promises = [];
for (const entry of entries) {
promises.push(new Promise((resolve, reject) => {
entry.copyTo(dirEntry, entry.name, resolve, reject);
}));
}
Promise.all(promises)
.then(() => {
successCallback(dirEntry);
})
.catch((errors) => {
(0, FileSystemUtil_1.onError)(errors, errorCallback);
});
}, errorCallback);
}, errorCallback);
}
createReader() {
return new DefaultDirectoryReader_1.DefaultDirectoryReader(this);
}
async delete() {
const accessor = this.params.accessor;
if (await this.hasChild()) {
throw new FileError_1.InvalidModificationError(this.filesystem.name, this.fullPath, `${this.fullPath} is not empty`);
}
await accessor.remove(this.params);
}
getDirectory(path, options, successCallback, errorCallback) {
const fullPath = (0, FileSystemUtil_1.resolveToFullPath)(this.fullPath, path);
if (fullPath === FileSystemConstants_1.DIR_SEPARATOR) {
successCallback(this.filesystem.root);
return;
}
if (!options) {
options = {};
}
if (!successCallback) {
successCallback = () => {
};
}
this.params.accessor
.getObject(fullPath, false)
.then((obj) => {
if (obj.size != null) {
(0, FileSystemUtil_1.onError)(new FileError_1.PathExistsError(this.filesystem.name, fullPath, `${fullPath} is not a directory`), errorCallback);
return;
}
if (options.create) {
if (options.exclusive) {
(0, FileSystemUtil_1.onError)(new FileError_1.PathExistsError(fullPath, `${fullPath} already exists`), errorCallback);
return;
}
}
successCallback(this.toDirectoryEntry(obj));
})
.catch((err) => {
if (err instanceof FileError_1.NotFoundError) {
if (options.create) {
this.registerObject(fullPath, false)
.then((newObj) => {
successCallback(this.toDirectoryEntry(newObj));
})
.catch((err) => {
(0, FileSystemUtil_1.onError)(err, errorCallback);
});
}
else {
(0, FileSystemUtil_1.onError)(err, errorCallback);
}
}
else {
(0, FileSystemUtil_1.onError)(err, errorCallback);
}
});
}
getFile(path, options, successCallback, errorCallback) {
const fullPath = (0, FileSystemUtil_1.resolveToFullPath)(this.fullPath, path);
if (!options) {
options = {};
}
if (!successCallback) {
successCallback = () => {
};
}
this.params.accessor
.getObject(fullPath, true)
.then((obj) => {
if (obj.size == null) {
(0, FileSystemUtil_1.onError)(new FileError_1.PathExistsError(this.filesystem.name, fullPath, `${fullPath} is not a file`), errorCallback);
return;
}
if (options.create) {
if (options.exclusive) {
(0, FileSystemUtil_1.onError)(new FileError_1.PathExistsError(this.filesystem.name, fullPath, `${fullPath} already exists`), errorCallback);
return;
}
}
successCallback(this.toFileEntry(obj));
})
.catch((err) => {
if (err instanceof FileError_1.NotFoundError) {
if (options.create) {
this.registerObject(fullPath, true)
.then((newObj) => {
successCallback(this.toFileEntry(newObj));
})
.catch((err) => {
errorCallback(err);
});
}
else {
errorCallback(err);
}
}
else {
(0, FileSystemUtil_1.onError)(err, errorCallback);
}
});
}
list(successCallback, errorCallback) {
this.params.accessor
.getObjects(this.fullPath)
.then((objects) => {
successCallback(this.createEntries(objects));
})
.catch((err) => {
(0, FileSystemUtil_1.onError)(err, errorCallback);
});
}
moveTo(parent, newName, successCallback, errorCallback) {
if (!this.canCopy(parent, newName, errorCallback)) {
return;
}
parent.getDirectory(newName || this.name, { create: true }, (dirEntry) => {
const reader = this.createReader();
reader.readEntries((entries) => {
const promises = [];
for (const entry of entries) {
promises.push(new Promise((resolve, reject) => {
entry.moveTo(dirEntry, entry.name, resolve, reject);
}));
}
Promise.all(promises)
.then(() => {
successCallback(dirEntry);
})
.catch((errors) => {
(0, FileSystemUtil_1.onError)(errors, errorCallback);
});
}, errorCallback);
}, errorCallback);
}
remove(successCallback, errorCallback) {
this.hasChild()
.then((result) => {
if (result) {
(0, FileSystemUtil_1.onError)(new FileError_1.InvalidModificationError(this.filesystem.name, this.fullPath, `${this.fullPath} is not empty`), errorCallback);
return;
}
this.params.accessor
.remove(this.params)
.then(() => {
successCallback();
})
.catch((err) => {
(0, FileSystemUtil_1.onError)(err, errorCallback);
});
})
.catch((err) => {
(0, FileSystemUtil_1.onError)(err, errorCallback);
});
}
removeRecursively(successCallback, errorCallback) {
this.params.accessor
.removeRecursively(this.params)
.then(() => {
successCallback();
})
.catch((err) => {
(0, FileSystemUtil_1.onError)(err, errorCallback);
});
}
createEntries(objects) {
const entries = [];
for (const obj of objects) {
if (obj.fullPath.startsWith(FileSystemConstants_1.INDEX_DIR_PATH)) {
continue;
}
entries.push(this.createEntry(obj));
}
return entries;
}
async hasChild() {
const objects = await this.params.accessor.getObjects(this.fullPath);
return 0 < objects.length;
}
async registerObject(fullPath, isFile) {
const accessor = this.params.accessor;
const obj = (0, FileSystemUtil_1.createFileSystemObject)(fullPath, isFile);
if (isFile) {
await accessor.putObject(obj, new Blob([], FileSystemConstants_1.DEFAULT_BLOB_PROPS));
}
else {
await accessor.putObject(obj);
}
return obj;
}
}
exports.AbstractDirectoryEntry = AbstractDirectoryEntry;
//# sourceMappingURL=AbstractDirectoryEntry.js.map