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