kura
Version:
The FileSystem API abstraction library.
230 lines • 6.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.onError = exports.isIllegalObject = exports.isIllegalPath = exports.isIllegalFileName = exports.getTextSize = exports.getSize = exports.getMemorySize = exports.createEmptyFile = exports.dataUrlToBase64 = exports.blobToFile = exports.normalizePath = exports.resolveToFullPath = exports.createEntry = exports.getBaseName = exports.getExtension = exports.getName = exports.createFileSystemObject = exports.getParentPath = void 0;
const DirectoryEntryAsync_1 = require("./DirectoryEntryAsync");
const FileEntryAsync_1 = require("./FileEntryAsync");
const FileSystemConstants_1 = require("./FileSystemConstants");
const LAST_PATH_PART = /\/([^/]+)\/?$/;
function getParentPath(fullPath) {
const parentPath = fullPath.replace(LAST_PATH_PART, "");
return parentPath === "" ? FileSystemConstants_1.DIR_SEPARATOR : parentPath;
}
exports.getParentPath = getParentPath;
function createFileSystemObject(fullPath, isFile) {
const obj = {
fullPath,
name: getName(fullPath),
};
if (isFile) {
obj.lastModified = Date.now();
obj.size = 0;
}
return obj;
}
exports.createFileSystemObject = createFileSystemObject;
function getName(fullPath) {
if (!fullPath || fullPath === FileSystemConstants_1.DIR_SEPARATOR) {
return "";
}
if (fullPath.endsWith(FileSystemConstants_1.DIR_SEPARATOR)) {
fullPath = fullPath.substr(0, fullPath.length - 1);
}
const chunks = fullPath.split(FileSystemConstants_1.DIR_SEPARATOR);
if (0 < chunks.length) {
return chunks[chunks.length - 1];
}
else {
return fullPath;
}
}
exports.getName = getName;
function getExtension(fullPath) {
if (!fullPath || fullPath === FileSystemConstants_1.DIR_SEPARATOR) {
return "";
}
const name = getName(fullPath);
const chunks = name.split(".");
if (chunks.length <= 1) {
return "";
}
else if (chunks.length === 2) {
if (!chunks[0]) {
return name;
}
else {
return chunks[1];
}
}
else {
return chunks[chunks.length - 1];
}
}
exports.getExtension = getExtension;
function getBaseName(fullPath) {
if (!fullPath || fullPath === FileSystemConstants_1.DIR_SEPARATOR) {
return "";
}
const name = getName(fullPath);
const chunks = name.split(".");
if (chunks.length === 0) {
return "";
}
else if (chunks.length === 1) {
return chunks[0];
}
else if (chunks.length === 2) {
if (!chunks[0]) {
return name;
}
else {
return chunks[0];
}
}
else {
chunks.splice(chunks.length - 1);
return chunks.join(".");
}
}
exports.getBaseName = getBaseName;
function createEntry(fileSystemAsync, entry) {
return entry.isFile
? new FileEntryAsync_1.FileEntryAsync(fileSystemAsync, entry)
: new DirectoryEntryAsync_1.DirectoryEntryAsync(fileSystemAsync, entry);
}
exports.createEntry = createEntry;
function resolveToFullPath(cwdFullPath, path) {
if (!path) {
return cwdFullPath;
}
cwdFullPath = cwdFullPath.replace(FileSystemConstants_1.LAST_DIR_SEPARATORS, "");
const relativePath = path[0] != FileSystemConstants_1.DIR_SEPARATOR;
if (relativePath) {
path = cwdFullPath + FileSystemConstants_1.DIR_SEPARATOR + path;
}
return normalizePath(path);
}
exports.resolveToFullPath = resolveToFullPath;
function normalizePath(fullPath) {
const parts = fullPath.split(FileSystemConstants_1.DIR_SEPARATOR);
const finalParts = [];
for (const part of parts) {
if (part === "..") {
if (!finalParts.length) {
throw Error("Invalid path");
}
finalParts.pop();
}
else if (part === ".") {
}
else if (part !== "") {
finalParts.push(part);
}
}
fullPath = FileSystemConstants_1.DIR_SEPARATOR + finalParts.join(FileSystemConstants_1.DIR_SEPARATOR);
return fullPath;
}
exports.normalizePath = normalizePath;
function blobToFile(fileBits, name, lastModified, type) {
const file = new File(fileBits, name, {
lastModified: lastModified,
type: type || FileSystemConstants_1.DEFAULT_CONTENT_TYPE,
});
return file;
}
exports.blobToFile = blobToFile;
function dataUrlToBase64(dataUrl) {
const index = dataUrl.indexOf(",");
if (0 <= index) {
return dataUrl.substr(index + 1);
}
return dataUrl;
}
exports.dataUrlToBase64 = dataUrlToBase64;
function createEmptyFile(name) {
return new File([], name, {
lastModified: Date.now(),
type: FileSystemConstants_1.DEFAULT_CONTENT_TYPE,
});
}
exports.createEmptyFile = createEmptyFile;
function getMemorySize(content) {
if (!content) {
return 0;
}
let size;
if (typeof content === "string") {
size = content.length * 2;
}
else if (content instanceof Blob) {
size = content.size;
}
else {
size = content.byteLength;
}
return size;
}
exports.getMemorySize = getMemorySize;
function getSize(content) {
if (!content) {
return 0;
}
let size;
if (typeof content === "string") {
const length = content.length;
let paddingCount = 0;
for (let i = length - 1; 0 <= i; i--) {
const c = content.charAt(i);
if (c !== "=") {
break;
}
paddingCount++;
}
size = length - paddingCount;
}
else if (content instanceof Blob) {
size = content.size;
}
else {
size = content.byteLength;
}
return size;
}
exports.getSize = getSize;
function getTextSize(text) {
return encodeURIComponent(text).replace(/%../g, "x").length;
}
exports.getTextSize = getTextSize;
function isIllegalFileName(name) {
return /[\x00-\x1f\x7f-\x9f\\/:*?"<>|]/.test(name);
}
exports.isIllegalFileName = isIllegalFileName;
function isIllegalPath(fullPath, index) {
if (fullPath === FileSystemConstants_1.DIR_SEPARATOR) {
return true;
}
if (index && fullPath.startsWith(FileSystemConstants_1.INDEX_DIR_PATH)) {
return true;
}
return false;
}
exports.isIllegalPath = isIllegalPath;
function isIllegalObject(obj, index) {
if (isIllegalPath(obj.fullPath, index)) {
return true;
}
if (isIllegalFileName(obj.name)) {
return true;
}
return false;
}
exports.isIllegalObject = isIllegalObject;
function onError(err, errorCallback) {
if (errorCallback) {
errorCallback(err);
}
else {
console.error(err);
}
}
exports.onError = onError;
//# sourceMappingURL=FileSystemUtil.js.map