-
Notifications
You must be signed in to change notification settings - Fork 0
/
imaginary.go
133 lines (113 loc) · 3.08 KB
/
imaginary.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
package handler
import (
"io/ioutil"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"microfile/config"
)
type Imaginary struct {
Path string
DefaultURL string
Type string
Quality int
OutputDirectory string
}
// We can't process images from these events probably
func (i *Imaginary) ignoredEvent(event fsnotify.Event) bool {
return (event.Op == fsnotify.Remove || event.Op == fsnotify.Chmod)
}
func (i Imaginary) Handle(event fsnotify.Event) {
if i.ignoredEvent(event) == true {
return
}
file, _ := os.Open(event.Name)
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", filepath.Base(file.Name()))
io.Copy(part, file)
writer.Close()
destinationURL := PathCompletion(i.Path, i.DefaultURL)
r, _ := http.NewRequest("POST", destinationURL, body)
r.Header.Add("Accept", writer.FormDataContentType())
r.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
response, err := client.Do(r)
if err != nil {
log.WithFields(
log.Fields{
"handler": "imaginary",
"changed_file": event.Name,
"url": destinationURL,
}).Warn("Problem with imaginary service")
return
}
if response.StatusCode == 200 {
filename, err := i.writeImage(response, event.Name)
if err != nil {
log.WithFields(
log.Fields{
"handler": "imaginary",
"changed_file": event.Name,
"output_file": filename,
}).Warn("Problem saving converted image")
}
} else {
log.WithFields(
log.Fields{
"handler": "imaginary",
"status_code": response.StatusCode,
"changed_file": event.Name,
}).Warn("Imaginary response")
}
}
func (i *Imaginary) Init(action config.Action) error {
if action.Options["path"] != "" {
i.Path = action.Options["path"]
}
outputDirectory := action.Options["output_directory"]
if outputDirectory != "" {
i.OutputDirectory = outputDirectory
} else {
log.WithFields(
log.Fields{
"handler": "imaginary",
}).Fatal("output_directory for Imaginary needs to be set (where converted images are saved)")
}
return nil
}
func (i *Imaginary) writeImage(response *http.Response, filePath string) (string, error) {
basename := filepath.Base(filePath)
destinationFile := filepath.Join(i.OutputDirectory, basename)
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.WithFields(
log.Fields{
"handler": "imaginary",
"output_file": destinationFile,
}).Fatal("Can't read response Body")
}
defer response.Body.Close()
err = ioutil.WriteFile(destinationFile, bodyBytes, 0644)
if err != nil {
log.WithFields(
log.Fields{
"handler": "imaginary",
"output_file": destinationFile,
"output_directory": i.OutputDirectory,
}).Warn("Can't write file. Does output_directory exist?")
} else {
log.WithFields(
log.Fields{
"handler": "imaginary",
"output_file": destinationFile,
}).Info("Converted image.")
}
return destinationFile, nil
}