| 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 'package:pathos/path.dart' as path; | |
| 11 | |
| 12 import 'byte_stream.dart'; | 10 import 'byte_stream.dart'; |
| 13 import 'utils.dart'; | 11 import 'utils.dart'; |
| 14 | 12 |
| 15 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to | 13 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to |
| 16 /// correspond to a physical file. | 14 /// correspond to a physical file. |
| 17 class MultipartFile { | 15 class MultipartFile { |
| 18 /// The name of the form field for the file. | 16 /// The name of the form field for the file. |
| 19 final String field; | 17 final String field; |
| 20 | 18 |
| 21 /// The size of the file in bytes. This must be known in advance, even if this | 19 /// The size of the file in bytes. This must be known in advance, even if this |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 var charset = contentType.charset; | 71 var charset = contentType.charset; |
| 74 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); | 72 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); |
| 75 contentType.charset = encoding.name; | 73 contentType.charset = encoding.name; |
| 76 | 74 |
| 77 return new MultipartFile.fromBytes(field, encodeString(value, encoding), | 75 return new MultipartFile.fromBytes(field, encodeString(value, encoding), |
| 78 filename: filename, | 76 filename: filename, |
| 79 contentType: contentType); | 77 contentType: contentType); |
| 80 } | 78 } |
| 81 | 79 |
| 82 // TODO(nweiz): Infer the content-type from the filename. | 80 // TODO(nweiz): Infer the content-type from the filename. |
| 83 /// Creates a new [MultipartFile] from a path to a file on disk. | 81 /// Creates a new [MultipartFile] from a [File]. |
| 84 /// | 82 /// |
| 85 /// [filename] defaults to the basename of [filePath]. [contentType] currently | 83 /// [filename] defaults to the name of the file on disk. [contentType] |
| 86 /// defaults to `application/octet-stream`, but in the future may be inferred | 84 /// currently defaults to `application/octet-stream`, but in the future may be |
| 87 /// from [filename]. | 85 /// inferred from [filename]. |
| 88 static Future<MultipartFile> fromPath(String field, String filePath, | 86 static Future<MultipartFile> fromFile(String field, File file, |
| 89 {String filename, ContentType contentType}) { | 87 {String filename, ContentType contentType}) { |
| 90 if (filename == null) filename = path.basename(filePath); | 88 if (filename == null) filename = new Path(file.name).filename; |
| 91 var file = new File(filePath); | |
| 92 return file.length().then((length) { | 89 return file.length().then((length) { |
| 93 var stream = new ByteStream(file.openRead()); | 90 var stream = new ByteStream(file.openRead()); |
| 94 return new MultipartFile(field, stream, length, | 91 return new MultipartFile(field, stream, length, |
| 95 filename: filename, | 92 filename: filename, |
| 96 contentType: contentType); | 93 contentType: contentType); |
| 97 }); | 94 }); |
| 98 } | 95 } |
| 99 | 96 |
| 100 // Finalizes the file in preparation for it being sent as part of a | 97 // Finalizes the file in preparation for it being sent as part of a |
| 101 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 98 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
| 102 // of the file. The stream may be closed to indicate an empty file. | 99 // of the file. The stream may be closed to indicate an empty file. |
| 103 ByteStream finalize() { | 100 ByteStream finalize() { |
| 104 if (isFinalized) { | 101 if (isFinalized) { |
| 105 throw new StateError("Can't finalize a finalized MultipartFile."); | 102 throw new StateError("Can't finalize a finalized MultipartFile."); |
| 106 } | 103 } |
| 107 _isFinalized = true; | 104 _isFinalized = true; |
| 108 return _stream; | 105 return _stream; |
| 109 } | 106 } |
| 110 } | 107 } |
| OLD | NEW |