Skip to content

Commit

Permalink
Merge pull request #104 from bcc-code/feat/paths
Browse files Browse the repository at this point in the history
feat(paths): change hard paths into paths.Path
  • Loading branch information
fredrikvedvik authored Nov 9, 2023
2 parents 982cf13 + 162e9f0 commit e88bbcf
Show file tree
Hide file tree
Showing 56 changed files with 558 additions and 419 deletions.
5 changes: 3 additions & 2 deletions activities/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package activities

import (
"context"
"github.com/bcc-code/bccm-flows/paths"
"github.com/bcc-code/bccm-flows/services/ffmpeg"
"go.temporal.io/sdk/activity"
)

type AnalyzeFileParams struct {
FilePath string
FilePath paths.Path
}

type AnalyzeFileResult struct {
Expand All @@ -19,7 +20,7 @@ func AnalyzeFile(ctx context.Context, input AnalyzeFileParams) (*AnalyzeFileResu
logger := activity.GetLogger(ctx)
logger.Info("Starting AnalyzeFileActivity")

info, err := ffmpeg.GetStreamInfo(input.FilePath)
info, err := ffmpeg.GetStreamInfo(input.FilePath.Local())
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions activities/baton/qc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package batonactivities

import (
"context"
"github.com/bcc-code/bccm-flows/paths"
"github.com/bcc-code/bccm-flows/services/baton"
"github.com/bcc-code/bccm-flows/utils"
"go.temporal.io/sdk/activity"
"time"
)

type QCParams struct {
Path utils.Path
Path paths.Path
Plan baton.TestPlan
}

Expand Down
49 changes: 26 additions & 23 deletions activities/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@ package activities

import (
"context"
"github.com/bcc-code/bccm-flows/utils"
"github.com/bcc-code/bccm-flows/paths"
"github.com/samber/lo"
"go.temporal.io/sdk/activity"
"os"
"path/filepath"
)

type FileInput struct {
Path string
Path paths.Path
}

type FileResult struct {
Path string
Path paths.Path
}

type MoveFileInput struct {
Source string
Destination string
Source paths.Path
Destination paths.Path
}

func MoveFile(ctx context.Context, input MoveFileInput) (*FileResult, error) {
log := activity.GetLogger(ctx)
activity.RecordHeartbeat(ctx, "MoveFile")
log.Info("Starting MoveFileActivity")

err := os.MkdirAll(filepath.Dir(input.Destination), os.ModePerm)
err := os.MkdirAll(filepath.Dir(input.Destination.Local()), os.ModePerm)
if err != nil {
return nil, err
}
err = os.Rename(input.Source, input.Destination)
err = os.Rename(input.Source.Local(), input.Destination.Local())
if err != nil {
return nil, err
}
_ = os.Chmod(input.Destination, os.ModePerm)
_ = os.Chmod(input.Destination.Local(), os.ModePerm)
return &FileResult{
Path: input.Destination,
}, nil
Expand All @@ -45,35 +46,35 @@ func StandardizeFileName(ctx context.Context, input FileInput) (*FileResult, err
activity.RecordHeartbeat(ctx, "StandardizeFileName")
log.Info("Starting StandardizeFileNameActivity")

path := utils.FixFilename(input.Path)
err := os.Rename(input.Path, path)
path := paths.FixFilename(input.Path.Local())
err := os.Rename(input.Path.Local(), path)
if err != nil {
return nil, err
}
_ = os.Chmod(path, os.ModePerm)
return &FileResult{
Path: path,
Path: paths.MustParse(path),
}, nil
}

type CreateFolderInput struct {
Destination string
Destination paths.Path
}

func CreateFolder(ctx context.Context, input CreateFolderInput) error {
log := activity.GetLogger(ctx)
activity.RecordHeartbeat(ctx, "CreateFolder")
log.Info("Starting CreateFolderActivity")

err := os.MkdirAll(input.Destination, os.ModePerm)
err := os.MkdirAll(input.Destination.Local(), os.ModePerm)
if err != nil {
return err
}
return os.Chmod(input.Destination, os.ModePerm)
return os.Chmod(input.Destination.Local(), os.ModePerm)
}

type WriteFileInput struct {
Path string
Path paths.Path
Data []byte
}

Expand All @@ -82,15 +83,15 @@ func WriteFile(ctx context.Context, input WriteFileInput) error {
activity.RecordHeartbeat(ctx, "WriteFile")
log.Info("Starting WriteFileActivity")

err := os.MkdirAll(filepath.Dir(input.Path), os.ModePerm)
err := os.MkdirAll(filepath.Dir(input.Path.Local()), os.ModePerm)
if err != nil {
return err
}
err = os.WriteFile(input.Path, input.Data, os.ModePerm)
err = os.WriteFile(input.Path.Local(), input.Data, os.ModePerm)
if err != nil {
return err
}
_ = os.Chmod(input.Path, os.ModePerm)
_ = os.Chmod(input.Path.Local(), os.ModePerm)
return nil
}

Expand All @@ -99,25 +100,27 @@ func ReadFile(ctx context.Context, input FileInput) ([]byte, error) {
activity.RecordHeartbeat(ctx, "ReadFile")
log.Info("Starting ReadFileActivity")

return os.ReadFile(input.Path)
return os.ReadFile(input.Path.Local())
}

func ListFiles(ctx context.Context, input FileInput) ([]string, error) {
func ListFiles(ctx context.Context, input FileInput) ([]paths.Path, error) {
log := activity.GetLogger(ctx)
activity.RecordHeartbeat(ctx, "ListFiles")
log.Info("Starting ListFilesActivity")

files, err := filepath.Glob(filepath.Join(input.Path, "*"))
files, err := filepath.Glob(filepath.Join(input.Path.Local(), "*"))
if err != nil {
return nil, err
}
return files, err
return lo.Map(files, func(i string, _ int) paths.Path {
return paths.MustParse(i)
}), err
}

func DeletePath(ctx context.Context, input FileInput) error {
log := activity.GetLogger(ctx)
activity.RecordHeartbeat(ctx, "DeletePath")
log.Info("Starting DeletePathActivity")

return os.RemoveAll(input.Path)
return os.RemoveAll(input.Path.Local())
}
15 changes: 8 additions & 7 deletions activities/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package activities
import (
"context"
"github.com/bcc-code/bccm-flows/common"
"github.com/bcc-code/bccm-flows/paths"
"github.com/bcc-code/bccm-flows/services/ffmpeg"
"github.com/bcc-code/bccm-flows/services/transcode"
"go.temporal.io/sdk/activity"
)

type AnalyzeEBUR128Params struct {
FilePath string
FilePath paths.Path
TargetLoudness float64
}

Expand All @@ -21,7 +22,7 @@ func AnalyzeEBUR128Activity(ctx context.Context, input AnalyzeEBUR128Params) (*c
stop, progressCallback := registerProgressCallback(ctx)
defer close(stop)

analyzeResult, err := ffmpeg.AnalyzeEBUR128(input.FilePath, progressCallback)
analyzeResult, err := ffmpeg.AnalyzeEBUR128(input.FilePath.Local(), progressCallback)
if err != nil {
return nil, err
}
Expand All @@ -47,8 +48,8 @@ func AnalyzeEBUR128Activity(ctx context.Context, input AnalyzeEBUR128Params) (*c
}

type AdjustAudioLevelParams struct {
InFilePath string
OutFilePath string
InFilePath paths.Path
OutFilePath paths.Path
Adjustment float64
}

Expand All @@ -67,14 +68,14 @@ func AdjustAudioLevelActivity(ctx context.Context, input *AdjustAudioLevelParams
}

type NormalizeAudioParams struct {
FilePath string
OutputPath string
FilePath paths.Path
OutputPath paths.Path
TargetLUFS float64
PerformOutputAnalysis bool
}

type NormalizeAudioResult struct {
FilePath string
FilePath paths.Path
InputAnalysis *common.AnalyzeEBUR128Result
OutputAnalysis *common.AnalyzeEBUR128Result
}
Expand Down
13 changes: 7 additions & 6 deletions activities/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package activities
import (
"context"
"fmt"
"github.com/bcc-code/bccm-flows/paths"
"github.com/bcc-code/bccm-flows/services/transcode"
"go.temporal.io/sdk/activity"
)

type TranscodePreviewParams struct {
FilePath string
DestinationDirPath string
FilePath paths.Path
DestinationDirPath paths.Path
}

type TranscodePreviewResponse struct {
PreviewFilePath string
PreviewFilePath paths.Path
AudioOnly bool
}

Expand All @@ -26,16 +27,16 @@ func TranscodePreview(ctx context.Context, input TranscodePreviewParams) (*Trans
defer close(stop)

result, err := transcode.Preview(transcode.PreviewInput{
OutputDir: input.DestinationDirPath,
FilePath: input.FilePath,
OutputDir: input.DestinationDirPath.Local(),
FilePath: input.FilePath.Local(),
}, progressCallback)
if err != nil {
fmt.Println(err.Error())
return nil, err
}

return &TranscodePreviewResponse{
PreviewFilePath: result.LowResolutionPath,
PreviewFilePath: paths.MustParse(result.LowResolutionPath),
AudioOnly: result.AudioOnly,
}, nil
}
8 changes: 4 additions & 4 deletions activities/queues.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package activities

import (
"github.com/bcc-code/bccm-flows/environment"
"reflect"
"runtime"
"strings"

"github.com/bcc-code/bccm-flows/utils"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -58,10 +58,10 @@ var videoActivities = lo.Map(GetVideoTranscodeActivities(), func(i any, _ int) s
func GetQueueForActivity(activity any) string {
f := getFunctionName(activity)
if lo.Contains(audioActivities, f) {
return utils.GetAudioQueue()
return environment.GetAudioQueue()
}
if lo.Contains(videoActivities, f) {
return utils.GetTranscodeQueue()
return environment.GetTranscodeQueue()
}
return utils.GetWorkerQueue()
return environment.GetWorkerQueue()
}
6 changes: 3 additions & 3 deletions activities/rclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package activities
import (
"context"
"fmt"
"github.com/bcc-code/bccm-flows/utils"
"github.com/bcc-code/bccm-flows/paths"
"time"

"github.com/bcc-code/bccm-flows/services/rclone"
Expand Down Expand Up @@ -47,8 +47,8 @@ func RcloneCopyDir(ctx context.Context, input RcloneCopyDirInput) (bool, error)
}

type RcloneMoveFileInput struct {
Source utils.Path
Destination utils.Path
Source paths.Path
Destination paths.Path
}

func RcloneMoveFile(ctx context.Context, input RcloneMoveFileInput) (bool, error) {
Expand Down
18 changes: 9 additions & 9 deletions activities/subtrans.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package activities
import (
"context"
"fmt"
"github.com/bcc-code/bccm-flows/paths"
"net/url"
"os"
"path"
"path/filepath"
"strings"

"github.com/bcc-code/bccm-flows/activities/vidispine"
Expand All @@ -18,7 +19,7 @@ type GetSubtitlesInput struct {
SubtransID string
Format string
ApprovedOnly bool
DestinationFolder string
DestinationFolder paths.Path
FilePrefix string
}

Expand Down Expand Up @@ -57,7 +58,7 @@ func GetSubtransIDActivity(ctx context.Context, input *GetSubtransIDInput) (*Get
}

// Extract file name
fileName := path.Base(parsedUri.Path)
fileName := filepath.Base(parsedUri.Path)

// Split by dot
fileNameSplit := strings.Split(fileName, ".")
Expand Down Expand Up @@ -93,10 +94,10 @@ func GetSubtransIDActivity(ctx context.Context, input *GetSubtransIDInput) (*Get
return out, nil
}

func GetSubtitlesActivity(ctx context.Context, params GetSubtitlesInput) (map[string]string, error) {
func GetSubtitlesActivity(ctx context.Context, params GetSubtitlesInput) (map[string]paths.Path, error) {
client := subtrans.NewClient(os.Getenv("SUBTRANS_BASE_URL"), os.Getenv("SUBTRANS_API_KEY"))

info, err := os.Stat(params.DestinationFolder)
info, err := os.Stat(params.DestinationFolder.Local())
if os.IsNotExist(err) {
return nil, err
}
Expand All @@ -116,15 +117,14 @@ func GetSubtitlesActivity(ctx context.Context, params GetSubtitlesInput) (map[st
params.FilePrefix = p
}

out := map[string]string{}
out := map[string]paths.Path{}
for lang, sub := range subs {
path := path.Join(params.DestinationFolder, params.FilePrefix+lang+"."+params.Format)
path := filepath.Join(params.DestinationFolder.Local(), params.FilePrefix+lang+"."+params.Format)
err := os.WriteFile(path, []byte(sub), 0644)
if err != nil {
return nil, err
}
out[lang] = path

out[lang] = paths.MustParse(path)
}
return out, nil
}
Loading

0 comments on commit e88bbcf

Please sign in to comment.