-
Notifications
You must be signed in to change notification settings - Fork 2
/
media_image.go
206 lines (159 loc) · 5.06 KB
/
media_image.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
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package media
import (
"io"
"github.com/lib/pq"
"github.com/rande/gonode/core/helper"
"github.com/rande/gonode/core/vault"
"github.com/rande/gonode/modules/base"
log "github.com/sirupsen/logrus"
)
type ImageMeta struct {
Width int `json:"width"`
Height int `json:"height"`
Size int `json:"size"`
ContentType string `json:"content_type"`
Length int `json:"length"`
Exif ExifMeta `json:"exif"`
Hash string `json:"hash"`
SourceStatus int `json:"source_status"`
SourceError string `json:"source_error"`
}
type Image struct {
Reference string `json:"reference"`
Name string `json:"name"`
SourceUrl string `json:"source_url"`
}
type ImageHandler struct {
Vault *vault.Vault
Logger *log.Logger
}
func (h *ImageHandler) GetStruct() (base.NodeData, base.NodeMeta) {
return &Image{}, &ImageMeta{
SourceStatus: base.ProcessStatusInit,
}
}
func (h *ImageHandler) PreInsert(node *base.Node, m base.NodeManager) error {
data := node.Data.(*Image)
meta := node.Meta.(*ImageMeta)
if data.SourceUrl != "" && meta.SourceStatus == base.ProcessStatusInit {
meta.SourceStatus = base.ProcessStatusUpdate
meta.SourceError = ""
}
return nil
}
func (h *ImageHandler) PreUpdate(node *base.Node, m base.NodeManager) error {
data := node.Data.(*Image)
meta := node.Meta.(*ImageMeta)
if data.SourceUrl != "" && meta.SourceStatus == base.ProcessStatusInit {
meta.SourceStatus = base.ProcessStatusUpdate
meta.SourceError = ""
}
return nil
}
func (h *ImageHandler) PostInsert(node *base.Node, m base.NodeManager) error {
meta := node.Meta.(*ImageMeta)
if meta.SourceStatus == base.ProcessStatusUpdate {
m.Notify("media_file_download", node.Uuid.String())
}
return nil
}
func (h *ImageHandler) PostUpdate(node *base.Node, m base.NodeManager) error {
meta := node.Meta.(*ImageMeta)
if meta.SourceStatus == base.ProcessStatusUpdate {
m.Notify("media_file_download", node.Uuid.String())
}
return nil
}
func (h *ImageHandler) GetDownloadData(node *base.Node) *base.DownloadData {
meta := node.Meta.(*ImageMeta)
data := base.GetDownloadData()
data.Filename = node.Name
data.ContentType = meta.ContentType
data.Stream = func(node *base.Node, w io.Writer) {
_, err := h.Vault.Get(node.UniqueId(), w)
helper.PanicOnError(err)
}
return data
}
func (h *ImageHandler) StoreStream(node *base.Node, r io.Reader) (int64, error) {
return HandleImageReader(node, h.Vault, r, h.Logger)
}
type ImageDownloadListener struct {
Vault *vault.Vault
HttpClient base.HttpClient
Logger *log.Logger
}
func (l *ImageDownloadListener) Handle(notification *pq.Notification, m base.NodeManager) (int, error) {
reference, err := base.GetReferenceFromString(notification.Extra)
if err != nil {
l.Logger.WithFields(log.Fields{
"module": "media.downloader",
"node_uuid": notification.Extra,
}).Debug("Unable to parse reference")
// unable to parse the reference
return base.PubSubListenContinue, nil
}
if l.Logger != nil {
l.Logger.WithFields(log.Fields{
"module": "media.downloader",
"node_uuid": notification.Extra,
}).Debug("Download binary from uuid")
}
node := m.Find(reference)
if node == nil {
if l.Logger != nil {
l.Logger.WithFields(log.Fields{
"module": "media.downloader",
"node_uuid": notification.Extra,
}).Info("Unable to download file, uuid does not exist")
}
return base.PubSubListenContinue, nil
}
data := node.Data.(*Image)
meta := node.Meta.(*ImageMeta)
if meta.SourceStatus == base.ProcessStatusDone {
if l.Logger != nil {
l.Logger.WithFields(log.Fields{
"module": "media.downloader",
"node_uuid": notification.Extra,
"meta_source_status": base.ProcessStatusDone,
}).Warn("Stop downloading process, already done! (race condition ?)")
}
return base.PubSubListenContinue, nil
}
resp, err := l.HttpClient.Get(data.SourceUrl)
if err != nil {
meta.SourceStatus = base.ProcessStatusError
meta.SourceError = "Unable to retrieve the remote file"
if l.Logger != nil {
l.Logger.WithFields(log.Fields{
"module": "media.downloader",
"node_uuid": notification.Extra,
"error": err.Error(),
"meta_source_status": base.ProcessStatusError,
}).Warn("Unable to retrieve the remote file")
}
m.Save(node, false)
return base.PubSubListenContinue, err
}
defer resp.Body.Close()
if _, err = HandleImageReader(node, l.Vault, resp.Body, l.Logger); err != nil {
meta.SourceStatus = base.ProcessStatusError
meta.SourceError = "Unable to analyse the image"
m.Save(node, false)
return base.PubSubListenContinue, err
}
meta.SourceStatus = base.ProcessStatusDone
m.Save(node, false)
if l.Logger != nil {
l.Logger.WithFields(log.Fields{
"module": "media.downloader",
"node_uuid": notification.Extra,
}).Debug("File downloaded!")
}
return base.PubSubListenContinue, nil
}