@@ -11,9 +11,14 @@ import (
1111
1212 "github.com/aws/aws-sdk-go-v2/aws"
1313 "github.com/aws/aws-sdk-go-v2/service/s3"
14+ "gitlab.com/subrotokumar/playstack/libs/db"
1415 "gitlab.com/subrotokumar/playstack/transcoder/ffmpeg"
1516)
1617
18+ const (
19+ MsgVideoMetadataUpdateFailed string = "failed to update video metadata"
20+ )
21+
1722func (s * Service ) Download (ctx context.Context , destPath string ) error {
1823 s .log .Info ("Downloading file" , "path" , destPath )
1924
@@ -64,10 +69,7 @@ func (s *Service) Transcode(ctx context.Context, inputPath, outputDir string) er
6469
6570func (s * Service ) Upload (ctx context.Context , sourceDir string ) error {
6671 s .log .Info ("Uploading files from" , "dir" , sourceDir )
67- keys := strings .Split (s .cfg .Key (), "/" )
68- userId := keys [0 ]
69- videoId := keys [1 ]
70- uploadKey := userId + "/" + videoId + "/" + "output/"
72+ uploadKey := strings .ReplaceAll (s .cfg .Key (), "video.mp4" , "output/" )
7173 err := filepath .Walk (sourceDir , func (path string , info os.FileInfo , err error ) error {
7274 if err != nil {
7375 return err
@@ -89,7 +91,7 @@ func (s *Service) Upload(ctx context.Context, sourceDir string) error {
8991
9092 s .log .Info ("Uploading" , "key" , uploadKey + relPath )
9193 _ , err = s .storage .Client ().PutObject (ctx , & s3.PutObjectInput {
92- Bucket : aws .String (s .cfg .Bucket () ),
94+ Bucket : aws .String (s .cfg .Aws . MediaBucket ),
9395 Key : aws .String (uploadKey + relPath ),
9496 Body : file ,
9597 })
@@ -107,6 +109,10 @@ func (s *Service) Upload(ctx context.Context, sourceDir string) error {
107109}
108110
109111func (s * Service ) Process (ctx context.Context ) error {
112+ if err := s .UpdateMetadata (ctx , UpdateMetadataRequest {Status : db .VideoStatusUPLOADED }); err != nil {
113+ s .log .Error (MsgVideoMetadataUpdateFailed , "err" , err .Error ())
114+ }
115+
110116 workDir := "./tmp/workspace"
111117 if err := os .MkdirAll (workDir , 0o755 ); err != nil {
112118 return fmt .Errorf ("create work dir: %w" , err )
@@ -131,15 +137,22 @@ func (s *Service) Process(ctx context.Context) error {
131137 return fmt .Errorf ("download video: %w" , err )
132138 }
133139
140+ if err := s .UpdateMetadata (ctx , UpdateMetadataRequest {Status : db .VideoStatusPROCESSING }); err != nil {
141+ s .log .Error (MsgVideoMetadataUpdateFailed , "err" , err .Error ())
142+ }
143+
134144 if err := s .Transcode (ctx , inputPath , outputPath ); err != nil {
145+ s .UpdateMetadata (ctx , UpdateMetadataRequest {Status : db .VideoStatusFAILED })
135146 return fmt .Errorf ("transcode video: %w" , err )
136147 }
137148
138149 if err := s .Upload (ctx , outputPath ); err != nil {
150+ s .UpdateMetadata (ctx , UpdateMetadataRequest {Status : db .VideoStatusFAILED })
139151 return fmt .Errorf ("upload files: %w" , err )
140152 }
141- if err := s .UpdateMetadata (ctx ); err != nil {
142- return fmt .Errorf ("update video: %w" , err )
153+ if err := s .UpdateMetadata (ctx , UpdateMetadataRequest {Status : db .VideoStatusREADY }); err != nil {
154+ s .log .Error (MsgVideoMetadataUpdateFailed , "err" , err .Error ())
155+ return err
143156 }
144157 return nil
145158}
0 commit comments