'node-postgres doesn't download bytea to file

I'm trying to download bytea data to file. In node-postgres result it represented as buffer.

node-postgres result in debugger

This buffer.toString() shows us the following:

{"size":617543,"filepath":"C:\Users\bykov\AppData\Local\Temp\840a5003b0404d37b3acfc500","newFilename":"840a5003b0404d37b3acfc500","mimetype":"image/jpeg","mtime":"2022-02-20T19:59:42.893Z","originalFilename":"4-6mBf8fjJE.jpg"}

But when I'm trying to send it to response object, it turns there is no such file. And I don't understand, it's some postgres settings, FS access permissions or some issues in my code? Function is here:

router.get(`/image`, async (req, res) => {
    executeWithValidation(req, res, (id, locale) => controller.readImage(id, locale)
        .then(resultSet => {
            function parseEntry(entry) {
                // remove opening quotes of key
                const unquotedFirst = entry.substr(1);
                // extract all symbols before separator : and remove closing quotes
                const key = unquotedFirst.substr(0, unquotedFirst.indexOf(`"`) + 1).replaceAll(`"`, ``);
                // value is rest of entry
                const value = entry.substr(key.length + 3).replaceAll(`"`, ``);
                return [key, value];
            }
            const string = resultSet.rows[0][`image`].toString();
            const content = {};
            const array = string.replaceAll(`{`, ``).replaceAll(`}`, ``).split(`,`);
            for (const entry of array) {
                // JSON.parse() fails parsing some entries
                const arr = parseEntry(entry);
                content[arr[0]] = arr[1];
            }

            res.download(content.filepath, content.originalFilename, err => {
                if (err) {
                    logger.error(err);
                    badRequest(res, err);
                } else {
                    fs.unlink(content.filepath, err => {
                        if (err) {
                            logger.error(err);
                        }
                    });
                }
            })

        })
        .catch(err => internalServerError(res, err)));
});

P.S. I'm a noob in JS and may miss some problems in my code.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source