invakid404 / payload-blurhash-plugin

Payload CMS plugin for automatic Blurhash encoding of images
The Unlicense
57 stars 5 forks source link

fix: ignore local api calls #202

Closed mlabrum closed 9 months ago

mlabrum commented 9 months ago

Hello!

Thanks for creating this library.

I've been trying to upload media from the payload local API, and I've found the payload local API doesn't always set req.collection which means this plugin bails out due to req.collection not existing.

e.g

await payload.init({
    secret: PAYLOAD_SECRET,
    local: true,
});

const blob = await fetch(
    "https://www.google.com/images/branding/googlelogo/2x/googlelogo_light_color_272x92dp.png"
).then((response) => response.blob());

const arrayBuffer = await blob.arrayBuffer();
const buffer = await Buffer.from(arrayBuffer, "binary");

await payload.create({
    collection: "media",
    data: {},
    file: {
        data: buffer,
        mimetype: blob.type,
        name: "test.png",
        size: buffer.length,
    },
});

I've also updated the build script to use the NCC node api, as the build script would not run for me locally and removed the requirement to use yarn to run the build script.

Thanks!

invakid404 commented 9 months ago

@mlabrum Do you know whether there's a way to make it actually work with the local API instead of just ignoring local API calls? I'm not too familiar with the local API in general.

In any case, thank you for bringing this issue up, I'll do my own research once I get a chance, but I'll merge this for now so that local API calls at least don't fail. It would be nice if the plugin worked with the local API as well though.

Also, thanks for the build-related changes as well, they look sound at first glance. I originally made the build script shell out to ncc, as I didn't want to bother figuring the API out, but it's definitely better this way :D

invakid404 commented 9 months ago

Released in 2.0.1.

mlabrum commented 9 months ago

Hey @invakid404, this change does make it work with the local API.

I found req.collection doesnt seem to be set with the local API so that part of the hook check failed, so I changed it to only require req.collection if it is not the local api.

Maybe we don't need that if check anymore? (It doesnt seem like req.collection is used?) This change made it generate blurhashes with the above local api code.

Honestly the NCC repo readme provided most of the code, I just made it write to disk.

Thanks for the quick response!

invakid404 commented 9 months ago

@mlabrum oh! I only looked at the changes in isolation and didn't understand what the issue was. Now that I looked at it more carefully, yes, you are right, req.collection is not used anywhere anymore. It was previously necessary, as I used req.collection to derive the path to the media folder, but since the files are retrieved from the request now, the check can be dropped altogether.

I did that in this commit and released it as v2.0.2. Let me know if it still works, as I can't test it properly right now.