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 encoding.name); | |
nweiz
2013/03/05 20:43:18
This needs to copy all the parameters from the ori
Søren Gjesse
2013/03/06 13:23:30
Done.
| |
76 | 78 |
77 return new MultipartFile.fromBytes(field, encodeString(value, encoding), | 79 return new MultipartFile.fromBytes(field, encodeString(value, encoding), |
78 filename: filename, | 80 filename: filename, |
79 contentType: contentType); | 81 contentType: contentType); |
80 } | 82 } |
81 | 83 |
82 // TODO(nweiz): Infer the content-type from the filename. | 84 // TODO(nweiz): Infer the content-type from the filename. |
83 /// Creates a new [MultipartFile] from a path to a file on disk. | 85 /// Creates a new [MultipartFile] from a path to a file on disk. |
84 /// | 86 /// |
85 /// [filename] defaults to the basename of [filePath]. [contentType] currently | 87 /// [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 | 103 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
102 // of the file. The stream may be closed to indicate an empty file. | 104 // of the file. The stream may be closed to indicate an empty file. |
103 ByteStream finalize() { | 105 ByteStream finalize() { |
104 if (isFinalized) { | 106 if (isFinalized) { |
105 throw new StateError("Can't finalize a finalized MultipartFile."); | 107 throw new StateError("Can't finalize a finalized MultipartFile."); |
106 } | 108 } |
107 _isFinalized = true; | 109 _isFinalized = true; |
108 return _stream; | 110 return _stream; |
109 } | 111 } |
110 } | 112 } |
OLD | NEW |