Error when trying to save new file in afterSaveFile

Hi, I am trying to create a thumbnail of an uploaded image using cloud code (from https://community.parseplatform.org/t/image-thumbnails/160/2).

The code below works fine until the last line thumbfile.save(null, { useMasterKey: true }) for which I get the error ParseError: unauthorized at handleError (/home/parse/node_modules/parse/lib/node/RESTController.js:422:17).
Why does it give this error even when I use the master key?

And if I try to set an ACL to thumbfile I get the error TypeError: node.setACL is not a function

Any help on how to fix this or what the problem is? :slight_smile: Thanks

In Parse.Cloud.afterSaveFile:

    const fileData = await file.getData();
    const fileBuffer = Buffer.from(fileData, 'base64');
    const mimeInfo = await fileType.fromBuffer(fileBuffer);

    if (mimeInfo.mime.startsWith("image")) {
        const options = { percentage: 5, responseType: 'base64' };
        const thumbnail = await imageThumbnail(fileBuffer, options);

        const thumbfile = new Parse.File("thumb_" + fileName, { base64: thumbnail });
        //setDenyWriteACL(thumbfile);
        await thumbfile.save(null, { useMasterKey: true });
    }

Can you try to replace await thumbfile.save(null, { useMasterKey: true }); to await thumbfile.save({ useMasterKey: true });?

you are creating a Parse.File (and not a Parse.Object). And options is the first argument: Parse-SDK-JS/ParseFile.js at master · parse-community/Parse-SDK-JS · GitHub

1 Like

Thanks for the help, I moved everything into Job and changed what you wrote:

Parse.Cloud.job("createThumb", async (request) => {
    const { params, headers, log, message } = request;
    message("I just started");

    const file = params.f;
    const fileName = file.name();
    const fileData = await file.getData();

    const fileBuffer = Buffer.from(fileData, 'base64');
    const mimeInfo = await fileType.fromBuffer(fileBuffer);

    if (mimeInfo.mime.startsWith("image")) {
        const options = { percentage: 5, responseType: 'base64' };
        const thumbnail = await imageThumbnail(fileBuffer, options);
        const thumbfile = new Parse.File("thumb_" + fileName, { base64: thumbnail });
        try {
            await thumbfile.save({ useMasterKey: true });
        } catch (err) {
            console.log("Failed to save thumb; " + err);
            return false;
        }
    }
    message("I just finished");
    return true;
});

However it gets an error when saving the file: Failed to save thumb; ParseError: 130 Unauthorised!

Nevermind, it was my own mistake ;D

I hade a beforeSaveFile that checked:

if (user != null) {
        ...
} else {
   throw "Unauthorised!";
}

And of course the user is null when the master key is used. Changed it to:

if (user != null || master)

and it works :slight_smile: