« index

Coverage for /Users/yunong/workspace/node-restify/lib/plugins/multipart_parser.js : 82%

101 lines | 83 run | 18 missing | 3 partial | 20 blocks | 9 blocks run | 11 blocks missing

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

  // Copyright 2012 Mark Cavage, Inc.  All rights reserved.
  
  var assert = require('assert-plus');
  var formidable = require('formidable');
  
  var errors = require('../errors');
  
  
  ///--- Globals
  
  var BadRequestError = errors.BadRequestError;
  
  
  ///--- API
  
  /**
   * Returns a plugin that will parse the HTTP request body IFF the
   * contentType is multipart/form-data
   *
   * If req.params already contains a given key, that key is skipped and an
   * error is logged.
   *
   * @return {Function} restify handler.
   * @throws {TypeError} on bad input
   */
  function multipartBodyParser(options) {
      if (!options)
          options = {};
      assert.object(options, 'options');
  
      var override = options.overrideParams;
  
      function parseMultipartBody(req, res, next) {
          if (req.getContentType() !== 'multipart/form-data' ||
              (req.getContentLength() === 0 && !req.isChunked())req.isChunked()))
              return (next());
  
          var form = new formidable.IncomingForm();
          // enable multiple files on a single upload field
          // (html5 multiple attribute)
          form.multiples = options.multiples || false;
          form.keepExtensions = options.keepExtensions ? true : false;
          if (options.uploadDir)
              form.uploadDir = options.uploadDir;
          if (options.maxFieldsSize)
              form.maxFieldsSize = options.maxFieldsSize;
  
          form.onPart = function onPart(part) {
              if (part.filename && options.multipartFileHandler)
                  options.multipartFileHandler(part, req);
              else if (!part.filename && options.multipartHandler)
                  options.multipartHandler(part, req);
              else
                  form.handlePart(part);
          };
  
          form.parse(req, function (err, fields, files) {
              if (err)
                  return (next(new BadRequestError(err.message)));
  
              req.body = fields;
              req.files = files;
  
              if (options.mapParams !== false) {
                  Object.keys(fields).forEach(function (k) {
                      if (req.params[k] && !override)override)
                          return (false);
  
                      req.params[k] = fields[k];
                      return (true);
                  });
  
                  if (options.mapFiles) {
                      Object.keys(files).forEach(function (f) {
                          if (req.params[f] && !override)
                              return (false);
                          var fs = require('fs');
                          return fs.readFile(
                              files[f].path,
                              'utf8',
                              function (ex, data) {
                                  if (ex) {
                                      return (false);
                                  }
                                  req.params[f] = data;
                                  return (true);
                              });
                      });
                  }
              }
  
              return (next());
          });
  
          return (false);
      }
  
      return (parseMultipartBody);
  }
  
  module.exports = multipartBodyParser;
« index | cover.io