Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decipher / Cipher Stream with request() to API with form-data produces an error #409

Open
Ncifra opened this issue Oct 19, 2018 · 1 comment

Comments

@Ncifra
Copy link

Ncifra commented Oct 19, 2018

Hello,

We have two API's in Node.js. The first one acts as the main hub, while the other one is used to send emails.

Currently I have some files that are encrypted and stored in the file system of the first API. The files get decrypted with the following function:

decryptFile: function (filePath, callback) {
                var fileName = path.basename(filePath);
                var encryptedFilePath = path.dirname(filePath) + '/' + this.encryptText(fileName);
                // input file
                var fileDecipher = encryptionKeys.crypto.createDecipher(encryptionKeys.algorithm, encryptionKeys.password);
                var r = fs.createReadStream(encryptedFilePath);
                r.on("error", function (err) {
                    if (err) {
                        callback(err);
                    }
                });
                // return decrypted content
                return callback(null, r.pipe(fileDecipher));

        }

To send the files we use:

utils.files.decryptFile(fullPath, function (err, decryptedFile) {

requestOptions = {
            url: pushAPIURL + 'email/send',
            method: "POST",
            formData: {
               attachments: [decryptedFile]
            },
            headers: {
                authorization: global.PUSHToken
            }
        };
        return request(requestOptions);
});

Due to this we receive:

Error: Part terminated early due to unexpected end of multipart data

on the mail API that uses multer

This seems to be related to the missing knownLength of the attachment from what I have been reading, but the solutions are far and apart. Any idea on this?

EDIT: The above related to the "knownLength" may not be accurate so take it just as a suggestion.

Thanks

@Ncifra
Copy link
Author

Ncifra commented Oct 23, 2018

So the solution to this was thanks to: #356 (comment)

It seems indeed related to the missing known length of the stream + seemingly some other metadata fields that are missing. On the file decryption function, I added the counting of the stream length for each chunk:

...
            let file, length = 0;
            // decrypt content
            r.on("error", function (err) {
                if (err) {
                    callback(err);
                }
            });
            r.on("data", function (chunk) {
                // console.log("data");
                length += chunk.length;
            });
            r.on("end", function () {
                // console.log("end");
                return callback(null, file, fileName, length);
            });
            file = r.pipe(fileDecipher);

Then, on the file that is sending the request we add the following

...
utils.files.decryptFile(fullPath, function (err, decryptedFile, filename, length) {
            decryptedFile.httpVersion = "1.0";
            decryptedFile.name = filename;
            decryptedFile.headers = {'content-length': length};
            requestOptions = {
            url: pushAPIURL + 'email/send',
            method: "POST",
            formData: {
               attachments: [decryptedFile]
            },
            headers: {
                authorization: global.PUSHToken
            }
        };
        return request(requestOptions);
});
...

I am leaving this open just in case this needs to be made as part of an upcoming feature, so an admin from form-data can close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant