2025-12-03 19:29:37 +00:00
package services
import (
"context"
"fmt"
"github.com/google/uuid" // Added import for uuid
"go.uber.org/zap"
"gorm.io/gorm"
"veza-backend-api/internal/models"
)
// TrackUploadService gère le suivi de progression des uploads de tracks
type TrackUploadService struct {
db * gorm . DB
logger * zap . Logger
}
// NewTrackUploadService crée un nouveau service de suivi d'upload
func NewTrackUploadService ( db * gorm . DB , logger * zap . Logger ) * TrackUploadService {
if logger == nil {
logger = zap . NewNop ( )
}
return & TrackUploadService {
db : db ,
logger : logger ,
}
}
// GetUploadProgress récupère la progression d'un upload de track
func ( s * TrackUploadService ) GetUploadProgress ( ctx context . Context , trackID uuid . UUID ) ( * models . UploadProgress , error ) { // Changed trackID to uuid.UUID
var track models . Track
if err := s . db . WithContext ( ctx ) . First ( & track , "id = ?" , trackID ) . Error ; err != nil { // Updated query
if err == gorm . ErrRecordNotFound {
return nil , fmt . Errorf ( "track not found" )
}
return nil , fmt . Errorf ( "failed to get track: %w" , err )
}
// Calculer le pourcentage de progression basé sur le statut
progress := s . calculateProgress ( track . Status )
return & models . UploadProgress {
TrackID : trackID ,
Status : track . Status ,
Progress : progress ,
Message : track . StatusMessage ,
StreamStatus : track . StreamStatus ,
StreamManifestURL : track . StreamManifestURL ,
} , nil
}
2025-12-06 16:21:59 +00:00
2025-12-03 19:29:37 +00:00
// UpdateUploadStatus met à jour le statut d'un track
func ( s * TrackUploadService ) UpdateUploadStatus ( ctx context . Context , trackID uuid . UUID , status models . TrackStatus , message string ) error { // Changed trackID to uuid.UUID
updates := map [ string ] interface { } {
"status" : status ,
}
if message != "" {
updates [ "status_message" ] = message
}
if err := s . db . WithContext ( ctx ) . Model ( & models . Track { } ) . Where ( "id = ?" , trackID ) . Updates ( updates ) . Error ; err != nil {
return fmt . Errorf ( "failed to update status: %w" , err )
}
s . logger . Info ( "Track upload status updated" ,
zap . Any ( "track_id" , trackID ) , // Changed to zap.Any for uuid.UUID
zap . String ( "status" , string ( status ) ) ,
zap . String ( "message" , message ) ,
)
return nil
}
2025-12-06 16:21:59 +00:00
2025-12-03 19:29:37 +00:00
// calculateProgress calcule le pourcentage de progression basé sur le statut
func ( s * TrackUploadService ) calculateProgress ( status models . TrackStatus ) int {
switch status {
case models . TrackStatusUploading :
return 25 // 25% pendant l'upload
case models . TrackStatusProcessing :
return 50 // 50% pendant le traitement
case models . TrackStatusCompleted :
return 100 // 100% une fois terminé
case models . TrackStatusFailed :
return 0 // 0% en cas d'échec
default :
return 0
}
}