all files / modules/extensions/ multipart.js

88.89% Statements 32/36
68.18% Branches 15/22
87.5% Functions 7/8
88.89% Lines 32/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                         54× 54×     54× 54×                 11×   11×   11×   11× 11×             11×                                                
const d = require("describe-property");
const parseContent = require("../multipart/parseContent");
 
const BOUNDARY_MATCHER = /^multipart\/.*boundary=(?:"([^"]+)"|([^;]+))/im;
const NAME_MATCHER = /\bname="([^"]+)"/i;
 
module.exports = function (mach) {
    mach.Message.PARSERS["multipart/form-data"] = function (message, maxLength) {
        function partHandler(part) {
            return message.handlePart(part);
        }
 
    // If the content has been buffered, use the buffer.
        Iif (message.isBuffered) {
            return message.bufferContent().then(function (content) {
                return parseContent(content, message.multipartBoundary, maxLength, partHandler);
            });
        }
 
        return parseContent(message.content, message.multipartBoundary, maxLength, partHandler);
    };
 
    Object.defineProperties(mach.Message.prototype, {
 
    /**
     * The value that was used as the boundary for multipart content. This
     * is present only in multipart messages.
     */
        multipartBoundary: d.gs(function () {
            const contentType = this.contentType;
            Iif (!contentType) {
                return null;
            }
            const match = contentType.match(BOUNDARY_MATCHER);
            return match ? match[1] || match[2] : null;
        }),
 
    /**
     * The unique "name" or ID of this message, as given in its Content-Disposition
     * header. This is usually present only on messages that are part of a larger,
     * multipart message.
     */
        name: d.gs(function () {
            const contentDisposition = this.headers["Content-Disposition"];
            Iif (!contentDisposition) {
                return null;
            }
            const match = contentDisposition.match(NAME_MATCHER);
            return match ? match[1] : this.headers["Content-ID"];
        }),
 
    /**
     * The filename of this message, as given in its Content-Disposition header.
     * This is usually present only on messages that are part of a larger, multipart
     * message and that originate from a file upload.
     */
        filename: d.gs(function () {
            const contentDisposition = this.headers["Content-Disposition"];
 
            Eif (contentDisposition) {
        // Match quoted filenames.
                let match = contentDisposition.match(/filename="([^;]*)"/i);
 
                let filename;
                if (match) {
                    filename = decodeURIComponent(match[1].replace(/\\"/g, "\""));
                } else {
          // Match unquoted filenames.
                    match = contentDisposition.match(/filename=([^;]+)/i);
 
                    if (match) {
                        filename = decodeURIComponent(match[1]);
                    }
                }
 
                if (filename) {
          // Take the last part of the filename. This handles full Windows
          // paths given by IE (and possibly other dumb clients).
                    return filename.substr(filename.lastIndexOf("\\") + 1);
                }
            }
 
            return null;
        }),
 
    /**
     * A low-level hook responsible for handling Message objects embedded as multipart
     * objects inside this message. It should return the value to use for the given
     * message in the parameters hash. By default parts that originate from file uploads
     * are buffered and all others are converted to strings.
     *
     * This should be overridden if you want to specify some kind of custom handling
     * for multipart data, such as streaming it directly to a network file storage.
     * For example, the server extension overrides this method to save uploaded files
     * to a temporary location on disk.
     */
        handlePart: d(function (part) {
            return part.filename ? part.bufferContent() : part.stringifyContent();
        })
 
    });
};