fix(upload): set explicit MIME type on multipart upload

Without a content type header, http package defaults to
application/octet-stream which the server rejects. Derive MIME
from file extension using a lookup map (Dart 2 compatible).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-08 21:17:55 +05:30
parent c40e600937
commit 9f1de2bead

View File

@@ -2,6 +2,7 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import '../storage/token_storage.dart';
class ApiClient {
@@ -105,7 +106,21 @@ class ApiClient {
/// `{ fileId, url, name, type, mimeType, size, backend }`
Future<Map<String, dynamic>> uploadFile(String url, String filePath) async {
final request = http.MultipartRequest('POST', Uri.parse(url));
request.files.add(await http.MultipartFile.fromPath('file', filePath));
const _mimeMap = <String, List<String>>{
'jpg': ['image', 'jpeg'],
'jpeg': ['image', 'jpeg'],
'png': ['image', 'png'],
'webp': ['image', 'webp'],
'mp4': ['video', 'mp4'],
'mov': ['video', 'quicktime'],
};
final ext = filePath.split('.').last.toLowerCase();
final parts = _mimeMap[ext] ?? ['image', 'jpeg'];
request.files.add(await http.MultipartFile.fromPath(
'file',
filePath,
contentType: MediaType(parts[0], parts[1]),
));
late http.StreamedResponse streamed;
try {