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