Update: 2026-06-12 01:23:54
This commit is contained in:
@@ -489,25 +489,58 @@ class ScanDocumentsByApi extends GetxController {
|
||||
|
||||
final String token = box.read(BoxName.jwt)?.toString().split(AppInformation.addd)[0] ?? '';
|
||||
final String fingerPrint = box.read(BoxName.deviceFingerprint)?.toString() ?? '';
|
||||
final String driverID = box.read(BoxName.driverID).toString();
|
||||
final String link = AppLink.uploadImagePortrate;
|
||||
|
||||
var request = http.MultipartRequest(
|
||||
'POST',
|
||||
Uri.parse(AppLink.uploadImagePortrate),
|
||||
);
|
||||
int maxRetries = 3;
|
||||
int attempt = 0;
|
||||
String responseString = '';
|
||||
|
||||
request.files.add(
|
||||
http.MultipartFile.fromBytes('image', imagePortrait),
|
||||
);
|
||||
while (attempt < maxRetries) {
|
||||
attempt++;
|
||||
final client = http.Client();
|
||||
try {
|
||||
Log.print('[UPLOAD_LOGGER] 🚀 [uploadImagePortrate] Attempt $attempt/$maxRetries started. Link: $link, Image Size: ${imagePortrait.length} bytes');
|
||||
|
||||
var request = http.MultipartRequest('POST', Uri.parse(link));
|
||||
request.files.add(
|
||||
http.MultipartFile.fromBytes('image', imagePortrait, filename: '$driverID.jpg'),
|
||||
);
|
||||
|
||||
request.headers.addAll({
|
||||
'Authorization': 'Bearer $token',
|
||||
'X-Device-FP': fingerPrint,
|
||||
});
|
||||
request.fields['driverID'] = box.read(BoxName.driverID).toString();
|
||||
request.headers.addAll({
|
||||
'Authorization': 'Bearer $token',
|
||||
'X-Device-FP': fingerPrint,
|
||||
});
|
||||
request.fields['driverID'] = driverID;
|
||||
|
||||
var response = await request.send();
|
||||
var responseData = await response.stream.toBytes();
|
||||
var responseString = String.fromCharCodes(responseData);
|
||||
final startTime = DateTime.now();
|
||||
var streamedResponse = await client.send(request).timeout(const Duration(seconds: 120));
|
||||
var res = await http.Response.fromStream(streamedResponse);
|
||||
final duration = DateTime.now().difference(startTime);
|
||||
|
||||
Log.print('[UPLOAD_LOGGER] 📥 [uploadImagePortrate] Attempt $attempt response received in ${duration.inSeconds}s. Status Code: ${res.statusCode}');
|
||||
Log.print('[UPLOAD_LOGGER] 📥 [uploadImagePortrate] Response Body: ${res.body}');
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
responseString = res.body;
|
||||
break; // Success
|
||||
} else {
|
||||
throw Exception('Failed to upload portrait: ${res.statusCode} - ${res.body}');
|
||||
}
|
||||
} catch (e, st) {
|
||||
Log.print('[UPLOAD_LOGGER] ⚠️ [uploadImagePortrate] Attempt $attempt failed. Error: $e', stackTrace: st);
|
||||
if (attempt >= maxRetries) {
|
||||
isLoading = false;
|
||||
update();
|
||||
rethrow;
|
||||
}
|
||||
final waitSeconds = attempt * 2;
|
||||
Log.print('[UPLOAD_LOGGER] ⏳ Waiting $waitSeconds seconds before retrying...');
|
||||
await Future.delayed(Duration(seconds: waitSeconds));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
update();
|
||||
|
||||
@@ -303,83 +303,104 @@ class ImageController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
uploadImage(File file, Map data, String link) async {
|
||||
Future<dynamic> _uploadMultipartFile({
|
||||
required File file,
|
||||
required Map data,
|
||||
required String link,
|
||||
required String fileKey,
|
||||
required String filename,
|
||||
required String methodLabel,
|
||||
}) async {
|
||||
final String token = r(box.read(BoxName.jwt)).split(AppInformation.addd)[0];
|
||||
final String fingerPrint = box.read(BoxName.deviceFingerprint)?.toString() ?? '';
|
||||
final int fileSizeBytes = await file.length();
|
||||
|
||||
var request = http.MultipartRequest('POST', Uri.parse(link));
|
||||
Log.print('uploadImage -> $link');
|
||||
int maxRetries = 3;
|
||||
int attempt = 0;
|
||||
|
||||
request.headers.addAll({
|
||||
'Authorization': 'Bearer $token',
|
||||
'X-Device-FP': fingerPrint,
|
||||
});
|
||||
while (attempt < maxRetries) {
|
||||
attempt++;
|
||||
final client = http.Client();
|
||||
try {
|
||||
Log.print('[UPLOAD_LOGGER] 🚀 [$methodLabel] Attempt $attempt/$maxRetries started. Link: $link, File Size: $fileSizeBytes bytes');
|
||||
|
||||
var request = http.MultipartRequest('POST', Uri.parse(link));
|
||||
request.headers.addAll({
|
||||
'Authorization': 'Bearer $token',
|
||||
'X-Device-FP': fingerPrint,
|
||||
});
|
||||
|
||||
var length = await file.length();
|
||||
var stream = http.ByteStream(file.openRead());
|
||||
request.files.add(
|
||||
http.MultipartFile(
|
||||
'image',
|
||||
stream,
|
||||
length,
|
||||
filename: '${box.read(BoxName.driverID)}.jpg',
|
||||
),
|
||||
);
|
||||
data.forEach((key, value) {
|
||||
request.fields[key] = value;
|
||||
});
|
||||
var length = await file.length();
|
||||
var stream = http.ByteStream(file.openRead());
|
||||
request.files.add(
|
||||
http.MultipartFile(
|
||||
fileKey,
|
||||
stream,
|
||||
length,
|
||||
filename: filename,
|
||||
),
|
||||
);
|
||||
data.forEach((key, value) {
|
||||
request.fields[key.toString()] = value.toString();
|
||||
});
|
||||
|
||||
var myrequest = await request.send();
|
||||
var res = await http.Response.fromStream(myrequest);
|
||||
Log.print('uploadImage response [${res.statusCode}]: ${res.body}');
|
||||
if (res.statusCode == 200) {
|
||||
return jsonDecode(res.body);
|
||||
} else {
|
||||
throw Exception('Failed to upload image: ${res.statusCode} - ${res.body}');
|
||||
final startTime = DateTime.now();
|
||||
var streamedResponse = await client.send(request).timeout(const Duration(seconds: 120));
|
||||
var res = await http.Response.fromStream(streamedResponse);
|
||||
final duration = DateTime.now().difference(startTime);
|
||||
|
||||
Log.print('[UPLOAD_LOGGER] 📥 [$methodLabel] Attempt $attempt response received in ${duration.inSeconds}s. Status Code: ${res.statusCode}');
|
||||
Log.print('[UPLOAD_LOGGER] 📥 [$methodLabel] Response Body: ${res.body}');
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
final decoded = jsonDecode(res.body);
|
||||
if (decoded is Map && decoded['status'] == 'failure') {
|
||||
throw Exception('Server returned failure status: ${decoded['message']}');
|
||||
}
|
||||
return decoded;
|
||||
} else {
|
||||
throw Exception('Failed to upload image: ${res.statusCode} - ${res.body}');
|
||||
}
|
||||
} catch (e, st) {
|
||||
Log.print('[UPLOAD_LOGGER] ⚠️ [$methodLabel] Attempt $attempt failed. Error: $e', stackTrace: st);
|
||||
if (attempt >= maxRetries) {
|
||||
rethrow;
|
||||
}
|
||||
final waitSeconds = attempt * 2;
|
||||
Log.print('[UPLOAD_LOGGER] ⏳ Waiting $waitSeconds seconds before retrying...');
|
||||
await Future.delayed(Duration(seconds: waitSeconds));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
throw Exception('Upload failed after $maxRetries attempts');
|
||||
}
|
||||
|
||||
uploadImage(File file, Map data, String link) async {
|
||||
return _uploadMultipartFile(
|
||||
file: file,
|
||||
data: data,
|
||||
link: link,
|
||||
fileKey: 'image',
|
||||
filename: '${box.read(BoxName.driverID)}.jpg',
|
||||
methodLabel: 'uploadImage',
|
||||
);
|
||||
}
|
||||
|
||||
uploadNewCar(File file, Map data, String link) async {
|
||||
final String token = r(box.read(BoxName.jwt)).split(AppInformation.addd)[0];
|
||||
final String fingerPrint = box.read(BoxName.deviceFingerprint)?.toString() ?? '';
|
||||
|
||||
var request = http.MultipartRequest('POST', Uri.parse(link));
|
||||
request.headers.addAll({
|
||||
'Authorization': 'Bearer $token',
|
||||
'X-Device-FP': fingerPrint,
|
||||
});
|
||||
|
||||
var length = await file.length();
|
||||
var stream = http.ByteStream(file.openRead());
|
||||
request.files.add(
|
||||
http.MultipartFile(
|
||||
'image',
|
||||
stream,
|
||||
length,
|
||||
filename: '${box.read(BoxName.driverID)}.jpg',
|
||||
),
|
||||
return _uploadMultipartFile(
|
||||
file: file,
|
||||
data: data,
|
||||
link: link,
|
||||
fileKey: 'image',
|
||||
filename: '${box.read(BoxName.driverID)}.jpg',
|
||||
methodLabel: 'uploadNewCar',
|
||||
);
|
||||
data.forEach((key, value) {
|
||||
request.fields[key] = value;
|
||||
});
|
||||
|
||||
var myrequest = await request.send();
|
||||
var res = await http.Response.fromStream(myrequest);
|
||||
Log.print('uploadNewCar response [${res.statusCode}]: ${res.body}');
|
||||
if (res.statusCode == 200) {
|
||||
return jsonDecode(res.body);
|
||||
} else {
|
||||
throw Exception('Failed to upload image: ${res.statusCode} - ${res.body}');
|
||||
}
|
||||
}
|
||||
|
||||
choosImagePicture(String link, String imageType) async {
|
||||
final pickedImage = await picker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
// preferredCameraDevice: CameraDevice.rear,
|
||||
// maxHeight: Get.height * .3,
|
||||
// maxWidth: Get.width * .9,
|
||||
// imageQuality: 100,
|
||||
);
|
||||
image = File(pickedImage!.path);
|
||||
|
||||
@@ -400,8 +421,7 @@ class ImageController extends GetxController {
|
||||
myImage = File(pickedImage.path);
|
||||
isloading = true;
|
||||
update();
|
||||
// Save the cropped image
|
||||
// File savedCroppedImage = File(croppedFile!.path);
|
||||
|
||||
File compressedImage = await compressImage(File(croppedFile!.path));
|
||||
print('link =$link');
|
||||
try {
|
||||
@@ -411,7 +431,6 @@ class ImageController extends GetxController {
|
||||
link,
|
||||
);
|
||||
|
||||
// Save the returned URL from the V3 backend to local storage
|
||||
if (response != null && response['status'] == 'success' && response['message'] != null) {
|
||||
if (response['message']['file_link'] != null) {
|
||||
box.write(BoxName.driverPhotoUrl, response['message']['file_link'].toString());
|
||||
@@ -427,37 +446,14 @@ class ImageController extends GetxController {
|
||||
}
|
||||
|
||||
uploadImagePicture(File file, Map data, String link) async {
|
||||
final String token = r(box.read(BoxName.jwt)).split(AppInformation.addd)[0];
|
||||
final String fingerPrint = box.read(BoxName.deviceFingerprint)?.toString() ?? '';
|
||||
|
||||
var request = http.MultipartRequest('POST', Uri.parse(link));
|
||||
request.headers.addAll({
|
||||
'Authorization': 'Bearer $token',
|
||||
'X-Device-FP': fingerPrint,
|
||||
});
|
||||
|
||||
var length = await file.length();
|
||||
var stream = http.ByteStream(file.openRead());
|
||||
request.files.add(
|
||||
http.MultipartFile(
|
||||
'image',
|
||||
stream,
|
||||
length,
|
||||
filename: '${box.read(BoxName.driverID)}.jpg',
|
||||
),
|
||||
return _uploadMultipartFile(
|
||||
file: file,
|
||||
data: data,
|
||||
link: link,
|
||||
fileKey: 'image',
|
||||
filename: '${box.read(BoxName.driverID)}.jpg',
|
||||
methodLabel: 'uploadImagePicture',
|
||||
);
|
||||
data.forEach((key, value) {
|
||||
request.fields[key] = value;
|
||||
});
|
||||
|
||||
var myrequest = await request.send();
|
||||
var res = await http.Response.fromStream(myrequest);
|
||||
Log.print('uploadImagePicture response [${res.statusCode}]: ${res.body}');
|
||||
if (res.statusCode == 200) {
|
||||
return jsonDecode(res.body);
|
||||
} else {
|
||||
throw Exception('Failed to upload image: ${res.statusCode} - ${res.body}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user