kura
Version:
The FileSystem API abstraction library.
118 lines • 4.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractFileWriter = void 0;
const FileSystemUtil_1 = require("./FileSystemUtil");
const FileError_1 = require("./FileError");
class AbstractFileWriter {
constructor(fileEntry, file) {
this.fileEntry = fileEntry;
this.file = file;
this.position = 0;
}
get length() {
return this.fileEntry.size;
}
abort() {
throw new FileError_1.NotImplementedError(this.fileEntry.filesystem.name, this.fileEntry.fullPath);
}
addEventListener(type, listener, options) {
throw new FileError_1.NotImplementedError(this.fileEntry.filesystem.name, this.fileEntry.fullPath);
}
dispatchEvent(event) {
throw new FileError_1.NotImplementedError(this.fileEntry.filesystem.name, this.fileEntry.fullPath);
}
removeEventListener(type, callback, options) {
throw new FileError_1.NotImplementedError(this.fileEntry.filesystem.name, this.fileEntry.fullPath);
}
seek(offset) {
this.position = offset;
if (this.length < this.position) {
this.position = this.length;
}
else if (this.position < 0) {
this.position = 0;
}
}
truncate(size) {
const current = this.file;
let file;
if (current) {
if (size < this.length) {
file = (0, FileSystemUtil_1.blobToFile)([current.slice(0, size)], current.name, Date.now());
}
else {
file = (0, FileSystemUtil_1.blobToFile)([current, new Uint8Array(size - this.length)], current.name, Date.now());
}
}
else {
file = (0, FileSystemUtil_1.createEmptyFile)(this.fileEntry.name);
}
this.doWrite(file, () => {
this.file = file;
this.position = 0;
});
}
write(data) {
const current = this.file;
if (current) {
const head = current.slice(0, this.position);
const tail = current.slice(this.position + data.size);
let padding = this.position - head.size;
if (padding < 0) {
padding = 0;
}
const file = (0, FileSystemUtil_1.blobToFile)([head, new Uint8Array(padding), data, tail], current.name, Date.now());
this.doWrite(file, () => {
this.file = file;
this.position += data.size;
});
}
else {
const file = (0, FileSystemUtil_1.blobToFile)([data], this.fileEntry.name, Date.now());
this.doWrite(file, () => {
this.file = file;
this.position = data.size;
});
}
}
doWrite(blob, onsuccess) {
const obj = {
name: this.fileEntry.name,
fullPath: this.fileEntry.fullPath,
lastModified: Date.now(),
size: blob.size,
};
const accessor = this.fileEntry.params.accessor;
accessor
.putObject(obj, blob)
.then(() => {
this.fileEntry.params.lastModified = obj.lastModified;
this.fileEntry.params.size = obj.size;
onsuccess();
if (this.onwriteend) {
const evt = {
loaded: this.position,
total: this.length,
lengthComputable: true,
};
this.onwriteend(evt);
}
})
.catch((err) => {
if (this.onerror) {
const evt = {
error: err,
loaded: this.position,
total: this.length,
lengthComputable: true,
};
this.onerror(evt);
}
else {
console.error("AbstractFileWriter#doWrite", err);
}
});
}
}
exports.AbstractFileWriter = AbstractFileWriter;
//# sourceMappingURL=AbstractFileWriter.js.map