UNPKG

@squarecloud/blob

Version:
156 lines (151 loc) 5.26 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; // src/utils/mimetype/mimetypes.ts var mimeTypesWithExtension = { "video/mp4": ["mp4"], "video/mpeg": ["mpeg"], "video/webm": ["webm"], "video/x-flv": ["flv"], "video/x-m4v": ["m4v"], "image/jpeg": ["jpg", "jpeg"], "image/png": ["png"], "image/apng": ["apng"], "image/tiff": ["tiff"], "image/gif": ["gif"], "image/webp": ["webp"], "image/bmp": ["bmp"], "image/svg+xml": ["svg"], "image/x-icon": ["ico"], "image/ico": ["ico"], "image/cur": ["cur"], "image/heic": ["heic"], "image/heif": ["heif"], "audio/wav": ["wav"], "audio/ogg": ["ogg"], "audio/opus": ["opus"], "audio/mp4": ["mp4"], "audio/mpeg": ["mp3"], "audio/aac": ["aac"], "text/plain": ["txt"], "text/html": ["html"], "text/css": ["css"], "text/csv": ["csv"], "text/x-sql": ["sql"], "application/xml": ["xml"], "application/sql": ["sql"], "application/x-sql": ["sql"], "application/x-sqlite3": ["sqlite3"], "application/x-pkcs12": ["pfx"], "application/pdf": ["pdf"], "application/json": ["json"], "application/javascript": ["js"] }; var mimeTypes = Object.keys(mimeTypesWithExtension); // src/utils/mimetype/index.ts var MimeTypeUtil = class { /** * Returns the corresponding MIME type for a given file extension. * * @param extension - The file extension to search for. * @return The MIME type associated with the extension, or "text/plain" if not found. * * @example * ```js * MimeTypeUtil.fromExtension("jpeg") // "image/jpeg" | Supported * MimeTypeUtil.fromExtension("json") // "application/json" | Supported * MimeTypeUtil.fromExtension("potato") // "text/plain" | Unsupported, defaults to text/plain * ``` */ static fromExtension(extension) { const entries = Object.entries(mimeTypesWithExtension); const mimeType = entries.find( ([, extensions]) => extensions.includes(extension) )?.[0]; return mimeType || "text/plain"; } }; /** Supported mime types with their extensions */ __publicField(MimeTypeUtil, "mimeTypesWithExtension", mimeTypesWithExtension); /** All supported mime types */ __publicField(MimeTypeUtil, "mimeTypes", mimeTypes); // src/structures/error.ts var SquareCloudBlobError = class _SquareCloudBlobError extends Error { constructor(code, message, cause) { super(message, { cause }); this.name = _SquareCloudBlobError.name; this.message = this.getMessage(code); } getMessage(rawCode) { const code = rawCode.replaceAll("_", " ").toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase()); const message = this.message ? `: ${this.message}` : ""; return `${code}${message}`; } }; // src/utils/object-url.ts var objectUrlRegex = /^(?<url>https:\/\/public-blob\.squarecloud\.dev)?\/?(?<userId>\d+\/)(?<prefix>[\w\d-_]+\/)?(?<name>[\w\d_]+)-(?<hash>[\w\d]+)\.(?<extension>\w+)$/; function parseObjectUrl(url) { const match = url.match(objectUrlRegex); if (!match?.groups) { throw new SquareCloudBlobError("Invalid object URL"); } const payload = { userId: match.groups.userId.replace("/", ""), prefix: match.groups.prefix?.replace("/", ""), name: match.groups.name, hash: match.groups.hash, extension: match.groups.extension }; return { id: `${payload.userId}/${payload.prefix ? `${payload.prefix}/` : ""}${payload.name}-${payload.hash}.${payload.extension}`, ...payload }; } // src/structures/object.ts var BlobObject = class { constructor(data) { /** The id of the object */ __publicField(this, "id"); /** The url to view or download the object */ __publicField(this, "url"); /** The name of the object */ __publicField(this, "name"); /** The prefix of the object (Optional) */ __publicField(this, "prefix"); /** The hash of the object */ __publicField(this, "hash"); /** The id of the user who created the object */ __publicField(this, "userId"); /** The file extension of the object */ __publicField(this, "extension"); /** The MIME type of the object */ __publicField(this, "mimeType"); /** The size of the object in bytes */ __publicField(this, "size"); /** The expiration date of the object (Only available using `objects.list`) */ __publicField(this, "expiresAt"); /** The creation date of the object (Only available using `objects.list`) */ __publicField(this, "createdAt"); const { id, name, prefix, hash, userId, extension } = parseObjectUrl( data.idOrUrl ); this.id = id; this.url = `https://public-blob.squarecloud.dev/${id}`; this.name = name; this.prefix = prefix; this.hash = hash; this.userId = userId; this.extension = extension; this.mimeType = MimeTypeUtil.fromExtension(extension); this.size = data.size; this.expiresAt = data.expiresAt; this.createdAt = data.createdAt; } }; export { BlobObject }; //# sourceMappingURL=object.mjs.map