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 'byte_stream.dart'; | 10 import 'byte_stream.dart'; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // TODO(nweiz): Infer the content-type from the filename. | 80 // TODO(nweiz): Infer the content-type from the filename. |
81 /// Creates a new [MultipartFile] from a [File]. | 81 /// Creates a new [MultipartFile] from a [File]. |
82 /// | 82 /// |
83 /// [filename] defaults to the name of the file on disk. [contentType] | 83 /// [filename] defaults to the name of the file on disk. [contentType] |
84 /// currently defaults to `application/octet-stream`, but in the future may be | 84 /// currently defaults to `application/octet-stream`, but in the future may be |
85 /// inferred from [filename]. | 85 /// inferred from [filename]. |
86 static Future<MultipartFile> fromFile(String field, File file, | 86 static Future<MultipartFile> fromFile(String field, File file, |
87 {String filename, ContentType contentType}) { | 87 {String filename, ContentType contentType}) { |
88 if (filename == null) filename = new Path(file.name).filename; | 88 if (filename == null) filename = new Path(file.name).filename; |
89 return file.length().then((length) { | 89 return file.length().then((length) { |
90 var stream = wrapInputStream(file.openInputStream()); | 90 var stream = new ByteStream(file.openRead()); |
91 return new MultipartFile(field, stream, length, | 91 return new MultipartFile(field, stream, length, |
92 filename: filename, | 92 filename: filename, |
93 contentType: contentType); | 93 contentType: contentType); |
94 }); | 94 }); |
95 } | 95 } |
96 | 96 |
97 // 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 |
98 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 98 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
99 // 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. |
100 ByteStream finalize() { | 100 ByteStream finalize() { |
101 if (isFinalized) { | 101 if (isFinalized) { |
102 throw new StateError("Can't finalize a finalized MultipartFile."); | 102 throw new StateError("Can't finalize a finalized MultipartFile."); |
103 } | 103 } |
104 _isFinalized = true; | 104 _isFinalized = true; |
105 return _stream; | 105 return _stream; |
106 } | 106 } |
107 } | 107 } |
OLD | NEW |