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:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
| 12 import 'package:stack_trace/stack_trace.dart'; |
12 | 13 |
13 import 'byte_stream.dart'; | 14 import 'byte_stream.dart'; |
14 import 'utils.dart'; | 15 import 'utils.dart'; |
15 | 16 |
16 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to | 17 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to |
17 /// correspond to a physical file. | 18 /// correspond to a physical file. |
18 class MultipartFile { | 19 class MultipartFile { |
19 /// The name of the form field for the file. | 20 /// The name of the form field for the file. |
20 final String field; | 21 final String field; |
21 | 22 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // TODO(nweiz): Infer the content-type from the filename. | 87 // TODO(nweiz): Infer the content-type from the filename. |
87 /// Creates a new [MultipartFile] from a path to a file on disk. | 88 /// Creates a new [MultipartFile] from a path to a file on disk. |
88 /// | 89 /// |
89 /// [filename] defaults to the basename of [filePath]. [contentType] currently | 90 /// [filename] defaults to the basename of [filePath]. [contentType] currently |
90 /// defaults to `application/octet-stream`, but in the future may be inferred | 91 /// defaults to `application/octet-stream`, but in the future may be inferred |
91 /// from [filename]. | 92 /// from [filename]. |
92 static Future<MultipartFile> fromPath(String field, String filePath, | 93 static Future<MultipartFile> fromPath(String field, String filePath, |
93 {String filename, ContentType contentType}) { | 94 {String filename, ContentType contentType}) { |
94 if (filename == null) filename = path.basename(filePath); | 95 if (filename == null) filename = path.basename(filePath); |
95 var file = new File(filePath); | 96 var file = new File(filePath); |
96 return file.length().then((length) { | 97 return Chain.track(file.length()).then((length) { |
97 var stream = new ByteStream(file.openRead()); | 98 var stream = new ByteStream(Chain.track(file.openRead())); |
98 return new MultipartFile(field, stream, length, | 99 return new MultipartFile(field, stream, length, |
99 filename: filename, | 100 filename: filename, |
100 contentType: contentType); | 101 contentType: contentType); |
101 }); | 102 }); |
102 } | 103 } |
103 | 104 |
104 // Finalizes the file in preparation for it being sent as part of a | 105 // Finalizes the file in preparation for it being sent as part of a |
105 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 106 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
106 // of the file. The stream may be closed to indicate an empty file. | 107 // of the file. The stream may be closed to indicate an empty file. |
107 ByteStream finalize() { | 108 ByteStream finalize() { |
108 if (isFinalized) { | 109 if (isFinalized) { |
109 throw new StateError("Can't finalize a finalized MultipartFile."); | 110 throw new StateError("Can't finalize a finalized MultipartFile."); |
110 } | 111 } |
111 _isFinalized = true; | 112 _isFinalized = true; |
112 return _stream; | 113 return _stream; |
113 } | 114 } |
114 } | 115 } |
OLD | NEW |