Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: pkg/http/lib/src/multipart_file.dart

Issue 12351006: Change MultipartFile.fromFile to MultipartFile.fromPath in pkg/http. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/http/lib/src/utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 'package:pathos/path.dart' as path;
11
10 import 'byte_stream.dart'; 12 import 'byte_stream.dart';
11 import 'utils.dart'; 13 import 'utils.dart';
12 14
13 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to 15 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to
14 /// correspond to a physical file. 16 /// correspond to a physical file.
15 class MultipartFile { 17 class MultipartFile {
16 /// The name of the form field for the file. 18 /// The name of the form field for the file.
17 final String field; 19 final String field;
18 20
19 /// The size of the file in bytes. This must be known in advance, even if this 21 /// The size of the file in bytes. This must be known in advance, even if this
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 var charset = contentType.charset; 73 var charset = contentType.charset;
72 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8); 74 var encoding = encodingForCharset(contentType.charset, Encoding.UTF_8);
73 contentType.charset = encoding.name; 75 contentType.charset = encoding.name;
74 76
75 return new MultipartFile.fromBytes(field, encodeString(value, encoding), 77 return new MultipartFile.fromBytes(field, encodeString(value, encoding),
76 filename: filename, 78 filename: filename,
77 contentType: contentType); 79 contentType: contentType);
78 } 80 }
79 81
80 // TODO(nweiz): Infer the content-type from the filename. 82 // TODO(nweiz): Infer the content-type from the filename.
81 /// Creates a new [MultipartFile] from a [File]. 83 /// Creates a new [MultipartFile] from a path to a file on disk.
82 /// 84 ///
83 /// [filename] defaults to the name of the file on disk. [contentType] 85 /// [filename] defaults to the basename of [filePath]. [contentType] currently
84 /// 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
85 /// inferred from [filename]. 87 /// from [filename].
86 static Future<MultipartFile> fromFile(String field, File file, 88 static Future<MultipartFile> fromPath(String field, String filePath,
87 {String filename, ContentType contentType}) { 89 {String filename, ContentType contentType}) {
88 if (filename == null) filename = new Path(file.name).filename; 90 if (filename == null) filename = path.basename(filePath);
91 var file = new File(filePath);
89 return file.length().then((length) { 92 return file.length().then((length) {
90 var stream = new ByteStream(file.openRead()); 93 var stream = new ByteStream(file.openRead());
91 return new MultipartFile(field, stream, length, 94 return new MultipartFile(field, stream, length,
92 filename: filename, 95 filename: filename,
93 contentType: contentType); 96 contentType: contentType);
94 }); 97 });
95 } 98 }
96 99
97 // 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
98 // [MultipartRequest]. This returns a [ByteStream] that should emit the body 101 // [MultipartRequest]. This returns a [ByteStream] that should emit the body
99 // 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.
100 ByteStream finalize() { 103 ByteStream finalize() {
101 if (isFinalized) { 104 if (isFinalized) {
102 throw new StateError("Can't finalize a finalized MultipartFile."); 105 throw new StateError("Can't finalize a finalized MultipartFile.");
103 } 106 }
104 _isFinalized = true; 107 _isFinalized = true;
105 return _stream; 108 return _stream;
106 } 109 }
107 } 110 }
OLDNEW
« no previous file with comments | « no previous file | pkg/http/lib/src/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698