Merge pull request 'feature/upload-image' (#8) from feature/upload-image into main

Reviewed-on: #8
This commit is contained in:
v4l3n71n 2024-09-17 23:51:42 +02:00
commit 5e0d3ab72e
2 changed files with 122 additions and 76 deletions

View File

@ -69,6 +69,7 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
String eventStartDate = "";
String organizers = "";
String place = "";
String imgUrl = "";
Events? events;
@override
@ -85,6 +86,7 @@ class _ItemMenuState extends State<ItemMenu> 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<ItemMenu> 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<ItemMenu> with ShowErrorDialog {
eventStartDate = formerDate;
organizers = formerOrga;
place = formerMap;
imgUrl = formerImage;
});
}
@ -201,8 +209,7 @@ class _ItemMenuState extends State<ItemMenu> 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(

View File

@ -2,9 +2,11 @@ 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';
import 'dart:typed_data';
import '../classes/alert.dart';
import '../classes/eventAdded.dart';
@ -86,6 +88,13 @@ class _UpdateeventImageState extends State<UpdateeventImage>
}
}
convertNulltoEmptyString(var check) {
if (check == null) {
return "";
}
return check;
}
String formatDate(String date) {
var splitedDate = date.split("-");
var day = splitedDate[0];
@ -116,73 +125,101 @@ class _UpdateeventImageState extends State<UpdateeventImage>
List<String> send = ["toto"];
if (accessToken.isNotEmpty) {
try {
//String credentials = "${pseudo}:${password}";
//Codec<String, String> 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;
await dotenv.load();
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.imagePath);
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) {
var body = json.decode(utf8.decode(res.bodyBytes));
String imgUrl = body["data"]["url"];
//String credentials = "${pseudo}:${password}";
//Codec<String, String> 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,
"imgUrl": imgUrl
}));
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}");
@ -193,15 +230,17 @@ class _UpdateeventImageState extends State<UpdateeventImage>
}
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);