From bd7d538481f747bbfbeda64a76ef300c7cd7c26f Mon Sep 17 00:00:00 2001 From: venbaittech Date: Wed, 8 Oct 2025 15:06:06 +0530 Subject: [PATCH] UI_PASSPORT_DOC_DOWNLOAD --- .../create_user/traveller_details.dart | 38 ++++---- lib/services/apiService.dart | 97 +++++++++++++++++++ 2 files changed, 118 insertions(+), 17 deletions(-) diff --git a/lib/Screens/userManagement/create_user/traveller_details.dart b/lib/Screens/userManagement/create_user/traveller_details.dart index fb14bfe..0926b9a 100644 --- a/lib/Screens/userManagement/create_user/traveller_details.dart +++ b/lib/Screens/userManagement/create_user/traveller_details.dart @@ -2155,7 +2155,7 @@ class TravellerDetailsState extends State { children: [ GestureDetector( onTap: () { - print('DOWNLOAD - $passportFile'); + print('DOWNLOAD - $passportFile - ${widget.userIdApi} '); if (passportFile != null) { try { @@ -2204,23 +2204,27 @@ class TravellerDetailsState extends State { } } else if (passportFileUrlFromApi != null) { print("Raw file path: $passportFileUrlFromApi"); + apiService.getPassportDocDownload( + context, + widget.userIdApi, + ); - // Extract public part of the path starting from "/assets" - String cleanedPath = passportFileUrlFromApi!; - final index = passportFileUrlFromApi!.indexOf("/assets"); - if (index != -1) { - cleanedPath = passportFileUrlFromApi!.substring(index); - } - - final fullUrl = - 'https://apitest.tripapprovaltool.com$cleanedPath'; - print("Final download URL: $fullUrl"); - - final anchor = - html.AnchorElement(href: fullUrl) - ..target = '_blank' - ..download = selectedFileNames ?? "document.pdf" - ..click(); + // // Extract public part of the path starting from "/assets" + // String cleanedPath = passportFileUrlFromApi!; + // final index = passportFileUrlFromApi!.indexOf("/assets"); + // if (index != -1) { + // cleanedPath = passportFileUrlFromApi!.substring(index); + // } + // + // final fullUrl = + // 'https://apitest.tripapprovaltool.com$cleanedPath'; + // print("Final download URL: $fullUrl"); + // + // final anchor = + // html.AnchorElement(href: fullUrl) + // ..target = '_blank' + // ..download = selectedFileNames ?? "document.pdf" + // ..click(); } else { print("No file available to download."); } diff --git a/lib/services/apiService.dart b/lib/services/apiService.dart index 60a9922..5fe4572 100644 --- a/lib/services/apiService.dart +++ b/lib/services/apiService.dart @@ -1145,6 +1145,103 @@ class ApiService { } } + Future getPassportDocDownload(BuildContext context, userId) async { + // final String apiUrldata = '$apiUrl/api/plans/download?plan_id=$planId'; + final String apiUrldata = '$apiUrl/api/downloadPassport?user_id=$userId'; + + // final String apiUrldata = '$apiUrl/auth/googlelogin'; + + final token = await getToken(); + + if (token == null) { + throw Exception('Token not found. Please log in.'); + } + + final response = await http.get( + Uri.parse(apiUrldata), + headers: { + 'Authorization': 'Bearer $token', + 'Content-Type': 'application/json', + 'app-signature': 'ts-traveltool-2025-signature-123456', + }, + ); + + if (response.statusCode == 200) { + try { + print("PDf Dowloaded"); + final contentType = response.headers['content-type'] ?? ''; + + // print('contentType - $contentType'); + // String fileName = "Document"; + // if (contentType.contains("pdf")) { + // fileName += ".pdf"; + // } else if (contentType.contains("png")) { + // fileName += ".png"; + // } else if (contentType.contains("jpeg") || + // contentType.contains("jpg")) { + // fileName += ".jpg"; + // } else { + // // fallback (unknown type, save as bin) + // fileName += ".$contentType"; + // } + + final disposition = response.headers['content-disposition'] ?? ''; + String fileName = "passport_$userId"; + + final regExp = RegExp( + r'filename[^;=\n]*=(([' + '"]).*?\2|[^;\n]*)', + ); + final match = regExp.firstMatch(disposition); + if (match != null) { + fileName = match.group(1)!.replaceAll('"', ''); + } + + // Create a blob from the response body + final blob = html.Blob([response.bodyBytes]); + + // Generate a download URL for the blob + final url = html.Url.createObjectUrlFromBlob(blob); + + // Create a link element to trigger the download + final anchor = + html.AnchorElement(href: url) + ..setAttribute('download', fileName) + ..click(); + + // Revoke the download URL to free up resources + html.Url.revokeObjectUrl(url); + } catch (e) { + throw Exception('Error parsing response: $e'); + } + } else if (response.statusCode == 403) { + print("403-FORB"); + await logout(context); + return null; + // throw Exception('Failed to load users'); + } else if (response.statusCode == 404) { + // showDialog( + // context: context, + // builder: (BuildContext context) { + // return AlertDialog( + // title: Text('File not found.'), + // // content: Text('File not found.'), + // actions: [ + // TextButton( + // child: Text('OK'), + // onPressed: () { + // Navigator.of(context).pop(); // Close the dialog + // }, + // ), + // ], + // ); + // }, + // ); + } else { + throw Exception('Failed to load plans'); + } + } + Future getForexPdfDownload(BuildContext context, forexId) async { final String apiUrldata = '$apiUrl/api/plans/forexDownload?forex_id=$forexId';