`Added file size limit check for image uploads and updated related code`

This commit is contained in:
OkaykOrhmn 2024-12-08 11:09:45 +03:30
parent e4c9c1f369
commit 39e486e439
6 changed files with 61 additions and 23 deletions

View File

@ -26,6 +26,7 @@ class MediaService {
static String? audioPlayerTag; static String? audioPlayerTag;
static StudioDetailsData? currentPodcast; static StudioDetailsData? currentPodcast;
static StudioRequestArgs? podcastPlaylistArgs; static StudioRequestArgs? podcastPlaylistArgs;
static int maxSizeInBytes = 15 * 1024 * 1024;
static Duration? get duration => audioPlayer.duration; static Duration? get duration => audioPlayer.duration;

View File

@ -138,7 +138,7 @@ class RequestService {
if (files != null) { if (files != null) {
for (var file in files) { for (var file in files) {
final length = await file.length(); final length = await file.length();
final mimeType = lookupMimeType(file.path) ?? final mimeType = lookupMimeType(file.name) ??
'application/octet-stream'; // Get content type 'application/octet-stream'; // Get content type
request.files.add( request.files.add(
@ -156,7 +156,7 @@ class RequestService {
if (image != null) { if (image != null) {
final length = await image.length(); final length = await image.length();
final mimeType = lookupMimeType(image.path) ?? final mimeType = lookupMimeType(image.name) ??
'application/octet-stream'; // Get content type 'application/octet-stream'; // Get content type
request.files.add( request.files.add(

View File

@ -30,10 +30,6 @@ class Ai extends StatefulWidget {
class _AiState extends State<Ai> { class _AiState extends State<Ai> {
@override @override
void initState() { void initState() {
final state = context.read<HistoryAiChatState>();
state.getBots();
super.initState(); super.initState();
} }

View File

@ -106,14 +106,13 @@ class _CreateBotAssistantsPageState extends State<CreateBotAssistantsPage> {
? state.assistant!.image == null ? state.assistant!.image == null
: state.image.value == null, : state.image.value == null,
image: state.image.value)); image: state.image.value));
if (success) { if (success == null) {
context.read<BotAssistantsState>().getMyAssissmant(); context.read<BotAssistantsState>().getMyAssissmant();
context.read<CreateBotAssistantsState>().assistant = null; context.read<CreateBotAssistantsState>().assistant = null;
Navigator.pop(context); Navigator.pop(context);
} else { } else {
ActionSheetUtils(context).showAlert(AlertData( ActionSheetUtils(context)
message: 'مشکلی در ارتباط با سرور پیش آمده دوباره تلاش کنید', .showAlert(AlertData(message: success, aLertType: ALertType.error));
aLertType: ALertType.error));
} }
} }
@ -228,9 +227,35 @@ class _CreateBotAssistantsPageState extends State<CreateBotAssistantsPage> {
), ),
compressQuality: 30, 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<CreateBotAssistantsPage> {
child: SizedBox( child: SizedBox(
width: 80, width: 80,
height: 80, height: 80,
child: child: kIsWeb
Image.file(File(img.path))), ? Image.network(img.path)
: Image.file(File(img.path))),
) )
: SkeletonImage( : SkeletonImage(
imageUrl: state.assistant != null && imageUrl: state.assistant != null &&
@ -460,10 +486,25 @@ class _CreateBotAssistantsPageState extends State<CreateBotAssistantsPage> {
onPressed: () async { onPressed: () async {
final picks = await MediaService final picks = await MediaService
.pickMultiFile(); .pickMultiFile();
if (picks != null) { if (picks != null) {
for (var file in picks.xFiles) { for (var file in picks.xFiles) {
if (file.path.isDocument() || if (file.name.isDocument() ||
file.path.isAudio()) { 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( state.files.add(
FileCreateAssistantsModel( FileCreateAssistantsModel(
fromNetwork: false, fromNetwork: false,

View File

@ -62,7 +62,7 @@ class CreateBotAssistantsState extends CoreProvier {
update(); update();
} }
Future<bool> createAssistants( Future<String?> createAssistants(
{required final BotAssistantsReqModel data, final int? id}) async { {required final BotAssistantsReqModel data, final int? id}) async {
loadingCreate = true; loadingCreate = true;
update(); update();
@ -80,12 +80,12 @@ class CreateBotAssistantsState extends CoreProvier {
appState = AppState.idle; appState = AppState.idle;
loadingCreate = false; loadingCreate = false;
update(); update();
return true; return null;
} }
appState = AppState.failed; appState = AppState.failed;
loadingCreate = false; loadingCreate = false;
update(); update();
return false; return service.errorMessage;
} }
Future getAnAssistant({required final int id}) async { Future getAnAssistant({required final int id}) async {

View File

@ -139,11 +139,11 @@ class _HomeState extends State<Home>
state.tabController = _tabController; state.tabController = _tabController;
_tabController.addListener(() { _tabController.addListener(() {
state.currentPageIndex = _tabController.index; state.currentPageIndex = _tabController.index;
// if (_tabController.index == 2) { if (_tabController.index == 2) {
// homeScaffKey.currentState!.closeDrawer(); final state = context.read<HistoryAiChatState>();
// context.read<HistoryAiChatState>().getBots(); state.getBots();
// } }
}); });
if (!kIsWeb) { if (!kIsWeb) {
Future.delayed(Duration.zero, () { Future.delayed(Duration.zero, () {