diff options
Diffstat (limited to 'internal/pipeline/put.go')
-rw-r--r-- | internal/pipeline/put.go | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/internal/pipeline/put.go b/internal/pipeline/put.go index d44f74f..fed04f8 100644 --- a/internal/pipeline/put.go +++ b/internal/pipeline/put.go @@ -5,6 +5,7 @@ package pipeline import ( + "context" "fmt" "image" _ "image/jpeg" @@ -43,16 +44,25 @@ func (f fileWalk) Walk(path string, info os.FileInfo, err error) error { // CheckImages checks that all files with a ".jpg" or ".png" suffix // in a directory are images that can be decoded (skipping dotfiles) -func CheckImages(dir string) error { +func CheckImages(ctx context.Context, dir string) error { checker := make(fileWalk) go func() { _ = filepath.Walk(dir, checker.Walk) close(checker) }() + n := 0 for path := range checker { + select { + case <-ctx.Done(): + return ctx.Err() + default: + } suffix := filepath.Ext(path) lsuffix := strings.ToLower(suffix) + if lsuffix == ".jpeg" { + lsuffix = ".jpg" + } if lsuffix != ".jpg" && lsuffix != ".png" { continue } @@ -64,6 +74,11 @@ func CheckImages(dir string) error { if err != nil { return fmt.Errorf("Decoding image %s failed: %v", path, err) } + n++ + } + + if n == 0 { + return fmt.Errorf("No images found") } return nil @@ -71,7 +86,10 @@ func CheckImages(dir string) error { // DetectQueueType detects which queue to use based on the preponderance // of files of a particular extension in a directory -func DetectQueueType(dir string, conn Queuer) string { +func DetectQueueType(dir string, conn Queuer, nowipe bool) string { + if nowipe { + return conn.PreNoWipeQueueId() + } pngdirs, _ := filepath.Glob(dir + "/*.png") jpgdirs, _ := filepath.Glob(dir + "/*.jpg") pngcount := len(pngdirs) @@ -89,7 +107,7 @@ func DetectQueueType(dir string, conn Queuer) string { // slash. It also appends all file names with sequential numbers, like // 0001, to ensure they are appropriately named for further processing // in the pipeline. -func UploadImages(dir string, bookname string, conn Uploader) error { +func UploadImages(ctx context.Context, dir string, bookname string, conn Uploader) error { files, err := ioutil.ReadDir(dir) if err != nil { fmt.Errorf("Failed to read directory %s: %v", dir, err) @@ -97,11 +115,19 @@ func UploadImages(dir string, bookname string, conn Uploader) error { filenum := 0 for _, file := range files { + select { + case <-ctx.Done(): + return ctx.Err() + default: + } if file.IsDir() { continue } origsuffix := filepath.Ext(file.Name()) lsuffix := strings.ToLower(origsuffix) + if lsuffix == ".jpeg" { + lsuffix = ".jpg" + } if lsuffix != ".jpg" && lsuffix != ".png" { continue } @@ -109,7 +135,8 @@ func UploadImages(dir string, bookname string, conn Uploader) error { origbase := strings.TrimSuffix(origname, origsuffix) origpath := filepath.Join(dir, origname) - newname := fmt.Sprintf("%s_%04d%s", origbase, filenum, origsuffix) + safebase := strings.ReplaceAll(origbase, " ", "_") + newname := fmt.Sprintf("%s_%04d%s", safebase, filenum, lsuffix) err = conn.Upload(conn.WIPStorageId(), filepath.Join(bookname, newname), origpath) if err != nil { return fmt.Errorf("Failed to upload %s: %v", origpath, err) |