-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_image_editor.go
290 lines (243 loc) · 9.73 KB
/
gf_image_editor.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
GloFlow application and media management/publishing platform
Copyright (C) 2019 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_image_editor
import (
"fmt"
"os"
"time"
"context"
"bytes"
"net/http"
"io/ioutil"
"encoding/json"
"encoding/base64"
"image"
"image/png"
"go.mongodb.org/mongo-driver/bson/primitive"
// "github.com/globalsign/mgo/bson"
"github.com/gloflow/gloflow/go/gf_core"
"github.com/gloflow/gloflow/go/gf_apps/gf_images_lib/gf_images_core"
)
//-------------------------------------------------
type GFeditedImage struct {
Id primitive.ObjectID `bson:"_id,omitempty"`
Id_str string `bson:"id_str"`
T_str string `bson:"t"` // "img_edited"
Creation_unix_time_f float64 `bson:"creation_unix_time_f"`
Source_image_id_str gf_images_core.GFimageID `bson:"source_image_id_str"`
}
type GFeditedImageSaveHTTPinput struct {
Title_str string `json:"new_title_str"` // title of the new edited_image
Source_image_id_str gf_images_core.GFimageID `json:"source_image_id_str"` // id of the gf_image that has modification applied to it
Source_flow_name_str string `json:"source_flow_name_str"` // which flow was the original image from
Target_flow_name_str string `json:"target_flow_name_str"` // which flow the modified_image should be placed into
Image_base64_data_str string `json:"image_base64_data_str"` // base64 encoded pixel data of the image
Applied_filters_lst []string `json:"applied_filters_lst"` // list of filter names (in order) that were applied to the original image
New_height_int int `json:"new_height_int"` // new dimensions in case of cropping/resizing
New_width_int int `json:"new_width_int"` // new dimensions in case of cropping/resizing
}
type GFeditedImageProcessingInfo struct {
png_image image.Image
tmp_local_filepath_str string
image_origin_url_str string
image_origin_page_url_str string
image_width_int int
image_height_int int
}
//-------------------------------------------------
func saveEditedImagePipeline(p_handler_url_path_str string,
p_req *http.Request,
p_resp http.ResponseWriter,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
//--------------------------
// INPUT
var input *GFeditedImageSaveHTTPinput
body_bytes_lst, _ := ioutil.ReadAll(p_req.Body)
err := json.Unmarshal(body_bytes_lst, input)
if err != nil {
gfErr := gf_core.ErrorCreate("failed to parse json edited_image_save http_input",
"json_decode_error",
map[string]interface{}{"handler_url_path_str": p_handler_url_path_str,},
err, "gf_image_editor", pRuntimeSys)
return gfErr
}
new_title_str := input.Title_str
source_image_id_str := input.Source_image_id_str
//--------------------------
// SAVE_BASE64_DATA_TO_FILE
// IMPORTANT!! - save first, and then create a GFimages
processingInfo, gfErr := saveEditedImage(source_image_id_str,
input.Image_base64_data_str,
pRuntimeSys)
if err != nil {
return gfErr
}
//--------------------------
sourceImage, gfErr := gf_images_core.DBmongoGetImage(source_image_id_str, pCtx, pRuntimeSys)
if gfErr != nil {
return gfErr
}
processingInfo.image_origin_url_str = sourceImage.Origin_url_str
processingInfo.image_origin_page_url_str = sourceImage.Origin_page_url_str
gfErr = createImage(new_title_str,
[]string{input.Target_flow_name_str,},
processingInfo,
pCtx,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
//--------------------------
return nil
}
//-------------------------------------------------
func saveEditedImage(p_source_image_id_str gf_images_core.GFimageID,
p_image_base64_data_str string,
pRuntimeSys *gf_core.RuntimeSys) (*GFeditedImageProcessingInfo, *gf_core.GFerror) {
//--------------------------
// BASE64_DECODE
image_byte_lst, err := base64.StdEncoding.DecodeString(p_image_base64_data_str)
if err != nil {
gfErr := gf_core.ErrorCreate("failed to decode base64 string of image_data",
"base64_decoding_error",
map[string]interface{}{
"source_image_id_str": p_source_image_id_str,
"image_base64_data_str": p_image_base64_data_str,
},
err, "gf_image_editor", pRuntimeSys)
return nil, gfErr
}
//--------------------------
// PNG
image_reader := bytes.NewReader(image_byte_lst)
png_image, err := png.Decode(image_reader)
if err != nil {
gfErr := gf_core.ErrorCreate("failed to encode png image_byte array while saving edited_image",
"png_encoding_error",
map[string]interface{}{
"source_image_id_str": p_source_image_id_str,
"image_base64_data_str": p_image_base64_data_str,
},
err, "gf_image_editor", pRuntimeSys)
return nil, gfErr
}
//--------------------------
// FILE
creationUNIXtimeF := float64(time.Now().UnixNano())/1000000000.0
tmp_local_filepath_str := fmt.Sprintf("/%f.png", creationUNIXtimeF)
// FILE_CREATE
file, err := os.Create(tmp_local_filepath_str)
if err != nil {
gfErr := gf_core.ErrorCreate("OS failed to create a file to save edited_image to FS",
"file_create_error",
map[string]interface{}{
"source_image_id_str": p_source_image_id_str,
"tmp_local_filepath_str": tmp_local_filepath_str,
},
err, "gf_image_editor", pRuntimeSys)
return nil, gfErr
}
defer file.Close()
// FILE_WRITE_IMAGE
err = png.Encode(file, png_image)
if err != nil {
gfErr := gf_core.ErrorCreate("failed to encode png image_byte array while saving GIF frame to FS",
"png_encoding_error",
map[string]interface{}{"tmp_local_filepath_str": tmp_local_filepath_str,},
err, "gf_image_editor", pRuntimeSys)
return nil, gfErr
}
// FILE_SYNC
if err := file.Sync(); err != nil {
gfErr := gf_core.ErrorCreate("failed to decode jpen image_byte array while saving edited_image",
"file_sync_error",
map[string]interface{}{
"source_image_id_str": p_source_image_id_str,
"tmp_local_filepath_str": tmp_local_filepath_str,
},
err, "gf_image_editor", pRuntimeSys)
return nil, gfErr
}
//--------------------------
// IMAGE_DIMENSIONS
imageWidthInt, imageHeightInt := gf_images_core.GetImageDimensionsFromImage(png_image,
pRuntimeSys)
//--------------------------
processingInfo := GFeditedImageProcessingInfo{
png_image: png_image,
tmp_local_filepath_str: tmp_local_filepath_str,
image_width_int: imageWidthInt,
image_height_int: imageHeightInt,
}
return &processingInfo, nil
}
//-------------------------------------------------
func createImage(p_new_title_str string,
p_images_flows_names_lst []string,
pProcessingInfo *GFeditedImageProcessingInfo,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
pRuntimeSys.LogFun("FUN_ENTER", "gf_image_editor.create_gf_image()")
image_client_type_str := "gf_image_editor" // IMPORTANT!! - since gf_image_editor is creating the image,
image_format_str := "png"
local_thumbnails_target_dir_path_str := "."
small_thumb_max_size_px_int := 200
medium_thumb_max_size_px_int := 400
large_thumb_max_size_px_int := 600
//--------------------------
// GF_IMAGE_ID
imageURIstr := pProcessingInfo.tmp_local_filepath_str
imageIDstr := gf_images_core.CreateImageID(imageURIstr,
pRuntimeSys)
//--------------------------
// THUMBNAILS
imageThumbs, gfErr := gf_images_core.CreateThumbnails(pProcessingInfo.png_image,
imageIDstr,
image_format_str,
local_thumbnails_target_dir_path_str,
small_thumb_max_size_px_int,
medium_thumb_max_size_px_int,
large_thumb_max_size_px_int,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
//--------------------------
gfImageInfo := &gf_images_core.GFimageNewInfo{
IDstr: imageIDstr,
Title_str: p_new_title_str,
Flows_names_lst: p_images_flows_names_lst,
Image_client_type_str: image_client_type_str,
Origin_url_str: pProcessingInfo.image_origin_url_str,
Origin_page_url_str: pProcessingInfo.image_origin_page_url_str,
Original_file_internal_uri_str: pProcessingInfo.tmp_local_filepath_str, // image_local_file_path_str,
ThumbnailSmallURLstr: imageThumbs.Small_relative_url_str,
ThumbnailMediumURLstr: imageThumbs.Medium_relative_url_str,
ThumbnailLargeURLstr: imageThumbs.Large_relative_url_str,
Format_str: image_format_str,
Width_int: pProcessingInfo.image_width_int,
Height_int: pProcessingInfo.image_height_int,
}
// IMPORTANT!! - creates a GF_Image struct and stores it in the DB.
// every GIF in the system has its GF_Gif DB struct and GF_Image DB struct.
// these two structs are related by origin_url
_, gfErr = gf_images_core.ImageCreateNew(gfImageInfo, pCtx, pRuntimeSys)
if gfErr != nil {
return gfErr
}
return nil
}