40 lines
1.2 KiB
Dart
40 lines
1.2 KiB
Dart
import 'dart:io';
|
|
import 'package:firebase_core/firebase_core.dart' as firebase_core;
|
|
import 'package:uuid/uuid.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:firebase_storage/firebase_storage.dart';
|
|
|
|
class ImageService {
|
|
final String baseUrl = 'https://group-10-15.pvt.dsv.su.se';
|
|
//final String baseUrl = 'http://localhost:8080';
|
|
|
|
Future upload(XFile? image) async {
|
|
if (image == null) {
|
|
return;
|
|
}
|
|
|
|
File file = File(image.path);
|
|
final String filename = image.name;
|
|
|
|
// unikt filnamn
|
|
var uuid = Uuid();
|
|
var uuidV4 = uuid.v4();
|
|
final String filenameUnique =
|
|
'users_media/$uuidV4/$filename'; // Denna behöver fixas med datbasen! Bilderna som läggs upp måste deras unika kod kopplas till datbasen
|
|
|
|
final storage = FirebaseStorage.instance;
|
|
final storageRef = storage.ref();
|
|
final mediaRef = storageRef.child(filenameUnique);
|
|
|
|
// use filename
|
|
// add fle to Firebase storage
|
|
try {
|
|
await mediaRef.putFile(file);
|
|
} on firebase_core.FirebaseException catch (e) {
|
|
print('Failed saving file to Firebase storage: ${e.code}');
|
|
print(e.message);
|
|
}
|
|
}
|
|
}
|