add imgPath

This commit is contained in:
Valentin CZERYBA 2024-09-12 23:54:59 +02:00
parent 4f12fdf8b3
commit 6fd00ddd98

View File

@ -5,6 +5,7 @@ import 'package:intl/intl.dart';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import '../classes/alert.dart'; import '../classes/alert.dart';
import '../classes/eventAdded.dart'; import '../classes/eventAdded.dart';
@ -116,73 +117,95 @@ class _UpdateeventImageState extends State<UpdateeventImage>
List<String> send = ["toto"]; List<String> send = ["toto"];
if (accessToken.isNotEmpty) { if (accessToken.isNotEmpty) {
try { try {
//String credentials = "${pseudo}:${password}"; final params = {
//Codec<String, String> stringToBase64 = utf8.fuse(base64); 'expiration': '600',
//String encoded = stringToBase64.encode(credentials); 'key': '87a91f7e54516808dda2c4feffbd5287',
var urlPut = Uri.parse("${globals.api}/events"); };
var responsePut = await http.put(urlPut,
headers: { final urlPost = Uri.parse('https://api.imgbb.com/1/upload')
HttpHeaders.cookieHeader: 'access_token=${accessToken}', .replace(queryParameters: params);
HttpHeaders.acceptHeader: 'application/json, text/plain, */*', File image = File(widget.imagePath);
HttpHeaders.contentTypeHeader: 'application/json' Uint8List _bytes = await image.readAsBytes();
}, String _base64String = base64.encode(_bytes);
body: jsonEncode({
'name': name, final req = http.MultipartRequest('POST', url)
'place': place, ..fields['image'] = _base64String;
'start_date': startDate,
'end_date': endDate, final stream = await req.send();
'zip_code': zipCode, final res = await http.Response.fromStream(stream);
'country': country,
'city': city, final status = res.statusCode;
'organizers': send, if (status == 200) {
'latitude': '0.0', //String credentials = "${pseudo}:${password}";
'longitude': '0.0', //Codec<String, String> stringToBase64 = utf8.fuse(base64);
'description': description //String encoded = stringToBase64.encode(credentials);
})); var urlPut = Uri.parse("${globals.api}/events");
print(responsePut.statusCode); var responsePut = await http.put(urlPut,
if ((responsePut.statusCode == 200) || headers: {
(responsePut.statusCode == 201)) { HttpHeaders.cookieHeader: 'access_token=${accessToken}',
showEventDialog(context, "Evenement ${name} ajoute"); HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
} else { HttpHeaders.contentTypeHeader: 'application/json'
var text = ""; },
switch (responsePut.statusCode) { body: jsonEncode({
case 400: 'name': name,
{ 'place': place,
text = "Requête mal construite"; 'start_date': startDate,
} 'end_date': endDate,
break; 'zip_code': zipCode,
case 406: 'country': country,
{ 'city': city,
text = "Mot de passe incorrect"; 'organizers': send,
} 'latitude': '0.0',
break; 'longitude': '0.0',
case 404: 'description': description
{ }));
text = "Utilisateur inconnu"; print(responsePut.statusCode);
} if ((responsePut.statusCode == 200) ||
break; (responsePut.statusCode == 201)) {
case 403: showEventDialog(context, "Evenement ${name} ajoute");
{ } else {
text = "Utilisateur desactive"; var text = "";
} switch (responsePut.statusCode) {
break; case 400:
case 410: {
{ text = "Requête mal construite";
text = "Token invalide"; }
} break;
break; case 406:
case 500: {
{ text = "Mot de passe incorrect";
text = "Probleme interne du serveur"; }
} break;
break; case 404:
default: {
{ text = "Utilisateur inconnu";
text = "Probleme d'authentification inconnu"; }
} break;
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) { } catch (e) {
showErrorDialog(context, "${e}"); showErrorDialog(context, "${e}");