| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:convert'; | 6 import 'dart:convert'; |
| 7 | 7 |
| 8 import 'package:async/async.dart'; |
| 8 import 'package:http_parser/http_parser.dart'; | 9 import 'package:http_parser/http_parser.dart'; |
| 9 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 10 | 11 |
| 11 import 'byte_stream.dart'; | 12 import 'byte_stream.dart'; |
| 12 import 'io.dart' as io; | 13 import 'io.dart' as io; |
| 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 80 |
| 80 // TODO(nweiz): Infer the content-type from the filename. | 81 // TODO(nweiz): Infer the content-type from the filename. |
| 81 /// Creates a new [MultipartFile] from a path to a file on disk. | 82 /// Creates a new [MultipartFile] from a path to a file on disk. |
| 82 /// | 83 /// |
| 83 /// [filename] defaults to the basename of [filePath]. [contentType] currently | 84 /// [filename] defaults to the basename of [filePath]. [contentType] currently |
| 84 /// defaults to `application/octet-stream`, but in the future may be inferred | 85 /// defaults to `application/octet-stream`, but in the future may be inferred |
| 85 /// from [filename]. | 86 /// from [filename]. |
| 86 /// | 87 /// |
| 87 /// This can only be used in an environment that supports "dart:io". | 88 /// This can only be used in an environment that supports "dart:io". |
| 88 static Future<MultipartFile> fromPath(String field, String filePath, | 89 static Future<MultipartFile> fromPath(String field, String filePath, |
| 89 {String filename, MediaType contentType}) { | 90 {String filename, MediaType contentType}) async { |
| 90 io.assertSupported("MultipartFile.fromPath"); | 91 io.assertSupported("MultipartFile.fromPath"); |
| 91 if (filename == null) filename = path.basename(filePath); | 92 if (filename == null) filename = path.basename(filePath); |
| 92 var file = io.newFile(filePath); | 93 var file = io.newFile(filePath); |
| 93 return file.length().then((length) { | 94 var length = await file.length(); |
| 94 var stream = new ByteStream(file.openRead()); | 95 var stream = new ByteStream(DelegatingStream.typed(file.openRead())); |
| 95 return new MultipartFile(field, stream, length, | 96 return new MultipartFile(field, stream, length, |
| 96 filename: filename, | 97 filename: filename, |
| 97 contentType: contentType); | 98 contentType: contentType); |
| 98 }); | |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Finalizes the file in preparation for it being sent as part of a | 101 // Finalizes the file in preparation for it being sent as part of a |
| 102 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 102 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
| 103 // of the file. The stream may be closed to indicate an empty file. | 103 // of the file. The stream may be closed to indicate an empty file. |
| 104 ByteStream finalize() { | 104 ByteStream finalize() { |
| 105 if (isFinalized) { | 105 if (isFinalized) { |
| 106 throw new StateError("Can't finalize a finalized MultipartFile."); | 106 throw new StateError("Can't finalize a finalized MultipartFile."); |
| 107 } | 107 } |
| 108 _isFinalized = true; | 108 _isFinalized = true; |
| 109 return _stream; | 109 return _stream; |
| 110 } | 110 } |
| 111 } | 111 } |
| OLD | NEW |