mirror of
https://github.com/penpot/penpot-mcp.git
synced 2026-04-25 11:18:37 +00:00
parent
2b1a287f15
commit
062ef67a7a
@ -142,39 +142,16 @@ export class PenpotUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes a base64 string to a Uint8Array.
|
* Decodes a base64 string to a Uint8Array.
|
||||||
* This is required because the Penpot plugin environment does not provide the atob function.
|
|
||||||
*
|
*
|
||||||
* @param base64 - The base64-encoded string to decode
|
* @param base64 - The base64-encoded string to decode
|
||||||
* @returns The decoded data as a Uint8Array
|
* @returns The decoded data as a Uint8Array
|
||||||
*/
|
*/
|
||||||
public static atob(base64: string): Uint8Array {
|
public static base64ToByteArray(base64: string): Uint8Array {
|
||||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
const binary = atob(base64);
|
||||||
const lookup = new Uint8Array(256);
|
const bytes = new Uint8Array(binary.length);
|
||||||
for (let i = 0; i < chars.length; i++) {
|
for (let i = 0; i < binary.length; i++) {
|
||||||
lookup[chars.charCodeAt(i)] = i;
|
bytes[i] = binary.charCodeAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
let bufferLength = base64.length * 0.75;
|
|
||||||
if (base64[base64.length - 1] === "=") {
|
|
||||||
bufferLength--;
|
|
||||||
if (base64[base64.length - 2] === "=") {
|
|
||||||
bufferLength--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const bytes = new Uint8Array(bufferLength);
|
|
||||||
let p = 0;
|
|
||||||
for (let i = 0; i < base64.length; i += 4) {
|
|
||||||
const encoded1 = lookup[base64.charCodeAt(i)];
|
|
||||||
const encoded2 = lookup[base64.charCodeAt(i + 1)];
|
|
||||||
const encoded3 = lookup[base64.charCodeAt(i + 2)];
|
|
||||||
const encoded4 = lookup[base64.charCodeAt(i + 3)];
|
|
||||||
|
|
||||||
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
|
|
||||||
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
|
|
||||||
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +181,7 @@ export class PenpotUtils {
|
|||||||
height: number | undefined
|
height: number | undefined
|
||||||
): Promise<Rectangle> {
|
): Promise<Rectangle> {
|
||||||
// convert base64 to Uint8Array
|
// convert base64 to Uint8Array
|
||||||
const bytes = PenpotUtils.atob(base64);
|
const bytes = PenpotUtils.base64ToByteArray(base64);
|
||||||
|
|
||||||
// upload the image data to Penpot
|
// upload the image data to Penpot
|
||||||
const imageData = await penpot.uploadMediaData(name, bytes, mimeType);
|
const imageData = await penpot.uploadMediaData(name, bytes, mimeType);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user