fix supabase storage
This commit is contained in:
@@ -83,10 +83,7 @@ class ServicesRepository {
|
||||
}
|
||||
|
||||
// --- SALVATAGGIO COMPLETO (PRIMA PADRE, POI FIGLI) ---
|
||||
Future<void> saveFullService(
|
||||
ServiceModel service,
|
||||
List<ServiceFileModel> files,
|
||||
) async {
|
||||
Future<void> saveFullService(ServiceModel service) async {
|
||||
try {
|
||||
// 1. Upsert del record principale
|
||||
final serviceData = await _supabase
|
||||
@@ -153,16 +150,16 @@ class ServicesRepository {
|
||||
if (insertTasks.isNotEmpty) {
|
||||
await Future.wait(insertTasks);
|
||||
}
|
||||
if (files.isNotEmpty) {
|
||||
if (service.files.isNotEmpty) {
|
||||
final List<Future> uploadTasks = [];
|
||||
|
||||
for (var file in files) {
|
||||
for (var file in service.files) {
|
||||
final storagePath =
|
||||
'$companyId/services/$newId/${DateTime.now().millisecondsSinceEpoch}_${file.name}.${file.extension}';
|
||||
final String mimeType = file.extension.toLowerCase() == 'pdf'
|
||||
? 'application/pdf'
|
||||
: 'image/${file.extension}';
|
||||
final fileToSave = file.copyWith(serviceId: newId);
|
||||
final fileToSave = file.copyWith(serviceId: newId, url: storagePath);
|
||||
|
||||
// Creiamo una funzione asincrona per caricare file e scrivere nel DB
|
||||
Future<void> uploadAndLink() async {
|
||||
@@ -182,13 +179,7 @@ class ServicesRepository {
|
||||
),
|
||||
);
|
||||
|
||||
// B. Otteniamo l'URL pubblico e scriviamo il record del file nel DB
|
||||
final String publicUrl = _supabase.storage
|
||||
.from('documents')
|
||||
.getPublicUrl(storagePath);
|
||||
await _supabase
|
||||
.from('service_file')
|
||||
.insert(fileToSave.copyWith(url: publicUrl).toMap());
|
||||
await _supabase.from('service_file').insert(fileToSave.toMap());
|
||||
}
|
||||
|
||||
uploadTasks.add(uploadAndLink());
|
||||
|
||||
Reference in New Issue
Block a user