From 234caa159e0c8bb1bac99ec8c30226e0386f3ac1 Mon Sep 17 00:00:00 2001 From: ari melody Date: Tue, 23 Jun 2026 19:07:51 +0100 Subject: [PATCH] allow thumbnail.png to not exist --- main.go | 10 ++++++---- scanner/scanner.go | 4 ++++ youtube/youtube.go | 38 ++++++++++++++++++++------------------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 67cd1c2..74572d1 100644 --- a/main.go +++ b/main.go @@ -217,14 +217,16 @@ func main() { } // scan for thumbnail + var thumbnail *yt.Thumbnail thumbnailPath, thumbnailSizeBytes, err := scanner.ScanThumbnail(directory) if err != nil { log.Fatalf("Failed to fetch thumbnail: %v", err) os.Exit(1) - } - thumbnail := &yt.Thumbnail{ - Filepath: thumbnailPath, - SizeBytes: thumbnailSizeBytes, + } else { + thumbnail = &yt.Thumbnail{ + Filepath: thumbnailPath, + SizeBytes: thumbnailSizeBytes, + } } // build video template for upload diff --git a/scanner/scanner.go b/scanner/scanner.go index 606f269..b124036 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -2,6 +2,7 @@ package scanner import ( "encoding/json" + "errors" "fmt" "os" "path" @@ -65,6 +66,9 @@ func ScanThumbnail(directory string) (string, int64, error) { thumbnailPath := path.Join(directory, "thumbnail.png") stat, err := os.Stat(thumbnailPath) if err != nil { + if os.IsNotExist(err) { + return "", 0, err + } return "", 0, err } if stat.IsDir() { diff --git a/youtube/youtube.go b/youtube/youtube.go index 5f0dc0f..96b18fc 100644 --- a/youtube/youtube.go +++ b/youtube/youtube.go @@ -285,27 +285,29 @@ func UploadVideo( return nil, err } - // upload thumbnail + if thumbnail != nil { + // upload thumbnail - thumbnailService := youtube.NewThumbnailsService(service) - thumbnailSetCall := thumbnailService.Set(ytVideo.Id) - thumbnailFile, err := os.Open(thumbnail.Filepath) - if err != nil { - log.Fatalf("Failed to open thumbnail: %v\n", err) - return nil, err - } - thumbnailSetCall.Media(thumbnailFile) + thumbnailService := youtube.NewThumbnailsService(service) + thumbnailSetCall := thumbnailService.Set(ytVideo.Id) + thumbnailFile, err := os.Open(thumbnail.Filepath) + if err != nil { + log.Printf("Failed to open thumbnail: %v\n", err) + return ytVideo, err + } + thumbnailSetCall.Media(thumbnailFile) - log.Println("Uploading thumbnail...") + log.Println("Uploading thumbnail...") - thumbnailSetCall.ProgressUpdater(func(current, total int64) { - if total == 0 { total = thumbnail.SizeBytes } - fmt.Printf("\t(%.2f%%)\n", float64(current) / float64(total) * 100) - }) - // kinda don't care about the response here- so long as it works! - _, err = thumbnailSetCall.Do() - if err != nil { - log.Printf("Failed to upload thumbnail: %v\n", err) + thumbnailSetCall.ProgressUpdater(func(current, total int64) { + if total == 0 { total = thumbnail.SizeBytes } + fmt.Printf("\t(%.2f%%)\n", float64(current) / float64(total) * 100) + }) + // kinda don't care about the response here- so long as it works! + _, err = thumbnailSetCall.Do() + if err != nil { + log.Printf("Failed to upload thumbnail: %v\n", err) + } } return ytVideo, err