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