From 387560fad7616c5eb1d2fc770bb1e7f372ec47de Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Fri, 26 Jan 2024 16:20:17 +0100 Subject: [PATCH] Added try catch file access --- lib/Components/cached_custom_resource.dart | 19 ++++++++++++------- lib/Helpers/ImageCustomProvider.dart | 22 ++++++++++++++-------- lib/Screens/PDF/pdf_view.dart | 19 ++++++++++++------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/Components/cached_custom_resource.dart b/lib/Components/cached_custom_resource.dart index 3bbff05..39ce796 100644 --- a/lib/Components/cached_custom_resource.dart +++ b/lib/Components/cached_custom_resource.dart @@ -102,14 +102,19 @@ class CachedCustomResource extends StatelessWidget { } Future _checkIfLocalResourceExists(TabletAppContext tabletAppContext) async { - Directory? appDocumentsDirectory = await getDownloadsDirectory(); - String localPath = appDocumentsDirectory!.path; - Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}'); - List fileList = configurationDirectory.listSync(); + try { + Directory? appDocumentsDirectory = await getDownloadsDirectory(); + String localPath = appDocumentsDirectory!.path; + Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}'); + List fileList = configurationDirectory.listSync(); - if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resourceDTO.id!))) { - File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(resourceDTO.id!)).path); - return file; + if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resourceDTO.id!))) { + File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(resourceDTO.id!)).path); + return file; + } + } catch(e) { + print("ERROR _checkIfLocalResourceExists CachedCustomResource"); + print(e); } return null; diff --git a/lib/Helpers/ImageCustomProvider.dart b/lib/Helpers/ImageCustomProvider.dart index 58ac3fb..21e287a 100644 --- a/lib/Helpers/ImageCustomProvider.dart +++ b/lib/Helpers/ImageCustomProvider.dart @@ -8,19 +8,25 @@ import 'package:tablet_app/app_context.dart'; class ImageCustomProvider { static ImageProvider getImageProvider(AppContext appContext, String imageId, String imageSource) { TabletAppContext tabletAppContext = appContext.getContext(); - if(appContext.getContext().localPath != null && tabletAppContext.configuration != null) { - Directory configurationDirectory = Directory('${tabletAppContext.localPath!}/${tabletAppContext.configuration!.id!}'); - List fileList = configurationDirectory.listSync(); + try { + if(appContext.getContext().localPath != null && tabletAppContext.configuration != null) { + Directory configurationDirectory = Directory('${tabletAppContext.localPath!}/${tabletAppContext.configuration!.id!}'); + List fileList = configurationDirectory.listSync(); - print(fileList); + print(fileList); - if (fileList.isNotEmpty) { - File file = File(fileList.first.path); - print("FILE EXISTT"); - return FileImage(file); + if (fileList.isNotEmpty) { + File file = File(fileList.first.path); + print("FILE EXISTT"); + return FileImage(file); + } } + } catch(e) { + print("Error getImageProvider"); + print(e.toString()); } + // If localpath not found or file missing print("MISSINGG FILE"); return CachedNetworkImageProvider(imageSource); diff --git a/lib/Screens/PDF/pdf_view.dart b/lib/Screens/PDF/pdf_view.dart index 7e68715..2c4130a 100644 --- a/lib/Screens/PDF/pdf_view.dart +++ b/lib/Screens/PDF/pdf_view.dart @@ -165,14 +165,19 @@ class _PDFViewWidget extends State { } //_webView Future _checkIfLocalResourceExists(TabletAppContext tabletAppContext, String resourceId) async { - Directory? appDocumentsDirectory = await getDownloadsDirectory(); - String localPath = appDocumentsDirectory!.path; - Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}'); - List fileList = configurationDirectory.listSync(); + try { + Directory? appDocumentsDirectory = await getDownloadsDirectory(); + String localPath = appDocumentsDirectory!.path; + Directory configurationDirectory = Directory('$localPath/${tabletAppContext.configuration!.id}'); + List fileList = configurationDirectory.listSync(); - if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resourceId))) { - File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(resourceId)).path); - return file; + if(fileList.any((fileL) => fileL.uri.pathSegments.last.contains(resourceId))) { + File file = File(fileList.firstWhere((fileL) => fileL.uri.pathSegments.last.contains(resourceId)).path); + return file; + } + } catch (e) { + print("ERROR _checkIfLocalResourceExists PDF"); + print(e); } return null;