Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for saving thumbnails to an alternate location #81

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion catalyst-uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func run() int {
segTimeout := fs.Duration("segment-timeout", 5*time.Minute, "Segment write timeout")
disableRecording := CommaSliceFlag(fs, "disable-recording", `Comma-separated list of playbackIDs to disable recording for`)
disableThumbs := CommaSliceFlag(fs, "disable-thumbs", `Comma-separated list of playbackIDs to disable thumbs for`)
thumbsURLReplacement := CommaMapFlag(fs, "thumbs-replace-urls", `Map of space separated playbackIDs to space separated URL replacement to use when saving thumbnails. E.g. playbackID1 playbackID2=oldURL newURL`)

defaultConfigFile := "/etc/livepeer/catalyst_uploader.conf"
if _, err := os.Stat(defaultConfigFile); os.IsNotExist(err) {
Expand All @@ -50,6 +51,7 @@ func run() int {
_ = fs.String("config", defaultConfigFile, "config file (optional)")

err = ff.Parse(fs, os.Args[1:],
ff.WithIgnoreUndefined(true),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarPrefix("CATALYST_UPLOADER"),
Expand Down Expand Up @@ -110,7 +112,7 @@ func run() int {
}

start := time.Now()
out, err := core.Upload(os.Stdin, uri, WaitBetweenWrites, *timeout, *storageFallbackURLs, *segTimeout, *disableThumbs)
out, err := core.Upload(os.Stdin, uri, WaitBetweenWrites, *timeout, *storageFallbackURLs, *segTimeout, *disableThumbs, *thumbsURLReplacement)
if err != nil {
glog.Errorf("Uploader failed for %s: %s", uri.Redacted(), err)
return 1
Expand Down
26 changes: 23 additions & 3 deletions core/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var expiryField = map[string]string{
"Object-Expires": "+168h", // Objects will be deleted after 7 days
}

func Upload(input io.Reader, outputURI *url.URL, waitBetweenWrites, writeTimeout time.Duration, storageFallbackURLs map[string]string, segTimeout time.Duration, disableThumbs []string) (*drivers.SaveDataOutput, error) {
func Upload(input io.Reader, outputURI *url.URL, waitBetweenWrites, writeTimeout time.Duration, storageFallbackURLs map[string]string, segTimeout time.Duration, disableThumbs []string, thumbsURLReplacement map[string]string) (*drivers.SaveDataOutput, error) {
ext := filepath.Ext(outputURI.Path)
inputFile, err := os.CreateTemp("", "upload-*"+ext)
if err != nil {
Expand All @@ -78,7 +78,7 @@ func Upload(input io.Reader, outputURI *url.URL, waitBetweenWrites, writeTimeout
return nil, fmt.Errorf("failed to upload video %s: (%d bytes) %w", outputURI.Redacted(), bytesWritten, err)
}

if err = extractThumb(outputURI, inputFileName, storageFallbackURLs, disableThumbs); err != nil {
if err = extractThumb(outputURI, inputFileName, storageFallbackURLs, disableThumbs, thumbsURLReplacement); err != nil {
glog.Errorf("extracting thumbnail failed for %s: %v", outputURI.Redacted(), err)
}
return out, nil
Expand Down Expand Up @@ -229,13 +229,33 @@ func uploadFile(outputURI *url.URL, fileName string, fields *drivers.FilePropert
return out, bytesWritten, err
}

func extractThumb(outputURI *url.URL, segmentFileName string, storageFallbackURLs map[string]string, disableThumbs []string) error {
func extractThumb(outputURI *url.URL, segmentFileName string, storageFallbackURLs map[string]string, disableThumbs []string, thumbsURLReplacement map[string]string) error {
for _, playbackID := range disableThumbs {
if strings.Contains(outputURI.Path, playbackID) {
glog.Infof("Thumbnails disabled for %s", outputURI.Redacted())
return nil
}
}
for playbackIDs, replacement := range thumbsURLReplacement {
for _, playbackID := range strings.Split(playbackIDs, " ") {
if strings.Contains(outputURI.Path, playbackID) {
outputURIStr := outputURI.String()
split := strings.Split(replacement, " ")
if len(split) != 2 {
break
}
original, replaceWith := split[0], split[1]

newURI, err := url.Parse(strings.Replace(outputURIStr, original, replaceWith, 1))
if err != nil {
return fmt.Errorf("failed to parse thumbnail URL: %w", err)
}
outputURI = newURI
glog.Infof("Replaced thumbnail location for %s", outputURI.Redacted())
break
}
}
}

tmpDir, err := os.MkdirTemp(os.TempDir(), "thumb-*")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestItWritesSlowInputIncrementally(t *testing.T) {
go func() {
u, err := url.Parse(outputFile.Name())
require.NoError(t, err)
_, err = Upload(slowReader, u, 100*time.Millisecond, time.Second, nil, time.Minute, nil)
_, err = Upload(slowReader, u, 100*time.Millisecond, time.Second, nil, time.Minute, nil, nil)
require.NoError(t, err, "")
}()

Expand Down
Loading