| 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 '../../../../pkg/pathos/lib/path.dart' as path; | 10 import '../../../../pkg/pathos/lib/path.dart' as path; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 var charset = contentType.charset; | 73 var charset = contentType.charset; |
| 74 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); | 74 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); |
| 75 contentType.charset = encoding.name; | 75 contentType.charset = encoding.name; |
| 76 | 76 |
| 77 return new MultipartFile.fromBytes(field, encodeString(value, encoding), | 77 return new MultipartFile.fromBytes(field, encodeString(value, encoding), |
| 78 filename: filename, | 78 filename: filename, |
| 79 contentType: contentType); | 79 contentType: contentType); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // TODO(nweiz): Infer the content-type from the filename. | 82 // TODO(nweiz): Infer the content-type from the filename. |
| 83 /// Creates a new [MultipartFile] from a [File]. | 83 /// Creates a new [MultipartFile] from a path to a file on disk. |
| 84 /// | 84 /// |
| 85 /// [filename] defaults to the name of the file on disk. [contentType] | 85 /// [filename] defaults to the basename of [filePath]. [contentType] currently |
| 86 /// currently defaults to `application/octet-stream`, but in the future may be | 86 /// defaults to `application/octet-stream`, but in the future may be inferred |
| 87 /// inferred from [filename]. | 87 /// from [filename]. |
| 88 static Future<MultipartFile> fromFile(String field, File file, | 88 static Future<MultipartFile> fromPath(String field, String filePath, |
| 89 {String filename, ContentType contentType}) { | 89 {String filename, ContentType contentType}) { |
| 90 if (filename == null) filename = new Path(file.name).filename; | 90 if (filename == null) filename = path.basename(filePath); |
| 91 var file = new File(filePath); |
| 91 return file.length().then((length) { | 92 return file.length().then((length) { |
| 92 var stream = new ByteStream(file.openRead()); | 93 var stream = new ByteStream(file.openRead()); |
| 93 return new MultipartFile(field, stream, length, | 94 return new MultipartFile(field, stream, length, |
| 94 filename: filename, | 95 filename: filename, |
| 95 contentType: contentType); | 96 contentType: contentType); |
| 96 }); | 97 }); |
| 97 } | 98 } |
| 98 | 99 |
| 99 // Finalizes the file in preparation for it being sent as part of a | 100 // Finalizes the file in preparation for it being sent as part of a |
| 100 // [MultipartRequest]. This returns a [ByteStream] that should emit the body | 101 // [MultipartRequest]. This returns a [ByteStream] that should emit the body |
| 101 // of the file. The stream may be closed to indicate an empty file. | 102 // of the file. The stream may be closed to indicate an empty file. |
| 102 ByteStream finalize() { | 103 ByteStream finalize() { |
| 103 if (isFinalized) { | 104 if (isFinalized) { |
| 104 throw new StateError("Can't finalize a finalized MultipartFile."); | 105 throw new StateError("Can't finalize a finalized MultipartFile."); |
| 105 } | 106 } |
| 106 _isFinalized = true; | 107 _isFinalized = true; |
| 107 return _stream; | 108 return _stream; |
| 108 } | 109 } |
| 109 } | 110 } |
| OLD | NEW |