UNPKG

taglib-wasm

Version:

TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps

33 lines (32 loc) 869 B
import { InvalidFormatError } from "../errors/classes.js"; function pictureToDataURL(picture) { let binary = ""; for (const byte of picture.data) { binary += String.fromCodePoint(byte); } const base64 = btoa(binary); return `data:${picture.mimeType};base64,${base64}`; } function dataURLToPicture(dataURL, type = "FrontCover", description) { const regex = /^data:([^;]+);base64,(.+)$/; const matches = regex.exec(dataURL); if (!matches) { throw new InvalidFormatError("Invalid data URL format"); } const [, mimeType, base64] = matches; const binaryString = atob(base64); const data = new Uint8Array(binaryString.length); for (let i = 0; i < binaryString.length; i++) { data[i] = binaryString.codePointAt(i); } return { mimeType, data, type, description }; } export { dataURLToPicture, pictureToDataURL };