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:
@@ -2,6 +2,7 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
import '../storage/token_storage.dart';
|
import '../storage/token_storage.dart';
|
||||||
|
|
||||||
class ApiClient {
|
class ApiClient {
|
||||||
@@ -105,7 +106,21 @@ class ApiClient {
|
|||||||
/// `{ fileId, url, name, type, mimeType, size, backend }`
|
/// `{ fileId, url, name, type, mimeType, size, backend }`
|
||||||
Future<Map<String, dynamic>> uploadFile(String url, String filePath) async {
|
Future<Map<String, dynamic>> uploadFile(String url, String filePath) async {
|
||||||
final request = http.MultipartRequest('POST', Uri.parse(url));
|
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;
|
late http.StreamedResponse streamed;
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user