penpot / penpot-plugins

Repository for developing and running Penpot plugins with examples and libraries.
Mozilla Public License 2.0
27 stars 4 forks source link

Getting a bad request for `uploadMediaData` #176

Closed NusryNizam closed 1 week ago

NusryNizam commented 1 week ago

https://github.com/penpot/penpot-plugins/blob/51625c3e82b23d6b1981323b99aaaae6bc5dd002/libs/plugin-types/index.d.ts#L852

I'm trying to send a Uint8Array (message.data), but i'm getting a bad request. Seems like this is a bug.

penpot.uploadMediaData("example-data", message.data, "image/jpeg")
      .then(() => {
        console.log("done");
      })
      .catch((e) => {
        console.log(e);
      });

image

girafic commented 1 week ago

Everything works fine here, check your message.data, it should have correct mime type on the image.

Working example for me:

const imageData = new Uint8Array([
      137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 5,
      0, 0, 0, 5, 8, 6, 0, 0, 0, 141, 111, 38, 229, 0, 0, 0, 28, 73, 68, 65, 84,
      8, 215, 99, 248, 255, 255, 63, 195, 127, 6, 32, 5, 195, 32, 18, 132, 208,
      49, 241, 130, 88, 205, 4, 0, 14, 245, 53, 203, 209, 142, 14, 31, 0, 0, 0,
      0, 73, 69, 78, 68, 174, 66, 96, 130,
    ]);
    const img = await penpot.uploadMediaData(
      "example.png",
      imageData,
      "image/png"
    );
    penpot.selection[0].fills = [
      {
        fillImage: {
          mtype: "image/png",
          id: img.id,
          width: img.width,
          height: img.height,
          keepApectRatio: true,
          name: "example.png",
        },
      },
    ];
    console.log(img);
NusryNizam commented 1 week ago

thank you for confirming @girafic . I'll look into my issue.

Edit: A reference for a solution just in case: https://community.penpot.app/t/getting-bad-request-when-trying-to-add-an-image/7042/3