-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
batch.go
107 lines (98 loc) · 2.24 KB
/
batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"compress/flate"
"context"
"encoding/base64"
"io"
"os"
"path/filepath"
"strings"
"github.com/ncruces/rethinkraw/pkg/osutil"
"golang.org/x/sync/errgroup"
)
func toBatchPath(paths ...string) string {
var buf strings.Builder
b64 := base64.NewEncoder(base64.RawURLEncoding, &buf)
flt, err := flate.NewWriter(b64, flate.BestCompression)
if err != nil {
panic(err)
}
for _, path := range paths {
flt.Write([]byte(path))
flt.Write([]byte{0})
}
flt.Close()
b64.Close()
return buf.String()
}
func fromBatchPath(path string) []string {
b64 := base64.NewDecoder(base64.RawURLEncoding, strings.NewReader(strings.TrimPrefix(path, "/")))
flt := flate.NewReader(b64)
var buf strings.Builder
_, err := io.Copy(&buf, flt)
if err != nil {
return nil
}
str := buf.String()
return strings.Split(strings.TrimSuffix(str, "\x00"), "\x00")
}
type batchPhoto struct {
Path string
Name string
}
func findPhotos(batch []string) ([]batchPhoto, error) {
var photos []batchPhoto
for _, path := range batch {
var prefix string
if len(batch) > 1 {
prefix, _ = filepath.Split(path)
} else {
prefix = path + string(filepath.Separator)
}
err := filepath.WalkDir(path, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if osutil.HiddenFile(entry) {
if entry.IsDir() {
return filepath.SkipDir
}
return nil
}
if entry.Type().IsRegular() {
if _, ok := extensions[strings.ToUpper(filepath.Ext(path))]; ok {
var name string
if strings.HasPrefix(path, prefix) {
name = path[len(prefix):]
} else {
_, name = filepath.Split(path)
}
photos = append(photos, batchPhoto{path, name})
}
}
return nil
})
if err != nil {
return nil, err
}
}
return photos, nil
}
func batchProcess(ctx context.Context, photos []batchPhoto, proc func(ctx context.Context, photo batchPhoto) error) <-chan error {
const parallelism = 6
output := make(chan error, parallelism)
go func() {
group, ctx := errgroup.WithContext(ctx)
group.SetLimit(parallelism)
for _, photo := range photos {
photo := photo
group.Go(func() error {
output <- proc(ctx, photo)
return nil
})
}
group.Wait()
close(output)
}()
return output
}