Feature
ein paar feature aber datenbank macht probleme wenn man aufträge speichern möchge
This commit is contained in:
@@ -1,38 +1,81 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:appwrite/appwrite.dart';
|
||||
|
||||
import '../appwrite_config.dart';
|
||||
|
||||
class AuftragStorageService {
|
||||
AuftragStorageService({FirebaseStorage? storage, FirebaseAuth? auth})
|
||||
: _storage = storage ?? FirebaseStorage.instance,
|
||||
_auth = auth ?? FirebaseAuth.instance;
|
||||
AuftragStorageService({Client? client})
|
||||
: _storage = Storage(client ?? appwriteClient),
|
||||
_account = Account(client ?? appwriteClient);
|
||||
|
||||
final FirebaseStorage _storage;
|
||||
final FirebaseAuth _auth;
|
||||
final Storage _storage;
|
||||
final Account _account;
|
||||
|
||||
Reference _basis(String auftragId) {
|
||||
final uid = _auth.currentUser?.uid;
|
||||
if (uid == null) throw StateError('Nicht angemeldet');
|
||||
return _storage.ref('users/$uid/auftraege/$auftragId');
|
||||
Future<String> _uid() async {
|
||||
final u = await _account.get();
|
||||
return u.$id;
|
||||
}
|
||||
|
||||
Future<String> hochladenFoto(String auftragId, Uint8List bytes, String dateiname) async {
|
||||
final ref = _basis(auftragId).child('fotos/$dateiname');
|
||||
await ref.putData(
|
||||
bytes,
|
||||
SettableMetadata(contentType: 'image/jpeg'),
|
||||
List<String> _filePermissions(String uid) => [
|
||||
Permission.read(Role.user(uid)),
|
||||
Permission.update(Role.user(uid)),
|
||||
Permission.delete(Role.user(uid)),
|
||||
];
|
||||
|
||||
Future<String> hochladenFoto(
|
||||
String auftragId,
|
||||
Uint8List bytes,
|
||||
String dateiname,
|
||||
) async {
|
||||
final uid = await _uid();
|
||||
final fileId = ID.unique();
|
||||
await _storage.createFile(
|
||||
bucketId: kAppwriteBucketId,
|
||||
fileId: fileId,
|
||||
file: InputFile.fromBytes(
|
||||
bytes: bytes,
|
||||
filename: dateiname,
|
||||
contentType: 'image/jpeg',
|
||||
),
|
||||
permissions: _filePermissions(uid),
|
||||
);
|
||||
return ref.getDownloadURL();
|
||||
return fileId;
|
||||
}
|
||||
|
||||
Future<String> hochladenUnterschrift(String auftragId, Uint8List pngBytes) async {
|
||||
final ref = _basis(auftragId).child('unterschrift.png');
|
||||
await ref.putData(
|
||||
pngBytes,
|
||||
SettableMetadata(contentType: 'image/png'),
|
||||
final uid = await _uid();
|
||||
final fileId = ID.unique();
|
||||
await _storage.createFile(
|
||||
bucketId: kAppwriteBucketId,
|
||||
fileId: fileId,
|
||||
file: InputFile.fromBytes(
|
||||
bytes: pngBytes,
|
||||
filename: 'unterschrift_$auftragId.png',
|
||||
contentType: 'image/png',
|
||||
),
|
||||
permissions: _filePermissions(uid),
|
||||
);
|
||||
return ref.getDownloadURL();
|
||||
return fileId;
|
||||
}
|
||||
|
||||
Future<Uint8List> getFileBytes(String fileId) async {
|
||||
return _storage.getFileView(
|
||||
bucketId: kAppwriteBucketId,
|
||||
fileId: fileId,
|
||||
);
|
||||
}
|
||||
|
||||
/// Entfernt die Datei im Bucket (kein Fehler bei HTTP-URLs oder fehlenden Rechten).
|
||||
Future<void> loescheDatei(String fileId) async {
|
||||
if (fileId.startsWith('http://') || fileId.startsWith('https://')) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await _storage.deleteFile(
|
||||
bucketId: kAppwriteBucketId,
|
||||
fileId: fileId,
|
||||
);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user