diff options
| author | horus_arch | 2015-04-19 22:09:52 +0200 |
|---|---|---|
| committer | horus_arch | 2015-04-19 22:09:52 +0200 |
| commit | 01e9a34952bd6ddd383680b0ca2312e476ad07a6 (patch) | |
| tree | 00902575e5c271cc5d35ea65aa8795b8caeb97bc /imageprocessor/imageprocessor.go | |
| download | mandible-01e9a34952bd6ddd383680b0ca2312e476ad07a6.tar.gz | |
Initial commit.
Diffstat (limited to 'imageprocessor/imageprocessor.go')
| -rw-r--r-- | imageprocessor/imageprocessor.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/imageprocessor/imageprocessor.go b/imageprocessor/imageprocessor.go new file mode 100644 index 0000000..905b522 --- /dev/null +++ b/imageprocessor/imageprocessor.go @@ -0,0 +1,79 @@ +package imageprocessor + +import ( + "mandible/uploadedfile" +) + +type multiProcessType []ProcessType + +func (this multiProcessType) Process(image *uploadedfile.UploadedFile) error { + for _, processor := range this { + err := processor.Process(image) + if err != nil { + return err + } + } + + return nil +} + +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) { + errs <- p.Process(image) + }(processor) + } + + for i := 0; i < len(this); i++ { + select { + case err := <-errs: + if err != nil { + return err + } + } + } + + return nil +} + +type ProcessType interface { + Process(image *uploadedfile.UploadedFile) error +} + +type ImageProcessor struct { + processor ProcessType +} + +func (this *ImageProcessor) Run(image *uploadedfile.UploadedFile) error { + return this.processor.Process(image) +} + +func Factory(maxFileSize int64, file *uploadedfile.UploadedFile) (*ImageProcessor, error) { + size, err := file.FileSize() + if err != nil { + return &ImageProcessor{}, err + } + + processor := multiProcessType{} + processor = append(processor, &ImageOrienter{}) + + if size > maxFileSize { + processor = append(processor, &ImageScaler{maxFileSize}) + } + + async := asyncProcessType{} + + for _, t := range file.GetThumbs() { + async = append(async, t) + } + + if len(async) > 0 { + processor = append(processor, async) + } + + return &ImageProcessor{processor}, nil +} |
