From 39e486e439b76f3e046c49e1f3c46bb52e2e4bb4 Mon Sep 17 00:00:00 2001 From: OkaykOrhmn Date: Sun, 8 Dec 2024 11:09:45 +0330 Subject: [PATCH] `Added file size limit check for image uploads and updated related code` --- lib/services/media/media.dart | 1 + lib/services/network/request.dart | 4 +- lib/views/ai/ai.dart | 4 -- lib/views/ai/create_bot_assistants_page.dart | 61 ++++++++++++++++--- lib/views/ai/create_bot_assistants_state.dart | 6 +- lib/views/home/home.dart | 8 +-- 6 files changed, 61 insertions(+), 23 deletions(-) diff --git a/lib/services/media/media.dart b/lib/services/media/media.dart index 3811264..2f41c32 100644 --- a/lib/services/media/media.dart +++ b/lib/services/media/media.dart @@ -26,6 +26,7 @@ class MediaService { static String? audioPlayerTag; static StudioDetailsData? currentPodcast; static StudioRequestArgs? podcastPlaylistArgs; + static int maxSizeInBytes = 15 * 1024 * 1024; static Duration? get duration => audioPlayer.duration; diff --git a/lib/services/network/request.dart b/lib/services/network/request.dart index 0234140..3443318 100644 --- a/lib/services/network/request.dart +++ b/lib/services/network/request.dart @@ -138,7 +138,7 @@ class RequestService { if (files != null) { for (var file in files) { final length = await file.length(); - final mimeType = lookupMimeType(file.path) ?? + final mimeType = lookupMimeType(file.name) ?? 'application/octet-stream'; // Get content type request.files.add( @@ -156,7 +156,7 @@ class RequestService { if (image != null) { final length = await image.length(); - final mimeType = lookupMimeType(image.path) ?? + final mimeType = lookupMimeType(image.name) ?? 'application/octet-stream'; // Get content type request.files.add( diff --git a/lib/views/ai/ai.dart b/lib/views/ai/ai.dart index 8aa9670..1fa96b8 100644 --- a/lib/views/ai/ai.dart +++ b/lib/views/ai/ai.dart @@ -30,10 +30,6 @@ class Ai extends StatefulWidget { class _AiState extends State { @override void initState() { - final state = context.read(); - - state.getBots(); - super.initState(); } diff --git a/lib/views/ai/create_bot_assistants_page.dart b/lib/views/ai/create_bot_assistants_page.dart index 6b49e1a..0967da9 100644 --- a/lib/views/ai/create_bot_assistants_page.dart +++ b/lib/views/ai/create_bot_assistants_page.dart @@ -106,14 +106,13 @@ class _CreateBotAssistantsPageState extends State { ? state.assistant!.image == null : state.image.value == null, image: state.image.value)); - if (success) { + if (success == null) { context.read().getMyAssissmant(); context.read().assistant = null; Navigator.pop(context); } else { - ActionSheetUtils(context).showAlert(AlertData( - message: 'مشکلی در ارتباط با سرور پیش آمده دوباره تلاش کنید', - aLertType: ALertType.error)); + ActionSheetUtils(context) + .showAlert(AlertData(message: success, aLertType: ALertType.error)); } } @@ -228,9 +227,35 @@ class _CreateBotAssistantsPageState extends State { ), compressQuality: 30, ); - if (file != null) { - state.image.value = XFile(file.path); + } + + if (file != null) { + final fileSizeInBytes = + await file.length(); + if (fileSizeInBytes > + MediaService.maxSizeInBytes) { + ActionSheetUtils(context).showAlert( + AlertData( + message: + 'حجم فایل باید کمتر از 15 مگ باشد', + aLertType: ALertType.error)); + return; } + + state.image.value = XFile(file.path); + } else if (kIsWeb && pickedFile != null) { + final fileSizeInBytes = + await pickedFile.length(); + if (fileSizeInBytes > + MediaService.maxSizeInBytes) { + ActionSheetUtils(context).showAlert( + AlertData( + message: + 'حجم فایل باید کمتر از 15 مگ باشد', + aLertType: ALertType.error)); + return; + } + state.image.value = pickedFile; } }, ), @@ -241,8 +266,9 @@ class _CreateBotAssistantsPageState extends State { child: SizedBox( width: 80, height: 80, - child: - Image.file(File(img.path))), + child: kIsWeb + ? Image.network(img.path) + : Image.file(File(img.path))), ) : SkeletonImage( imageUrl: state.assistant != null && @@ -460,10 +486,25 @@ class _CreateBotAssistantsPageState extends State { onPressed: () async { final picks = await MediaService .pickMultiFile(); + if (picks != null) { for (var file in picks.xFiles) { - if (file.path.isDocument() || - file.path.isAudio()) { + if (file.name.isDocument() || + file.name.isAudio()) { + final fileSizeInBytes = + await file.length(); + if (fileSizeInBytes > + MediaService + .maxSizeInBytes) { + ActionSheetUtils(context) + .showAlert(AlertData( + message: + 'حجم فایل باید کمتر از 15 مگ باشد', + aLertType: + ALertType + .error)); + return; + } state.files.add( FileCreateAssistantsModel( fromNetwork: false, diff --git a/lib/views/ai/create_bot_assistants_state.dart b/lib/views/ai/create_bot_assistants_state.dart index c71511f..18d3a6a 100644 --- a/lib/views/ai/create_bot_assistants_state.dart +++ b/lib/views/ai/create_bot_assistants_state.dart @@ -62,7 +62,7 @@ class CreateBotAssistantsState extends CoreProvier { update(); } - Future createAssistants( + Future createAssistants( {required final BotAssistantsReqModel data, final int? id}) async { loadingCreate = true; update(); @@ -80,12 +80,12 @@ class CreateBotAssistantsState extends CoreProvier { appState = AppState.idle; loadingCreate = false; update(); - return true; + return null; } appState = AppState.failed; loadingCreate = false; update(); - return false; + return service.errorMessage; } Future getAnAssistant({required final int id}) async { diff --git a/lib/views/home/home.dart b/lib/views/home/home.dart index 22dc4e2..0e4f1b8 100644 --- a/lib/views/home/home.dart +++ b/lib/views/home/home.dart @@ -139,11 +139,11 @@ class _HomeState extends State state.tabController = _tabController; _tabController.addListener(() { state.currentPageIndex = _tabController.index; - // if (_tabController.index == 2) { - // homeScaffKey.currentState!.closeDrawer(); + if (_tabController.index == 2) { + final state = context.read(); - // context.read().getBots(); - // } + state.getBots(); + } }); if (!kIsWeb) { Future.delayed(Duration.zero, () {