Repository: cfug/dio (dart)

Issue:
I can't use MediaType when I create MultipartFile
### Package

dio

### Version

5.4.3+1

### Operating-System

Android, iOS, Web, MacOS, Linux, Windows

### Adapter

Default Dio

### Output of `flutter doctor -v`

```shell
!] Flutter (Channel stable, 3.19.0, on macOS 14.4.1 23E224 darwin-arm64, locale pt-BR)
    • Flutter version 3.19.0 on channel stable at /Users/usuario/fvm/versions/3.19.0
    ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.2.0/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/usuario/fvm/versions/3.19.0. Consider adding /Users/usuario/fvm/versions/3.19.0/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (3 months ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/usuario/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/usuario/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.87.2)
    • VS Code at /Users/usuario/apps/Visual Studio Code.app/Contents
    • Flutter extension version 3.89.20240501
```


### Dart Version

_No response_

### Steps to Reproduce

When using MultipartFile.fromFile does it ask for a contentType parameter that is of type MediaType? but I can't pass the parameter because it is an http_parser parameter but dio does not export the http_parser


### Expected Result

Future<FormData?> _convertForFormDataDio(
      GenericFormData genericFormData) async {
    final FormData formData = FormData();

    for (String mediaPath in genericFormData.filePaths) {
      formData.files.add(
        MapEntry<String, MultipartFile>(
          'files',
          await MultipartFile.fromFile(
            mediaPath,
            filename: path.basename(mediaPath),
            contentType: MediaType.parse('.png') ,
            headers: <String, List<String>>{
              'Content-Type': <String>['multipart/form-data'],
            },
          ),
        ),
      );
    }

    for (String key in genericFormData.fields.keys) {
      formData.fields.add(MapEntry<String, String>(
          key, genericFormData.fields[key].toString()));
    }

    return formData;
  }

### Actual Result

Future<FormData?> _convertForFormDataDio(
      GenericFormData genericFormData) async {
    final FormData formData = FormData();

    for (String mediaPath in genericFormData.filePaths) {
      formData.files.add(
        MapEntry<String, MultipartFile>(
          'files',
          await MultipartFile.fromFile(
            mediaPath,
            filename: path.basename(mediaPath),
            contentType: MediaType.parse('.png') ,//Undefined name 'MediaType'. Try correcting the name to one that is defined, or defining the name.
            headers: <String, List<String>>{
              'Content-Type': <String>['multipart/form-data'],
            },
          ),
        ),
      );
    }

    for (String key in genericFormData.fields.keys) {
      formData.fields.add(MapEntry<String, String>(
          key, genericFormData.fields[key].toString()));
    }

    return formData;
  }

Pull request description:
Resolves #2212

### New Pull Request Checklist

- [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

This PR exports `MediaType` from the http_parser package so that way users don't have to import a (probably) transitive dependency.

Patch:
diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md
index 8bdad790f..eebd85f38 100644
--- a/dio/CHANGELOG.md
+++ b/dio/CHANGELOG.md
@@ -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
 
diff --git a/dio/lib/src/multipart_file.dart b/dio/lib/src/multipart_file.dart
index 603b333a2..013401acd 100644
--- a/dio/lib/src/multipart_file.dart
+++ b/dio/lib/src/multipart_file.dart
@@ -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;
+
 /// A file to be uploaded as part of a [MultipartRequest]. This doesn't need to
 /// correspond to a physical file.
 ///
@@ -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),
@@ -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),
@@ -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(
@@ -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');
@@ -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;
@@ -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(
@@ -138,7 +141,7 @@ class MultipartFile {
   static MultipartFile fromFileSync(
     String filePath, {
     String? filename,
-    MediaType? contentType,
+    DioMediaType? contentType,
     final Map<String, List<String>>? headers,
   }) =>
       multipartFileFromPathSync(
diff --git a/dio/lib/src/multipart_file/io_multipart_file.dart b/dio/lib/src/multipart_file/io_multipart_file.dart
index 320acfdc4..164ce2419 100644
--- a/dio/lib/src/multipart_file/io_multipart_file.dart
+++ b/dio/lib/src/multipart_file/io_multipart_file.dart
@@ -1,7 +1,6 @@
 import 'dart:async';
 import 'dart:io';
 
-import 'package:http_parser/http_parser.dart';
 import 'package:path/path.dart' as p;
 
 import '../multipart_file.dart';
@@ -9,7 +8,7 @@ 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);
@@ -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);


Test patch:
diff --git a/dio/test/formdata_test.dart b/dio/test/formdata_test.dart
index 3c530d7c4..b416adcdd 100644
--- a/dio/test/formdata_test.dart
+++ b/dio/test/formdata_test.dart
@@ -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';
@@ -36,7 +35,7 @@ void main() async {
               headers: {
                 'test': <String>['c'],
               },
-              contentType: MediaType.parse('text/plain'),
+              contentType: DioMediaType.parse('text/plain'),
             ),
           ],
         });
@@ -88,7 +87,7 @@ void main() async {
               headers: {
                 'test': <String>['c'],
               },
-              contentType: MediaType.parse('text/plain'),
+              contentType: DioMediaType.parse('text/plain'),
             ),
           ),
         );
@@ -124,7 +123,7 @@ void main() async {
               headers: {
                 'test': <String>['c'],
               },
-              contentType: MediaType.parse('text/plain'),
+              contentType: DioMediaType.parse('text/plain'),
             ),
           ],
         });
diff --git a/dio/test/multipart_file_test.dart b/dio/test/multipart_file_test.dart
index 424b1f354..b858736ee 100644
--- a/dio/test/multipart_file_test.dart
+++ b/dio/test/multipart_file_test.dart
@@ -1,5 +1,4 @@
 import 'package:dio/dio.dart';
-import 'package:http_parser/http_parser.dart';
 import 'package:test/test.dart';
 
 void main() async {
@@ -7,7 +6,7 @@ void main() async {
     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',