OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library multipart_file; | 5 library multipart_file; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import '../../../../pkg/pathos/lib/path.dart' as path; | 10 import '../../../../pkg/pathos/lib/path.dart' as path; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 60 } |
61 | 61 |
62 /// Creates a new [MultipartFile] from a string. | 62 /// Creates a new [MultipartFile] from a string. |
63 /// | 63 /// |
64 /// The encoding to use when translating [value] into bytes is taken from | 64 /// The encoding to use when translating [value] into bytes is taken from |
65 /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. | 65 /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. |
66 /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in | 66 /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in |
67 /// the future may be inferred from [filename]. | 67 /// the future may be inferred from [filename]. |
68 factory MultipartFile.fromString(String field, String value, | 68 factory MultipartFile.fromString(String field, String value, |
69 {String filename, ContentType contentType}) { | 69 {String filename, ContentType contentType}) { |
70 contentType = contentType == null ? new ContentType("text", "plain") : | 70 contentType = contentType == null ? new ContentType("text", "plain") |
71 // Make a copy of the original contentType so we can modify charset. | 71 : contentType; |
72 new ContentType.fromString(contentType.toString()); | |
73 var charset = contentType.charset; | 72 var charset = contentType.charset; |
74 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); | 73 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); |
75 contentType.charset = encoding.name; | 74 // Make a new contentType with ensured charset. |
| 75 contentType = new ContentType(contentType.primaryType, |
| 76 contentType.subType, |
| 77 charset: encoding.name, |
| 78 parameters: contentType.parameters); |
76 | 79 |
77 return new MultipartFile.fromBytes(field, encodeString(value, encoding), | 80 return new MultipartFile.fromBytes(field, encodeString(value, encoding), |
78 filename: filename, | 81 filename: filename, |
79 contentType: contentType); | 82 contentType: contentType); |
80 } | 83 } |
81 | 84 |
82 // TODO(nweiz): Infer the content-type from the filename. | 85 // TODO(nweiz): Infer the content-type from the filename. |
83 /// Creates a new [MultipartFile] from a path to a file on disk. | 86 /// Creates a new [MultipartFile] from a path to a file on disk. |
84 /// | 87 /// |
85 /// [filename] defaults to the basename of [filePath]. [contentType] currently | 88 /// [filename] defaults to the basename of [filePath]. [contentType] currently |
(...skipping 15 matching lines...) Expand all Loading... |
101 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 104 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
102 // of the file. The stream may be closed to indicate an empty file. | 105 // of the file. The stream may be closed to indicate an empty file. |
103 ByteStream finalize() { | 106 ByteStream finalize() { |
104 if (isFinalized) { | 107 if (isFinalized) { |
105 throw new StateError("Can't finalize a finalized MultipartFile."); | 108 throw new StateError("Can't finalize a finalized MultipartFile."); |
106 } | 109 } |
107 _isFinalized = true; | 110 _isFinalized = true; |
108 return _stream; | 111 return _stream; |
109 } | 112 } |
110 } | 113 } |
OLD | NEW |