| 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:convert'; |
| 8 import 'dart:io'; | 9 import 'dart:io'; |
| 9 | 10 |
| 10 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
| 11 | 12 |
| 12 import 'byte_stream.dart'; | 13 import 'byte_stream.dart'; |
| 13 import 'utils.dart'; | 14 import 'utils.dart'; |
| 14 | 15 |
| 15 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to | 16 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to |
| 16 /// correspond to a physical file. | 17 /// correspond to a physical file. |
| 17 class MultipartFile { | 18 class MultipartFile { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 /// | 64 /// |
| 64 /// The encoding to use when translating [value] into bytes is taken from | 65 /// 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. | 66 /// [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 | 67 /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in |
| 67 /// the future may be inferred from [filename]. | 68 /// the future may be inferred from [filename]. |
| 68 factory MultipartFile.fromString(String field, String value, | 69 factory MultipartFile.fromString(String field, String value, |
| 69 {String filename, ContentType contentType}) { | 70 {String filename, ContentType contentType}) { |
| 70 contentType = contentType == null ? new ContentType("text", "plain") | 71 contentType = contentType == null ? new ContentType("text", "plain") |
| 71 : contentType; | 72 : contentType; |
| 72 var charset = contentType.charset; | 73 var charset = contentType.charset; |
| 73 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); | 74 var encoding = encodingForCharset(contentType.charset, UTF8); |
| 74 // Make a new contentType with ensured charset. | 75 // Make a new contentType with ensured charset. |
| 75 contentType = new ContentType(contentType.primaryType, | 76 contentType = new ContentType(contentType.primaryType, |
| 76 contentType.subType, | 77 contentType.subType, |
| 77 charset: encoding.name, | 78 charset: encoding.name, |
| 78 parameters: contentType.parameters); | 79 parameters: contentType.parameters); |
| 79 | 80 |
| 80 return new MultipartFile.fromBytes(field, encodeString(value, encoding), | 81 return new MultipartFile.fromBytes(field, encoding.encode(value), |
| 81 filename: filename, | 82 filename: filename, |
| 82 contentType: contentType); | 83 contentType: contentType); |
| 83 } | 84 } |
| 84 | 85 |
| 85 // TODO(nweiz): Infer the content-type from the filename. | 86 // TODO(nweiz): Infer the content-type from the filename. |
| 86 /// Creates a new [MultipartFile] from a path to a file on disk. | 87 /// Creates a new [MultipartFile] from a path to a file on disk. |
| 87 /// | 88 /// |
| 88 /// [filename] defaults to the basename of [filePath]. [contentType] currently | 89 /// [filename] defaults to the basename of [filePath]. [contentType] currently |
| 89 /// defaults to `application/octet-stream`, but in the future may be inferred | 90 /// defaults to `application/octet-stream`, but in the future may be inferred |
| 90 /// from [filename]. | 91 /// from [filename]. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 104 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 105 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
| 105 // of the file. The stream may be closed to indicate an empty file. | 106 // of the file. The stream may be closed to indicate an empty file. |
| 106 ByteStream finalize() { | 107 ByteStream finalize() { |
| 107 if (isFinalized) { | 108 if (isFinalized) { |
| 108 throw new StateError("Can't finalize a finalized MultipartFile."); | 109 throw new StateError("Can't finalize a finalized MultipartFile."); |
| 109 } | 110 } |
| 110 _isFinalized = true; | 111 _isFinalized = true; |
| 111 return _stream; | 112 return _stream; |
| 112 } | 113 } |
| 113 } | 114 } |
| OLD | NEW |