/
binary.go
124 lines (106 loc) · 2.85 KB
/
binary.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package binary
import (
"context"
"fmt"
"github.com/barasher/picdexer/conf"
"github.com/barasher/picdexer/internal/common"
"github.com/rs/zerolog/log"
"io/ioutil"
"os"
"sync"
)
const (
fileIdentifier = "file"
resizedFileIdentifier = "resizedFile"
keyIdentifier = "key"
defaultResizingThreadCount = 4
defaultToResizeChannelSize = defaultResizingThreadCount
)
type Storer struct {
conf conf.BinaryConf
resizer resizerInterface
pusher pusherInterface
}
func (s *Storer) resizingThreadCount() int {
n := s.conf.ResizingThreadCount
if n <1{
n = defaultResizingThreadCount
}
return n
}
func (s *Storer) toResizeChannelSize() int {
n := s.conf.ToResizeChannelSize
if n <1 {
n = defaultToResizeChannelSize
}
return n
}
func NewStorer(c conf.BinaryConf, push bool) (*Storer, error) {
s := &Storer{conf: c}
switch {
case c.Width > 0 && c.Height > 0:
s.resizer = NewResizer(c)
case c.Width == 0 && c.Height == 0:
s.resizer = NewNopResizer()
default:
return s, fmt.Errorf("wrong width (%v) & height (%v) couple", c.Width, c.Height)
}
s.pusher = NewNopPusher()
if push {
s.pusher = NewPusher(c)
}
return s, nil
}
func (s *Storer) StoreFolder(ctx context.Context, f string, o string) {
c := make(chan string, s.toResizeChannelSize())
go func() {
err := common.BrowseImages(f, func(path string, info os.FileInfo) {
c <- path
})
close(c)
if err != nil {
log.Error().Msgf("error while browsing folder %v: %v", f, err)
}
}()
s.StoreChannel(ctx, c, o)
}
func (s *Storer) StoreChannel(ctx context.Context, c <-chan string, o string) {
wg := &sync.WaitGroup{}
threadCount := s.resizingThreadCount()
wg.Add(threadCount)
for i := 0; i < threadCount; i++ {
go func(id int) {
s.storeChannel(ctx, id, c, o, wg)
}(i)
}
wg.Wait()
}
func (s *Storer) storeChannel(ctx context.Context, threadId int, c <-chan string, o string, wg *sync.WaitGroup) {
defer wg.Done()
subLog := log.With().Int("threadId", threadId).Logger()
var dir = o
var err error
if o == "" {
dir, err = ioutil.TempDir(os.TempDir(), "picdexer")
if err != nil {
subLog.Error().Msgf("error while creating temporary folder: %v", err)
return
}
defer os.RemoveAll(dir)
subLog.Debug().Msgf("Resized pictures temporary folder: %v", dir)
}
for cur := range c {
subLog.Debug().Str(fileIdentifier, cur).Msg("Resizing...")
outBin, outKey, err := s.resizer.resize(ctx, cur, dir)
if err != nil {
subLog.Error().Str(fileIdentifier, cur).Msgf("Error while resizing: %v", err)
continue
}
subLog.Debug().Str(fileIdentifier, cur).Str(resizedFileIdentifier, outBin).Str(keyIdentifier, outKey).Msg("Pushing...")
err = s.pusher.push(outBin, outKey)
if err != nil {
subLog.Error().Str(fileIdentifier, cur).Str(resizedFileIdentifier, outBin).Str(keyIdentifier, outKey).Msgf("Error while pushing: %v", err)
continue
}
}
}