From 6fd00ddd981e3d1d8d0a8fff519842291016de60 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 12 Sep 2024 23:54:59 +0200 Subject: [PATCH 1/3] add imgPath --- covas_mobile/lib/pages/UpdateEventImage.dart | 155 +++++++++++-------- 1 file changed, 89 insertions(+), 66 deletions(-) diff --git a/covas_mobile/lib/pages/UpdateEventImage.dart b/covas_mobile/lib/pages/UpdateEventImage.dart index d770ab3..3b39a72 100644 --- a/covas_mobile/lib/pages/UpdateEventImage.dart +++ b/covas_mobile/lib/pages/UpdateEventImage.dart @@ -5,6 +5,7 @@ import 'package:intl/intl.dart'; import 'dart:convert'; import 'dart:io'; +import 'dart:typed_data'; import '../classes/alert.dart'; import '../classes/eventAdded.dart'; @@ -116,73 +117,95 @@ class _UpdateeventImageState extends State List send = ["toto"]; if (accessToken.isNotEmpty) { try { - //String credentials = "${pseudo}:${password}"; - //Codec stringToBase64 = utf8.fuse(base64); - //String encoded = stringToBase64.encode(credentials); - var urlPut = Uri.parse("${globals.api}/events"); - var responsePut = await http.put(urlPut, - headers: { - HttpHeaders.cookieHeader: 'access_token=${accessToken}', - HttpHeaders.acceptHeader: 'application/json, text/plain, */*', - HttpHeaders.contentTypeHeader: 'application/json' - }, - body: jsonEncode({ - 'name': name, - 'place': place, - 'start_date': startDate, - 'end_date': endDate, - 'zip_code': zipCode, - 'country': country, - 'city': city, - 'organizers': send, - 'latitude': '0.0', - 'longitude': '0.0', - 'description': description - })); - print(responsePut.statusCode); - if ((responsePut.statusCode == 200) || - (responsePut.statusCode == 201)) { - showEventDialog(context, "Evenement ${name} ajoute"); - } else { - var text = ""; - switch (responsePut.statusCode) { - case 400: - { - text = "Requête mal construite"; - } - break; - case 406: - { - text = "Mot de passe incorrect"; - } - break; - case 404: - { - text = "Utilisateur inconnu"; - } - break; - case 403: - { - text = "Utilisateur desactive"; - } - break; - case 410: - { - text = "Token invalide"; - } - break; - case 500: - { - text = "Probleme interne du serveur"; - } - break; - default: - { - text = "Probleme d'authentification inconnu"; - } - break; + final params = { + 'expiration': '600', + 'key': '87a91f7e54516808dda2c4feffbd5287', + }; + + final urlPost = Uri.parse('https://api.imgbb.com/1/upload') + .replace(queryParameters: params); + File image = File(widget.imagePath); + Uint8List _bytes = await image.readAsBytes(); + String _base64String = base64.encode(_bytes); + + final req = http.MultipartRequest('POST', url) + ..fields['image'] = _base64String; + + final stream = await req.send(); + final res = await http.Response.fromStream(stream); + + final status = res.statusCode; + if (status == 200) { + //String credentials = "${pseudo}:${password}"; + //Codec stringToBase64 = utf8.fuse(base64); + //String encoded = stringToBase64.encode(credentials); + var urlPut = Uri.parse("${globals.api}/events"); + var responsePut = await http.put(urlPut, + headers: { + HttpHeaders.cookieHeader: 'access_token=${accessToken}', + HttpHeaders.acceptHeader: 'application/json, text/plain, */*', + HttpHeaders.contentTypeHeader: 'application/json' + }, + body: jsonEncode({ + 'name': name, + 'place': place, + 'start_date': startDate, + 'end_date': endDate, + 'zip_code': zipCode, + 'country': country, + 'city': city, + 'organizers': send, + 'latitude': '0.0', + 'longitude': '0.0', + 'description': description + })); + print(responsePut.statusCode); + if ((responsePut.statusCode == 200) || + (responsePut.statusCode == 201)) { + showEventDialog(context, "Evenement ${name} ajoute"); + } else { + var text = ""; + switch (responsePut.statusCode) { + case 400: + { + text = "Requête mal construite"; + } + break; + case 406: + { + text = "Mot de passe incorrect"; + } + break; + case 404: + { + text = "Utilisateur inconnu"; + } + break; + case 403: + { + text = "Utilisateur desactive"; + } + break; + case 410: + { + text = "Token invalide"; + } + break; + case 500: + { + text = "Probleme interne du serveur"; + } + break; + default: + { + text = "Probleme d'authentification inconnu"; + } + break; + } + showErrorDialog(context, text); } - showErrorDialog(context, text); + } else { + print("imgbb error : ${status}"); } } catch (e) { showErrorDialog(context, "${e}"); From 54247f8c565950cccd77a18787af78664e5747b7 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 17 Sep 2024 23:09:35 +0200 Subject: [PATCH 2/3] request http imgbb --- covas_mobile/lib/pages/UpdateEventImage.dart | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/covas_mobile/lib/pages/UpdateEventImage.dart b/covas_mobile/lib/pages/UpdateEventImage.dart index 3b39a72..f6a4c7d 100644 --- a/covas_mobile/lib/pages/UpdateEventImage.dart +++ b/covas_mobile/lib/pages/UpdateEventImage.dart @@ -121,21 +121,25 @@ class _UpdateeventImageState extends State 'expiration': '600', 'key': '87a91f7e54516808dda2c4feffbd5287', }; - + print("Post Img"); final urlPost = Uri.parse('https://api.imgbb.com/1/upload') .replace(queryParameters: params); File image = File(widget.imagePath); Uint8List _bytes = await image.readAsBytes(); String _base64String = base64.encode(_bytes); - final req = http.MultipartRequest('POST', url) + 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) { + var body = json.decode(utf8.decode(res.bodyBytes)); + String imgUrl = body["data"]["url"]; + //String credentials = "${pseudo}:${password}"; //Codec stringToBase64 = utf8.fuse(base64); //String encoded = stringToBase64.encode(credentials); @@ -157,7 +161,8 @@ class _UpdateeventImageState extends State 'organizers': send, 'latitude': '0.0', 'longitude': '0.0', - 'description': description + 'description': description, + "imgUrl": imgUrl })); print(responsePut.statusCode); if ((responsePut.statusCode == 200) || From 59bec53d9e26e75d4415189901839ecf1fefc473 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 17 Sep 2024 23:50:23 +0200 Subject: [PATCH 3/3] display event with image --- covas_mobile/lib/pages/ItemMenu.dart | 11 +++++-- covas_mobile/lib/pages/UpdateEventImage.dart | 31 +++++++++++++------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/covas_mobile/lib/pages/ItemMenu.dart b/covas_mobile/lib/pages/ItemMenu.dart index e42162e..77710e2 100644 --- a/covas_mobile/lib/pages/ItemMenu.dart +++ b/covas_mobile/lib/pages/ItemMenu.dart @@ -69,6 +69,7 @@ class _ItemMenuState extends State with ShowErrorDialog { String eventStartDate = ""; String organizers = ""; String place = ""; + String imgUrl = ""; Events? events; @override @@ -85,6 +86,7 @@ class _ItemMenuState extends State with ShowErrorDialog { String formerDate = ""; String formerOrga = ""; String formerMap = ""; + String formerImage = ""; if (accessToken.isNotEmpty) { var urlGet = Uri.parse("${globals.api}/events/${widget.title}"); @@ -102,8 +104,13 @@ class _ItemMenuState extends State with ShowErrorDialog { final time = DateFormat.Hm().format(startDate); final endDate = DateTime.parse(events["end_date"]); + final dateE = DateFormat.yMd().format(endDate); final timeE = DateFormat.Hm().format(endDate); + if (events["imgUrl"] != null) { + formerImage = events["imgUrl"]; + } + formerDate = "${date} ${time} à ${dateE} ${timeE}"; if (events["organizers"].length > 1) { formerOrga = "${events['organizers'][0]}"; @@ -168,6 +175,7 @@ class _ItemMenuState extends State with ShowErrorDialog { eventStartDate = formerDate; organizers = formerOrga; place = formerMap; + imgUrl = formerImage; }); } @@ -201,8 +209,7 @@ class _ItemMenuState extends State with ShowErrorDialog { Padding( padding: const EdgeInsets.only(top: 60.0), child: Center( - child: Container( - height: 250, child: Image.asset('images/flutter.png')), + child: Container(height: 250, child: Image.network(imgUrl)), ), ), Row( diff --git a/covas_mobile/lib/pages/UpdateEventImage.dart b/covas_mobile/lib/pages/UpdateEventImage.dart index f6a4c7d..3ee5d9c 100644 --- a/covas_mobile/lib/pages/UpdateEventImage.dart +++ b/covas_mobile/lib/pages/UpdateEventImage.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'package:intl/intl.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'dart:convert'; import 'dart:io'; @@ -87,6 +88,13 @@ class _UpdateeventImageState extends State } } + convertNulltoEmptyString(var check) { + if (check == null) { + return ""; + } + return check; + } + String formatDate(String date) { var splitedDate = date.split("-"); var day = splitedDate[0]; @@ -117,9 +125,10 @@ class _UpdateeventImageState extends State List send = ["toto"]; if (accessToken.isNotEmpty) { try { + await dotenv.load(); final params = { - 'expiration': '600', - 'key': '87a91f7e54516808dda2c4feffbd5287', + 'expiration': '15552000', + 'key': dotenv.env["IMGBB_API_KEY"], }; print("Post Img"); final urlPost = Uri.parse('https://api.imgbb.com/1/upload') @@ -221,15 +230,17 @@ class _UpdateeventImageState extends State } void start() async { - inputName.text = widget.events["name"]; - inputCity.text = widget.events["city"]; - inputAddress.text = widget.events["address"]; - inputZipCode.text = widget.events["zip_code"]; - inputCountry.text = widget.events["country"]; - inputDesc.text = widget.events["description"]; + inputName.text = convertNulltoEmptyString(widget.events["name"]); + inputCity.text = convertNulltoEmptyString(widget.events["city"]); + inputAddress.text = convertNulltoEmptyString(widget.events["address"]); + inputZipCode.text = convertNulltoEmptyString(widget.events["zip_code"]); + inputCountry.text = convertNulltoEmptyString(widget.events["country"]); + inputDesc.text = convertNulltoEmptyString(widget.events["description"]); - DateTime pickedStartDate = DateTime.parse(widget.events["start_date"]); - DateTime pickedEndDate = DateTime.parse(widget.events["end_date"]); + DateTime pickedStartDate = + DateTime.parse(convertNulltoEmptyString(widget.events["start_date"])); + DateTime pickedEndDate = + DateTime.parse(convertNulltoEmptyString(widget.events["end_date"])); startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedStartDate); endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedEndDate);