Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export MediaType from http_parser #2205

Merged
merged 9 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ See the [Migration Guide][] for the complete breaking changes list.**

- Raise the min Dart SDK version to 2.18.0.
- Add constructor for `DioExceptionType.badCertificate`.
- Create type alias `DioMediaType` for `http_parser`'s `MediaType`.

## 5.4.3+1

Expand Down
17 changes: 10 additions & 7 deletions dio/lib/src/multipart_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import 'multipart_file/io_multipart_file.dart'
if (dart.library.html) 'multipart_file/browser_multipart_file.dart';
import 'utils.dart';

/// The type (alias) for specifying the content-type of the `MultipartFile`.
typedef DioMediaType = MediaType;
Reprevise marked this conversation as resolved.
Show resolved Hide resolved

/// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to
/// correspond to a physical file.
///
Expand All @@ -27,7 +30,7 @@ class MultipartFile {
Stream<List<int>> stream,
this.length, {
this.filename,
MediaType? contentType,
DioMediaType? contentType,
Map<String, List<String>>? headers,
}) : _data = (() => stream),
headers = caseInsensitiveKeyMap(headers),
Expand All @@ -43,7 +46,7 @@ class MultipartFile {
Stream<List<int>> Function() data,
this.length, {
this.filename,
MediaType? contentType,
DioMediaType? contentType,
Map<String, List<String>>? headers,
}) : _data = data,
headers = caseInsensitiveKeyMap(headers),
Expand All @@ -56,7 +59,7 @@ class MultipartFile {
factory MultipartFile.fromBytes(
List<int> value, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) {
return MultipartFile.fromStream(
Expand All @@ -77,7 +80,7 @@ class MultipartFile {
factory MultipartFile.fromString(
String value, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) {
contentType ??= MediaType('text', 'plain');
Expand Down Expand Up @@ -106,7 +109,7 @@ class MultipartFile {
final Map<String, List<String>>? headers;

/// The content-type of the file. Defaults to `application/octet-stream`.
final MediaType? contentType;
final DioMediaType? contentType;

/// The stream builder that will emit the file's contents for every call.
final Stream<List<int>> Function() _data;
Expand All @@ -125,7 +128,7 @@ class MultipartFile {
static Future<MultipartFile> fromFile(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) =>
multipartFileFromPath(
Expand All @@ -138,7 +141,7 @@ class MultipartFile {
static MultipartFile fromFileSync(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) =>
multipartFileFromPathSync(
Expand Down
5 changes: 2 additions & 3 deletions dio/lib/src/multipart_file/io_multipart_file.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'dart:async';
import 'dart:io';

import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart' as p;

import '../multipart_file.dart';

Future<MultipartFile> multipartFileFromPath(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) async {
filename ??= p.basename(filePath);
Expand All @@ -27,7 +26,7 @@ Future<MultipartFile> multipartFileFromPath(
MultipartFile multipartFileFromPathSync(
String filePath, {
String? filename,
MediaType? contentType,
DioMediaType? contentType,
final Map<String, List<String>>? headers,
}) {
filename ??= p.basename(filePath);
Expand Down
7 changes: 3 additions & 4 deletions dio/test/formdata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';

import 'mock/adapters.dart';
Expand Down Expand Up @@ -36,7 +35,7 @@ void main() async {
headers: {
'test': <String>['c'],
},
contentType: MediaType.parse('text/plain'),
contentType: DioMediaType.parse('text/plain'),
),
],
});
Expand Down Expand Up @@ -88,7 +87,7 @@ void main() async {
headers: {
'test': <String>['c'],
},
contentType: MediaType.parse('text/plain'),
contentType: DioMediaType.parse('text/plain'),
),
),
);
Expand Down Expand Up @@ -124,7 +123,7 @@ void main() async {
headers: {
'test': <String>['c'],
},
contentType: MediaType.parse('text/plain'),
contentType: DioMediaType.parse('text/plain'),
),
],
});
Expand Down
3 changes: 1 addition & 2 deletions dio/test/multipart_file_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';

void main() async {
group(MultipartFile, () {
test(
'fromFile sets correct content-type',
() async {
final mediaType = MediaType.parse('text/plain');
final mediaType = DioMediaType.parse('text/plain');
final file = await MultipartFile.fromFile(
'test/mock/_testfile',
filename: '1.txt',
Expand Down