Tags añadidos

This commit is contained in:
Abraham
2026-01-12 18:40:28 -08:00
parent a9214e9eac
commit d1271a5578
4 changed files with 305 additions and 1 deletions

View File

@@ -144,6 +144,7 @@ class VideosProvider extends ChangeNotifier {
: '-'),
'createdAt': PlutoCell(
value: media.createdAt?.toString().split('.')[0] ?? '-'),
'tags': PlutoCell(value: media.tags.join(', ')),
'actions': PlutoCell(value: media),
},
),
@@ -246,6 +247,7 @@ class VideosProvider extends ChangeNotifier {
String? description,
int? categoryId,
int? durationSeconds,
List<String>? tags,
}) async {
if (webVideoBytes == null || videoName == null) {
errorMessage = 'No hay video seleccionado';
@@ -291,6 +293,7 @@ class VideosProvider extends ChangeNotifier {
'file_size_bytes': webVideoBytes!.length, // Peso del video
if (posterUrlUploaded != null) 'poster_url': posterUrlUploaded,
if (posterUrlUploaded != null) 'poster_file_name': posterName,
if (tags != null && tags.isNotEmpty) 'tags': tags,
};
await supabaseML.from('media_files').insert({
@@ -483,6 +486,102 @@ class VideosProvider extends ChangeNotifier {
}
}
/// Update video tags
Future<bool> updateVideoTags(int mediaFileId, List<String> tags) async {
try {
// Get current metadata
final response = await supabaseML
.from('media_files')
.select('metadata_json')
.eq('media_file_id', mediaFileId)
.eq('organization_fk', organizationId)
.single();
final metadata = response['metadata_json'] as Map<String, dynamic>? ?? {};
metadata['tags'] = tags;
await updateVideoMetadata(mediaFileId, metadata);
return true;
} catch (e) {
errorMessage = 'Error actualizando tags: $e';
notifyListeners();
print('Error en updateVideoTags: $e');
return false;
}
}
/// Update video poster
Future<bool> updateVideoPoster(
int mediaFileId, Uint8List posterBytes, String posterName) async {
try {
isLoading = true;
notifyListeners();
// Upload new poster to storage
final timestamp = DateTime.now().millisecondsSinceEpoch;
final fileName = '${timestamp}_$posterName';
final posterPath = 'imagenes/$fileName';
await supabaseML.storage.from('energymedia').uploadBinary(
posterPath,
posterBytes,
fileOptions: const FileOptions(
cacheControl: '3600',
upsert: false,
),
);
// Get public URL
final newPosterUrl =
supabaseML.storage.from('energymedia').getPublicUrl(posterPath);
// Get current metadata and old poster URL
final response = await supabaseML
.from('media_files')
.select('metadata_json')
.eq('media_file_id', mediaFileId)
.eq('organization_fk', organizationId)
.single();
final metadata = response['metadata_json'] as Map<String, dynamic>? ?? {};
final oldPosterUrl = metadata['poster_url'] as String?;
// Delete old poster from storage if exists
if (oldPosterUrl != null && oldPosterUrl.isNotEmpty) {
try {
final uri = Uri.parse(oldPosterUrl);
final pathSegments = uri.pathSegments;
final bucketIndex = pathSegments.indexOf('energymedia');
if (bucketIndex != -1 && bucketIndex < pathSegments.length - 1) {
final oldPosterPath =
pathSegments.sublist(bucketIndex + 1).join('/');
await supabaseML.storage
.from('energymedia')
.remove([oldPosterPath]);
}
} catch (e) {
print('Error eliminando poster antiguo: $e');
}
}
// Update metadata with new poster info
metadata['poster_url'] = newPosterUrl;
metadata['poster_file_name'] = posterName;
await updateVideoMetadata(mediaFileId, metadata);
isLoading = false;
notifyListeners();
return true;
} catch (e) {
errorMessage = 'Error actualizando poster: $e';
isLoading = false;
notifyListeners();
print('Error en updateVideoPoster: $e');
return false;
}
}
// ========== DELETE METHODS ==========
/// Delete video and its storage files
@@ -645,7 +744,7 @@ class VideosProvider extends ChangeNotifier {
'video': PlutoCell(value: media),
'thumbnail': PlutoCell(value: media.fileUrl),
'title': PlutoCell(value: media.title ?? media.fileName),
'fileName': PlutoCell(value: media.fileName),
'file_description': PlutoCell(value: media.fileDescription),
'category':
PlutoCell(value: _getCategoryName(media.mediaCategoryFk)),
'reproducciones': PlutoCell(value: media.reproducciones),
@@ -655,6 +754,7 @@ class VideosProvider extends ChangeNotifier {
: '-'),
'createdAt': PlutoCell(
value: media.createdAt?.toString().split('.')[0] ?? '-'),
'tags': PlutoCell(value: media.tags.join(', ')),
'actions': PlutoCell(value: media),
},
),