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

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

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

Powered by Google App Engine
This is Rietveld 408576698