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:convert'; | 9 import 'dart:convert'; |
9 import 'dart:io'; | |
10 | 10 |
| 11 import 'package:http_parser/http_parser.dart'; |
11 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
12 import 'package:stack_trace/stack_trace.dart'; | 13 import 'package:stack_trace/stack_trace.dart'; |
13 | 14 |
14 import 'byte_stream.dart'; | 15 import 'byte_stream.dart'; |
15 import 'utils.dart'; | 16 import 'utils.dart'; |
16 | 17 |
17 /// 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 |
18 /// correspond to a physical file. | 19 /// correspond to a physical file. |
19 class MultipartFile { | 20 class MultipartFile { |
20 /// The name of the form field for the file. | 21 /// The name of the form field for the file. |
21 final String field; | 22 final String field; |
22 | 23 |
23 /// 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 |
24 /// file is created from a [ByteStream]. | 25 /// file is created from a [ByteStream]. |
25 final int length; | 26 final int length; |
26 | 27 |
27 /// The basename of the file. May be null. | 28 /// The basename of the file. May be null. |
28 final String filename; | 29 final String filename; |
29 | 30 |
30 /// The content-type of the file. Defaults to `application/octet-stream`. | 31 /// The content-type of the file. Defaults to `application/octet-stream`. |
31 final ContentType contentType; | 32 final MediaType contentType; |
32 | 33 |
33 /// The stream that will emit the file's contents. | 34 /// The stream that will emit the file's contents. |
34 final ByteStream _stream; | 35 final ByteStream _stream; |
35 | 36 |
36 /// Whether [finalize] has been called. | 37 /// Whether [finalize] has been called. |
37 bool get isFinalized => _isFinalized; | 38 bool get isFinalized => _isFinalized; |
38 bool _isFinalized = false; | 39 bool _isFinalized = false; |
39 | 40 |
40 /// Creates a new [MultipartFile] from a chunked [Stream] of bytes. The length | 41 /// Creates a new [MultipartFile] from a chunked [Stream] of bytes. The length |
41 /// of the file in bytes must be known in advance. If it's not, read the data | 42 /// of the file in bytes must be known in advance. If it's not, read the data |
42 /// from the stream and use [MultipartFile.fromBytes] instead. | 43 /// from the stream and use [MultipartFile.fromBytes] instead. |
43 /// | 44 /// |
44 /// [contentType] currently defaults to `application/octet-stream`, but in the | 45 /// [contentType] currently defaults to `application/octet-stream`, but in the |
45 /// future may be inferred from [filename]. | 46 /// future may be inferred from [filename]. |
46 MultipartFile(this.field, Stream<List<int>> stream, this.length, | 47 MultipartFile(this.field, Stream<List<int>> stream, this.length, |
47 {this.filename, ContentType contentType}) | 48 {this.filename, MediaType contentType}) |
48 : this._stream = toByteStream(stream), | 49 : this._stream = toByteStream(stream), |
49 this.contentType = contentType != null ? contentType : | 50 this.contentType = contentType != null ? contentType : |
50 new ContentType("application", "octet-stream"); | 51 new MediaType("application", "octet-stream"); |
51 | 52 |
52 /// Creates a new [MultipartFile] from a byte array. | 53 /// Creates a new [MultipartFile] from a byte array. |
53 /// | 54 /// |
54 /// [contentType] currently defaults to `application/octet-stream`, but in the | 55 /// [contentType] currently defaults to `application/octet-stream`, but in the |
55 /// future may be inferred from [filename]. | 56 /// future may be inferred from [filename]. |
56 factory MultipartFile.fromBytes(String field, List<int> value, | 57 factory MultipartFile.fromBytes(String field, List<int> value, |
57 {String filename, ContentType contentType}) { | 58 {String filename, MediaType contentType}) { |
58 var stream = new ByteStream.fromBytes(value); | 59 var stream = new ByteStream.fromBytes(value); |
59 return new MultipartFile(field, stream, value.length, | 60 return new MultipartFile(field, stream, value.length, |
60 filename: filename, | 61 filename: filename, |
61 contentType: contentType); | 62 contentType: contentType); |
62 } | 63 } |
63 | 64 |
64 /// Creates a new [MultipartFile] from a string. | 65 /// Creates a new [MultipartFile] from a string. |
65 /// | 66 /// |
66 /// The encoding to use when translating [value] into bytes is taken from | 67 /// The encoding to use when translating [value] into bytes is taken from |
67 /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. | 68 /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. |
68 /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in | 69 /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in |
69 /// the future may be inferred from [filename]. | 70 /// the future may be inferred from [filename]. |
70 factory MultipartFile.fromString(String field, String value, | 71 factory MultipartFile.fromString(String field, String value, |
71 {String filename, ContentType contentType}) { | 72 {String filename, MediaType contentType}) { |
72 contentType = contentType == null ? new ContentType("text", "plain") | 73 contentType = contentType == null ? new MediaType("text", "plain") |
73 : contentType; | 74 : contentType; |
74 var charset = contentType.charset; | 75 var encoding = encodingForCharset(contentType.parameters['charset'], UTF8); |
75 var encoding = encodingForCharset(contentType.charset, UTF8); | 76 contentType = contentType.change(parameters: {'charset': encoding.name}); |
76 // Make a new contentType with ensured charset. | |
77 contentType = new ContentType(contentType.primaryType, | |
78 contentType.subType, | |
79 charset: encoding.name, | |
80 parameters: contentType.parameters); | |
81 | 77 |
82 return new MultipartFile.fromBytes(field, encoding.encode(value), | 78 return new MultipartFile.fromBytes(field, encoding.encode(value), |
83 filename: filename, | 79 filename: filename, |
84 contentType: contentType); | 80 contentType: contentType); |
85 } | 81 } |
86 | 82 |
87 // TODO(nweiz): Infer the content-type from the filename. | 83 // TODO(nweiz): Infer the content-type from the filename. |
88 /// 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. |
89 /// | 85 /// |
90 /// [filename] defaults to the basename of [filePath]. [contentType] currently | 86 /// [filename] defaults to the basename of [filePath]. [contentType] currently |
91 /// 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 |
92 /// from [filename]. | 88 /// from [filename]. |
93 static Future<MultipartFile> fromPath(String field, String filePath, | 89 static Future<MultipartFile> fromPath(String field, String filePath, |
94 {String filename, ContentType contentType}) { | 90 {String filename, MediaType contentType}) { |
95 if (filename == null) filename = path.basename(filePath); | 91 if (filename == null) filename = path.basename(filePath); |
96 var file = new File(filePath); | 92 var file = new File(filePath); |
97 return Chain.track(file.length()).then((length) { | 93 return Chain.track(file.length()).then((length) { |
98 var stream = new ByteStream(Chain.track(file.openRead())); | 94 var stream = new ByteStream(Chain.track(file.openRead())); |
99 return new MultipartFile(field, stream, length, | 95 return new MultipartFile(field, stream, length, |
100 filename: filename, | 96 filename: filename, |
101 contentType: contentType); | 97 contentType: contentType); |
102 }); | 98 }); |
103 } | 99 } |
104 | 100 |
105 // 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 |
106 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 102 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
107 // 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. |
108 ByteStream finalize() { | 104 ByteStream finalize() { |
109 if (isFinalized) { | 105 if (isFinalized) { |
110 throw new StateError("Can't finalize a finalized MultipartFile."); | 106 throw new StateError("Can't finalize a finalized MultipartFile."); |
111 } | 107 } |
112 _isFinalized = true; | 108 _isFinalized = true; |
113 return _stream; | 109 return _stream; |
114 } | 110 } |
115 } | 111 } |
OLD | NEW |