| 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 'utils.dart'; | 11 import 'utils.dart'; |
| 11 | 12 |
| 12 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to | 13 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to |
| 13 /// correspond to a physical file. | 14 /// correspond to a physical file. |
| 14 class MultipartFile { | 15 class MultipartFile { |
| 15 /// The name of the form field for the file. | 16 /// The name of the form field for the file. |
| 16 final String field; | 17 final String field; |
| 17 | 18 |
| 18 /// The size of the file in bytes. This must be known in advance, even if this | 19 /// The size of the file in bytes. This must be known in advance, even if this |
| 19 /// file is created from an [InputStream]. | 20 /// file is created from a [ByteStream]. |
| 20 final int length; | 21 final int length; |
| 21 | 22 |
| 22 /// The basename of the file. May be null. | 23 /// The basename of the file. May be null. |
| 23 final String filename; | 24 final String filename; |
| 24 | 25 |
| 25 /// The content-type of the file. Defaults to `application/octet-stream`. | 26 /// The content-type of the file. Defaults to `application/octet-stream`. |
| 26 final ContentType contentType; | 27 final ContentType contentType; |
| 27 | 28 |
| 28 /// The stream that will emit the file's contents. | 29 /// The stream that will emit the file's contents. |
| 29 final InputStream _stream; | 30 final ByteStream _stream; |
| 30 | 31 |
| 31 /// Whether [finalize] has been called. | 32 /// Whether [finalize] has been called. |
| 32 bool get isFinalized => _isFinalized; | 33 bool get isFinalized => _isFinalized; |
| 33 bool _isFinalized = false; | 34 bool _isFinalized = false; |
| 34 | 35 |
| 35 /// Creates a new [MultipartFile] from an [InputStream]. The length of the | 36 /// Creates a new [MultipartFile] from a chunked [Stream] of bytes. The length |
| 36 /// file in bytes must be known in advance. If it's not, read the data from | 37 /// of the file in bytes must be known in advance. If it's not, read the data |
| 37 /// the stream and use [MultipartFile.fromBytes] instead. | 38 /// from the stream and use [MultipartFile.fromBytes] instead. |
| 38 /// | 39 /// |
| 39 /// [contentType] currently defaults to `application/octet-stream`, but in the | 40 /// [contentType] currently defaults to `application/octet-stream`, but in the |
| 40 /// future may be inferred from [filename]. | 41 /// future may be inferred from [filename]. |
| 41 MultipartFile(this.field, this._stream, this.length, | 42 MultipartFile(this.field, Stream<List<int>> stream, this.length, |
| 42 {this.filename, ContentType contentType}) | 43 {this.filename, ContentType contentType}) |
| 43 : this.contentType = contentType != null ? contentType : | 44 : this._stream = toByteStream(stream), |
| 45 this.contentType = contentType != null ? contentType : |
| 44 new ContentType("application", "octet-stream"); | 46 new ContentType("application", "octet-stream"); |
| 45 | 47 |
| 46 /// Creates a new [MultipartFile] from a byte array. | 48 /// Creates a new [MultipartFile] from a byte array. |
| 47 /// | 49 /// |
| 48 /// [contentType] currently defaults to `application/octet-stream`, but in the | 50 /// [contentType] currently defaults to `application/octet-stream`, but in the |
| 49 /// future may be inferred from [filename]. | 51 /// future may be inferred from [filename]. |
| 50 factory MultipartFile.fromBytes(String field, List<int> value, | 52 factory MultipartFile.fromBytes(String field, List<int> value, |
| 51 {String filename, ContentType contentType}) { | 53 {String filename, ContentType contentType}) { |
| 52 var stream = new ListInputStream(); | 54 var stream = new ByteStream.fromBytes(value); |
| 53 stream.write(value); | |
| 54 stream.markEndOfStream(); | |
| 55 return new MultipartFile(field, stream, value.length, | 55 return new MultipartFile(field, stream, value.length, |
| 56 filename: filename, | 56 filename: filename, |
| 57 contentType: contentType); | 57 contentType: contentType); |
| 58 } | 58 } |
| 59 | 59 |
| 60 /// Creates a new [MultipartFile] from a string. | 60 /// Creates a new [MultipartFile] from a string. |
| 61 /// | 61 /// |
| 62 /// The encoding to use when translating [value] into bytes is taken from | 62 /// The encoding to use when translating [value] into bytes is taken from |
| 63 /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. | 63 /// [contentType] if it has a charset set. Otherwise, it defaults to UTF-8. |
| 64 /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in | 64 /// [contentType] currently defaults to `text/plain; charset=utf-8`, but in |
| (...skipping 15 matching lines...) Expand all 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 return new MultipartFile(field, file.openInputStream(), length, | 90 var stream = wrapInputStream(file.openInputStream()); |
| 91 return new MultipartFile(field, stream, length, |
| 91 filename: filename, | 92 filename: filename, |
| 92 contentType: contentType); | 93 contentType: contentType); |
| 93 }); | 94 }); |
| 94 } | 95 } |
| 95 | 96 |
| 96 // 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 |
| 97 // [MultipartRequest]. This returns an [InputStream] that should emit the body | 98 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
| 98 // 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. |
| 99 InputStream finalize() { | 100 ByteStream finalize() { |
| 100 if (isFinalized) { | 101 if (isFinalized) { |
| 101 throw new StateError("Can't finalize a finalized MultipartFile."); | 102 throw new StateError("Can't finalize a finalized MultipartFile."); |
| 102 } | 103 } |
| 103 _isFinalized = true; | 104 _isFinalized = true; |
| 104 return _stream; | 105 return _stream; |
| 105 } | 106 } |
| 106 } | 107 } |
| OLD | NEW |