-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatautil.go
193 lines (175 loc) · 5.05 KB
/
datautil.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package imgproc
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
)
// Sourcer is the interface that groups the basic Next and Close methods of an image data source.
type Sourcer interface {
// GetImgItemCh returns a ImgItemCh channel of pointers to ImgItem objects to be processed.
GetImgItemCh() ImgItemCh
}
// ImgItem represents an image being processed.
type ImgItem struct {
// Name is the image name, e.g. image filename or URL.
Name string
// Data is the image content.
Data []byte
}
// ImgItemCh represents channel of pointers to ImgItem.
type ImgItemCh chan *ImgItem
// FileSrc is a Sourcer structure which uses text files as a source of images.
// Each line of a file is image URL.
type FileSrc struct {
FileName string
loadNum int
}
// NewFileSrc returns new FileSrc object which implements Sourcer interface.
func NewFileSrc(fileName string, loadNum int) (*FileSrc, error) {
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return nil, fmt.Errorf("File %s not exists", fileName)
}
file, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("Error opening file %s: %s", fileName, err)
}
file.Close()
src := FileSrc{FileName: fileName, loadNum: loadNum}
return &src, nil
}
// GetImgItemCh returns a ImgItemCh channel of pointers to ImgItem objects to be processed.
func (src *FileSrc) GetImgItemCh(done <-chan struct{}) ImgItemCh {
imgItemCh := make(ImgItemCh, src.loadNum)
go func() {
defer close(imgItemCh) // Closing channel at the end of iterating over file lines.
file, err := os.Open(src.FileName)
if err != nil {
log.Printf("Error opening file %s: %s", src.FileName, err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
sem := make(chan struct{}, src.loadNum)
var wg sync.WaitGroup
defer wg.Wait() // Waiting for processing of all line in goroutines.
for scanner.Scan() {
// Downloading images in "loadNum" number of goroutines.
select {
case <-done:
log.Println("Iterating over file lines was cancelled")
return
case sem <- struct{}{}:
}
wg.Add(1)
go func(url string) {
defer wg.Done()
defer func() { <-sem }()
resp, err := http.Get(url)
if err != nil {
log.Printf("Cannot get content of URL: %s", url)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Cannot read response body content for url '%s': %s", url, err)
return
}
imgItemCh <- &ImgItem{url, body}
}(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading file %s: %s", src.FileName, err)
}
}()
return imgItemCh
}
// ProcessItems processes ImgItem objects coming from ImgItemCh channel in "workersNum"
// goroutines and returns ResultItemCh channel of processed results.
func ProcessItems(done <-chan struct{}, imgItemCh ImgItemCh, resultN, workersNum int) ResultItemCh {
resultItemCh := make(ResultItemCh)
go func() {
defer close(resultItemCh)
sem := make(chan struct{}, workersNum)
var wg sync.WaitGroup
defer wg.Wait()
for imgItem := range imgItemCh {
select {
case <-done:
log.Println("ProcessItems was cancelled")
return
case sem <- struct{}{}:
}
wg.Add(1)
go func(imgItem *ImgItem) {
defer wg.Done()
defer func() { <-sem }()
results, err := Analyze(imgItem.Data, resultN)
if err != nil {
log.Printf("Error during processing image %s: %s", imgItem.Name, err)
return
}
resultItemCh <- ResultItem{imgItem.Name, results}
}(imgItem)
}
}()
return resultItemCh
}
// CsvResult represents storage of analysis results.
type CsvResult struct {
FileName string
colNum int
}
// ResultItem represents processing result to be saved in CSV file.
type ResultItem struct {
Name string
Results ColorList
}
// ResultItemCh represents channel of pointers to ResultItem.
type ResultItemCh chan ResultItem
// NewCsvResult creates new CsvResult.
func NewCsvResult(fileName string, colNum int) (*CsvResult, error) {
if colNum < 1 {
return nil, fmt.Errorf("Column number should be above 0")
}
file, err := os.Create(fileName)
if err != nil {
return nil, fmt.Errorf("os.Create returns an error: %s", err)
}
file.Close()
return &CsvResult{fileName, colNum}, nil
}
// Add adds new line to CSV result file.
func (r *CsvResult) Add(res *ResultItem) error {
line := res.Name + ","
for i, color := range res.Results {
if i >= r.colNum {
break
}
line += "#" + color.Hex + ","
}
// Adding emty columns in case the size of ColorList is less than the numeber of result columns.
for i := 0; i < r.colNum-len(res.Results); i++ {
line += ","
}
return r.append(line)
}
func (r *CsvResult) append(line string) error {
f, err := os.OpenFile(r.FileName, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("os.OpenFile returns an error: %s", err)
}
defer func() {
if err := f.Close(); err != nil {
log.Printf("file.Close returns an error: %s", err)
}
}()
if _, err := f.Write([]byte(line + "\n")); err != nil {
return fmt.Errorf("file.Write returns an error: %s", err)
}
return nil
}