From ea3d5ef8de5a90517851485affc00d38bb08ee48 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 15 Dec 2024 20:10:19 +0100 Subject: [PATCH] =?UTF-8?q?add=20post=20image=20in=20edit=20event=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- covas_mobile/lib/pages/EditEvent.dart | 33 ++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/covas_mobile/lib/pages/EditEvent.dart b/covas_mobile/lib/pages/EditEvent.dart index 0ebb8cb..fedc2bb 100644 --- a/covas_mobile/lib/pages/EditEvent.dart +++ b/covas_mobile/lib/pages/EditEvent.dart @@ -8,6 +8,8 @@ import 'package:textfield_tags/textfield_tags.dart'; import 'dart:convert'; import 'dart:io'; + +import 'dart:typed_data'; import '../classes/events.dart'; import 'ItemMenu.dart'; @@ -206,12 +208,41 @@ class _EditEventState extends State if (responseGet.statusCode == 200) { var events = jsonDecode(utf8.decode(responseGet.bodyBytes)); if (events.length > 0) { - showErrorDialog(context, "Evenement deja existant"); + if (events[0]["id"] != widget.events!.id) { + showErrorDialog(context, "Evenement deja existant"); + } return; } } + if (widget.imgPath.isNotEmpty) { + final params = { + 'expiration': '15552000', + 'key': dotenv.env["IMGBB_API_KEY"], + }; + print("Post Img"); + final urlPost = Uri.parse('https://api.imgbb.com/1/upload') + .replace(queryParameters: params); + File image = File(widget.imgPath); + Uint8List _bytes = await image.readAsBytes(); + String _base64String = base64.encode(_bytes); + + final req = http.MultipartRequest('POST', urlPost) + ..fields['image'] = _base64String; + + final stream = await req.send(); + final res = await http.Response.fromStream(stream); + + final status = res.statusCode; + print("code status imgbb ${status}"); + if (status != 200) { + showErrorDialog(context, "Image non posté"); + return; + } + var body = json.decode(utf8.decode(res.bodyBytes)); + imgUrl = body["data"]["url"]; + } var urlPut = Uri.parse("${globals.api}/events/${widget.events!.id}"); var responsePut = await http.put(urlPut,