UNPKG

@bitbybit-dev/core

Version:

Bit By Bit Developers Core CAD API to Program Geometry

223 lines (222 loc) 7.38 kB
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()); }); }; import { AssetManager } from "../../asset-manager"; export class Asset { constructor() { this.assetManager = new AssetManager(); } /** * Gets the asset file * @param inputs file name to get from project assets * @returns Blob of asset * @group get * @shortname cloud file */ getFile(inputs) { return this.assetManager.getAsset(inputs.fileName); } /** * Gets the text from asset file stored in your cloud account. * @param inputs asset name to get from project assets * @returns Text of asset * @group get * @shortname text file */ getTextFile(inputs) { return __awaiter(this, void 0, void 0, function* () { const file = yield this.assetManager.getAsset(inputs.fileName); return yield file.text(); }); } /** * Gets the local asset file stored in your browser. * @param inputs asset name to get from local assets * @returns Blob of asset * @group get * @shortname local file */ getLocalFile(inputs) { return this.assetManager.getLocalAsset(inputs.fileName); } /** * Gets the text from asset file stored in your browser. * @param inputs asset name to get from local assets * @returns Text of asset or array of texts * @group get * @shortname local text file */ getLocalTextFile(inputs) { return __awaiter(this, void 0, void 0, function* () { const files = yield this.getLocalFile(inputs); if (Array.isArray(files)) { return yield Promise.all(files.map(f => f.text())); } else { return yield files.text(); } }); } /** * Fetches the blob from the given url, must be CORS enabled accessible endpoint * @param inputs url of the asset * @returns Blob * @group fetch * @shortname fetch blob */ fetchBlob(inputs) { return __awaiter(this, void 0, void 0, function* () { const res = yield fetch(inputs.url); return res.blob(); }); } /** * Fetches the file from the given url, must be CORS enabled accessible endpoint * @param inputs url of the asset * @returns File * @group fetch * @shortname fetch file */ fetchFile(inputs) { return __awaiter(this, void 0, void 0, function* () { const res = yield fetch(inputs.url); const blob = yield res.blob(); return new File([blob], inputs.url.split("/").pop().split("?")[0]); }); } /** * Fetches the json from the given url, must be CORS enabled accessible endpoint * @param inputs url of the asset * @returns JSON * @group fetch * @shortname fetch json */ fetchJSON(inputs) { return __awaiter(this, void 0, void 0, function* () { const res = yield fetch(inputs.url); return res.json(); }); } /** * Fetches the json from the given url, must be CORS enabled accessible endpoint * @param inputs url of the asset * @returns Text * @group fetch * @shortname fetch text */ fetchText(inputs) { return __awaiter(this, void 0, void 0, function* () { const res = yield fetch(inputs.url); return res.text(); }); } /** * Gets and creates the url string path to your file stored in your memory. * @param File or a blob * @returns URL string of a file * @group create * @shortname object url */ createObjectURL(inputs) { return URL.createObjectURL(inputs.file); } /** * Gets and creates the url string paths to your files stored in your memory. * @param Files or a blobs * @returns URL strings for given files * @group create * @shortname object urls */ createObjectURLs(inputs) { return inputs.files.map(f => URL.createObjectURL(f)); } /** * Downloads a file with the given content, extension, and content type. * @param inputs file name, content, extension, and content type * @group download * @shortname download file */ download(inputs) { let blob; if (typeof inputs.content === "string") { blob = new Blob([inputs.content], { type: inputs.contentType }); } else { blob = inputs.content; } this.assetManager.downloadFile(blob, inputs.fileName, inputs.extension, inputs.contentType); } /** * Converts a File or Blob to an ArrayBuffer. * @param inputs file or blob to convert * @returns ArrayBuffer * @group convert * @shortname to array buffer */ toArrayBuffer(inputs) { return __awaiter(this, void 0, void 0, function* () { return yield inputs.file.arrayBuffer(); }); } /** * Converts a File or Blob to a Uint8Array. * @param inputs file or blob to convert * @returns Uint8Array * @group convert * @shortname to uint8 array */ toUint8Array(inputs) { return __awaiter(this, void 0, void 0, function* () { const buffer = yield inputs.file.arrayBuffer(); return new Uint8Array(buffer); }); } /** * Converts a Blob to a File. * @param inputs blob, file name, and optional MIME type * @returns File * @group convert * @shortname blob to file */ blobToFile(inputs) { var _a; const type = (_a = inputs.mimeType) !== null && _a !== void 0 ? _a : inputs.blob.type; return new File([inputs.blob], inputs.fileName, { type }); } /** * Converts a File to a Blob. * @param inputs file to convert * @returns Blob * @group convert * @shortname file to blob */ fileToBlob(inputs) { return inputs.file.slice(0, inputs.file.size, inputs.file.type); } /** * Converts an ArrayBuffer to a Uint8Array. * @param inputs ArrayBuffer to convert * @returns Uint8Array * @group convert * @shortname array buffer to uint8 array */ arrayBufferToUint8Array(inputs) { return new Uint8Array(inputs.arrayBuffer); } /** * Converts a Uint8Array to an ArrayBuffer. * @param inputs Uint8Array to convert * @returns ArrayBuffer * @group convert * @shortname uint8 array to array buffer */ uint8ArrayToArrayBuffer(inputs) { return inputs.uint8Array.buffer.slice(inputs.uint8Array.byteOffset, inputs.uint8Array.byteOffset + inputs.uint8Array.byteLength); } }