This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
imageprocessor.go
124 lines (98 loc) · 2.96 KB
/
imageprocessor.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 imageprocessor
import (
"fmt"
"strings"
"github.com/Imgur/mandible/config"
"github.com/Imgur/mandible/uploadedfile"
)
type ProcessType interface {
Process(image *uploadedfile.UploadedFile) error
String() string
}
type multiProcessType []ProcessType
func (this multiProcessType) Process(image *uploadedfile.UploadedFile) error {
for _, processor := range this {
err := processor.Process(image)
if err != nil {
return fmt.Errorf("Error multiprocessing on %s: %s", processor.String(), err.Error())
}
}
return nil
}
func (this multiProcessType) String() string {
processes := make([]string, 0)
for _, p := range this {
processes = append(processes, p.String())
}
return "Multiple processes <" + strings.Join(processes, ", ") + ">"
}
type asyncProcessType []ProcessType
func (this asyncProcessType) Process(image *uploadedfile.UploadedFile) error {
errs := make(chan error, len(this))
for _, processor := range this {
go func(p ProcessType) {
err := p.Process(image)
if err != nil {
errs <- fmt.Errorf("Error asynchronously processing on %s: %s", p.String(), err.Error())
} else {
errs <- nil
}
}(processor)
}
for i := 0; i < len(this); i++ {
select {
case err := <-errs:
if err != nil {
return err
}
}
}
return nil
}
func (this asyncProcessType) String() string {
processes := make([]string, 0)
for _, p := range this {
processes = append(processes, p.String())
}
return "Async processes <" + strings.Join(processes, ", ") + ">"
}
type ImageProcessor struct {
processor ProcessType
}
func (this *ImageProcessor) Run(image *uploadedfile.UploadedFile) error {
return this.processor.Process(image)
}
type ImageProcessorStrategy func(*config.Configuration, *uploadedfile.UploadedFile) (*ImageProcessor, error)
// Just do nothing to the file after it's uploaded...
var PassthroughStrategy = func(cfg *config.Configuration, file *uploadedfile.UploadedFile) (*ImageProcessor, error) {
return &ImageProcessor{multiProcessType{}}, nil
}
var ThumbnailStrategy = func(cfg *config.Configuration, file *uploadedfile.UploadedFile) (*ImageProcessor, error) {
processor := asyncProcessType{}
for _, t := range file.GetThumbs() {
processor = append(processor, t)
}
return &ImageProcessor{processor}, nil
}
var EverythingStrategy = func(cfg *config.Configuration, file *uploadedfile.UploadedFile) (*ImageProcessor, error) {
size, err := file.FileSize()
if err != nil {
return &ImageProcessor{}, err
}
processor := multiProcessType{}
processor = append(processor, &ImageOrienter{})
processor = append(processor, &CompressLosslessly{})
processor = append(processor, &ExifStripper{})
if size > cfg.MaxFileSize {
processor = append(processor, &ImageScaler{cfg.MaxFileSize})
}
async := asyncProcessType{}
async = append(async, DuelOCRStratagy())
for _, t := range file.GetThumbs() {
async = append(async, t)
}
if len(async) > 0 {
processor = append(processor, async)
}
return &ImageProcessor{processor}, nil
}