`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 StudioDetailsData? currentPodcast;
static StudioRequestArgs? podcastPlaylistArgs;
static int maxSizeInBytes = 15 * 1024 * 1024;
static Duration? get duration => audioPlayer.duration;

View File

@ -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(

View File

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

View File

@ -106,14 +106,13 @@ class _CreateBotAssistantsPageState extends State<CreateBotAssistantsPage> {
? state.assistant!.image == null
: state.image.value == null,
image: state.image.value));
if (success) {
if (success == null) {
context.read<BotAssistantsState>().getMyAssissmant();
context.read<CreateBotAssistantsState>().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<CreateBotAssistantsPage> {
),
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(
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<CreateBotAssistantsPage> {
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,

View File

@ -62,7 +62,7 @@ class CreateBotAssistantsState extends CoreProvier {
update();
}
Future<bool> createAssistants(
Future<String?> 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 {

View File

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