From 129c4099010472f8cda145b2e76b1cec1c32df81 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sat, 2 May 2026 15:17:46 +0300 Subject: [PATCH] 26-5-2 --- lib/controller/functions/ocr_controller.dart | 12 +- lib/controller/functions/upload_image.dart | 106 ++++++++---------- .../Captin/home_captain/drawer_captain.dart | 10 +- 3 files changed, 57 insertions(+), 71 deletions(-) diff --git a/lib/controller/functions/ocr_controller.dart b/lib/controller/functions/ocr_controller.dart index 1245715..c96dfb5 100755 --- a/lib/controller/functions/ocr_controller.dart +++ b/lib/controller/functions/ocr_controller.dart @@ -483,11 +483,13 @@ class ScanDocumentsByApi extends GetxController { } else {} } - // Todo upload images and fields Future uploadImagePortrate() async { isLoading = true; update(); + final String token = box.read(BoxName.jwt)?.toString().split(AppInformation.addd)[0] ?? ''; + final String fingerPrint = box.read(BoxName.deviceFingerprint)?.toString() ?? ''; + var request = http.MultipartRequest( 'POST', Uri.parse(AppLink.uploadImagePortrate), @@ -498,22 +500,18 @@ class ScanDocumentsByApi extends GetxController { ); request.headers.addAll({ - "Content-Type": "multipart/form-data", - 'Authorization': - 'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}', + 'Authorization': 'Bearer $token', + 'X-Device-FP': fingerPrint, }); request.fields['driverID'] = box.read(BoxName.driverID).toString(); var response = await request.send(); - var responseData = await response.stream.toBytes(); var responseString = String.fromCharCodes(responseData); isLoading = false; update(); - // Print the response string - return responseString; } diff --git a/lib/controller/functions/upload_image.dart b/lib/controller/functions/upload_image.dart index 3a30127..37039b1 100755 --- a/lib/controller/functions/upload_image.dart +++ b/lib/controller/functions/upload_image.dart @@ -304,27 +304,19 @@ class ImageController extends GetxController { } uploadImage(File file, Map data, String link) async { - var request = http.MultipartRequest( - 'POST', - Uri.parse(link), - ); - Log.print('request: ${request}'); + 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)); + Log.print('uploadImage -> $link'); + + request.headers.addAll({ + 'Authorization': 'Bearer $token', + 'X-Device-FP': fingerPrint, + }); + var length = await file.length(); var stream = http.ByteStream(file.openRead()); - final headers = { - 'Authorization': - 'Bearer ${r(box.read(BoxName.jwt)).split(AppInformation.addd)[0]}', - 'X-HMAC-Auth': '${box.read(BoxName.hmac)}', - }; - var multipartFile = http.MultipartFile( - 'image', - stream, - length, - filename: basename(file.path), - ); - request.headers.addAll(headers); - // Set the file name to the driverID - request.files.add( http.MultipartFile( 'image', @@ -336,36 +328,29 @@ class ImageController extends GetxController { data.forEach((key, value) { request.fields[key] = value; }); + var myrequest = await request.send(); var res = await http.Response.fromStream(myrequest); + Log.print('uploadImage response [${res.statusCode}]: ${res.body}'); if (res.statusCode == 200) { - Log.print('jsonDecode(res.body): ${jsonDecode(res.body)}'); return jsonDecode(res.body); } else { - throw Exception( - 'Failed to upload image: ${res.statusCode} - ${res.body}'); + throw Exception('Failed to upload image: ${res.statusCode} - ${res.body}'); } } uploadNewCar(File file, Map data, String link) async { - var request = http.MultipartRequest( - 'POST', - Uri.parse(link), - ); + 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()); - var multipartFile = http.MultipartFile( - 'image', - stream, - length, - filename: basename(file.path), - ); - request.headers.addAll({ - 'Authorization': - 'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}', - }); - // Set the file name to the driverID request.files.add( http.MultipartFile( 'image', @@ -377,14 +362,14 @@ class ImageController extends GetxController { 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) { - Log.print('jsonDecode(res.body): ${jsonDecode(res.body)}'); return jsonDecode(res.body); } else { - throw Exception( - 'Failed to upload image: ${res.statusCode} - ${res.body}'); + throw Exception('Failed to upload image: ${res.statusCode} - ${res.body}'); } } @@ -420,16 +405,21 @@ class ImageController extends GetxController { File compressedImage = await compressImage(File(croppedFile!.path)); print('link =$link'); try { - await uploadImage( + var response = await uploadImage( compressedImage, {'driverID': (box.read(BoxName.driverID)), 'imageType': imageType}, 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()); + } + } } catch (e) { Log.print('e: ${e}'); mySnackeBarError('Image Upload Failed'.tr); - // Get.snackbar('Image Upload Failed'.tr, e.toString(), - // backgroundColor: AppColor.redColor); } finally { isloading = false; update(); @@ -437,42 +427,36 @@ class ImageController extends GetxController { } uploadImagePicture(File file, Map data, String link) async { - var request = http.MultipartRequest( - 'POST', - Uri.parse(link), //'https://ride.mobile-app.store/uploadImage1.php' - ); + 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()); - var multipartFile = http.MultipartFile( - 'image', - stream, - length, - filename: basename(file.path), - ); - request.headers.addAll({ - 'Authorization': - 'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}', - }); - // Set the file name to the driverID request.files.add( http.MultipartFile( 'image', stream, length, - filename: '${(box.read(BoxName.driverID))}.jpg', + filename: '${box.read(BoxName.driverID)}.jpg', ), ); 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}'); + throw Exception('Failed to upload image: ${res.statusCode} - ${res.body}'); } } } diff --git a/lib/views/home/Captin/home_captain/drawer_captain.dart b/lib/views/home/Captin/home_captain/drawer_captain.dart index 53b5387..e26b942 100755 --- a/lib/views/home/Captin/home_captain/drawer_captain.dart +++ b/lib/views/home/Captin/home_captain/drawer_captain.dart @@ -308,9 +308,13 @@ class UserHeader extends StatelessWidget { child: controller.isloading ? const CircularProgressIndicator(color: Colors.white) : CircleAvatar( - // محاولة تحميل الصورة - backgroundImage: NetworkImage( - '${AppLink.server}/portrate_captain_image/${box.read(BoxName.driverID)}.jpg'), + backgroundImage: NetworkImage((box + .read(BoxName.driverPhotoUrl) + ?.toString() + .isNotEmpty == + true) + ? box.read(BoxName.driverPhotoUrl) + : '${AppLink.server}/portrate_captain_image/${box.read(BoxName.driverID)}.jpg'), // [تعديل هام]: في حال فشل تحميل الصورة (غير موجودة) onBackgroundImageError: (exception, stackTrace) {