-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_images_s3.go
193 lines (154 loc) · 6.59 KB
/
gf_images_s3.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
/*
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_images_core
import (
"fmt"
"path"
"strings"
"github.com/gloflow/gloflow/go/gf_core"
"github.com/gloflow/gloflow/go/gf_extern_services/gf_aws"
)
//---------------------------------------------------
func S3getImage(pImageS3filePathStr string,
pTargetFileLocalPathStr string,
pS3bucketNameStr string,
pS3info *gf_aws.GFs3Info,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
gfErr := gf_aws.S3getFile(pImageS3filePathStr,
pTargetFileLocalPathStr,
pS3bucketNameStr,
pS3info,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
return nil
}
//---------------------------------------------------
// DEPRECATED!!
func S3storeImage(pImageLocalFilePathStr string,
pImageThumbs *GFimageThumbs,
pS3bucketNameStr string,
pS3info *gf_aws.GFs3Info,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
//--------------------
// UPLOAD FULL_SIZE (ORIGINAL) IMAGE
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// FIX!! - target filename of the original image should not be its original file name (that might collide accross domains or other images),
// and instead should be the image ID with the file extension.
// it also makes it more difficult to find the image on S3 that is represented by an Gf_img given
// only the ID of that Gf_img
s3FileNameStr := path.Base(pImageLocalFilePathStr)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/* for files acquired by the Fetcher images are already uploaded
with their Gf_img ID as their filename. so here the pImageLocalFilePathStr value is already
the image ID.
ADD!! - have an explicit p_target_s3FileNameStr argument, and dont derive it
automatically from the the filename in pImageLocalFilePathStr */
targetFileLocalPathStr := pImageLocalFilePathStr
targetFileS3pathStr := s3FileNameStr
_, gfErr := gf_aws.S3putFile(targetFileLocalPathStr,
targetFileS3pathStr,
pS3bucketNameStr,
pS3info,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
//--------------------
// UPLOAD THUMBS
gfErr = S3storeThumbnails(pImageThumbs,
pS3bucketNameStr,
pS3info,
pRuntimeSys)
if gfErr != nil {
return gfErr
}
//--------------------
return nil
}
//---------------------------------------------------
// DEPRECATED!!
func S3storeThumbnails(pImageThumbs *GFimageThumbs,
pS3bucketNameStr string,
pS3info *gf_aws.GFs3Info,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
// IMPORTANT - for some image types (GIF) the system doesnt produce thumbs,
// and therefore pImageThumbs is nil.
if pImageThumbs != nil {
//--------------------
// SMALL THUMB
small_t_path_str := pImageThumbs.Small_local_file_path_str // thumbs_info_map["small__target_thumbnail_file_path_str"]
small_t_s3_file_name_str := fmt.Sprintf("/thumbnails/%s", path.Base(small_t_path_str))
s3ResponseStr, gfErr := gf_aws.S3putFile(small_t_path_str, small_t_s3_file_name_str, pS3bucketNameStr, pS3info, pRuntimeSys)
if gfErr != nil {
return gfErr
}
pRuntimeSys.LogFun("INFO","s3ResponseStr - "+s3ResponseStr)
//--------------------
// MEDIUM THUMB
medium_t_path_str := pImageThumbs.Medium_local_file_path_str // thumbs_info_map["medium__target_thumbnail_file_path_str"]
medium_t_s3_file_name_str := fmt.Sprintf("/thumbnails/%s", path.Base(medium_t_path_str))
s3ResponseStr, gfErr = gf_aws.S3putFile(medium_t_path_str, medium_t_s3_file_name_str, pS3bucketNameStr, pS3info, pRuntimeSys)
if gfErr != nil {
return gfErr
}
pRuntimeSys.LogFun("INFO","s3ResponseStr - "+s3ResponseStr)
//--------------------
// LARGE THUMB
large_t_path_str := pImageThumbs.Large_local_file_path_str // thumbs_info_map["large__target_thumbnail_file_path_str"]
large_t_s3_file_name_str := fmt.Sprintf("/thumbnails/%s",path.Base(large_t_path_str))
s3ResponseStr, gfErr = gf_aws.S3putFile(large_t_path_str, large_t_s3_file_name_str, pS3bucketNameStr, pS3info, pRuntimeSys)
if gfErr != nil {
return gfErr
}
pRuntimeSys.LogFun("INFO","s3ResponseStr - "+s3ResponseStr)
//--------------------
}
return nil
}
//---------------------------------------------------
// S3__get_image_original_file_s3_filepath returns the S3 filepath of a gf_image's original image.
// Original image is the full-size file that was initially acquired, whether fetched from an external source
// or uploaded via API by other programs or by users via UI's).
// As input it requires a Gf_image struct.
func S3getImageOriginalFileS3filepath(p_image *GFimage,
pRuntimeSys *gf_core.RuntimeSys) string {
// when image is downloaded its renamed to its ID
downloaded_image_filename_str := fmt.Sprintf("%s.%s", p_image.IDstr, p_image.Format_str)
s3_filepath_str := downloaded_image_filename_str
return s3_filepath_str
}
//---------------------------------------------------
func S3getImageFilepath(pImageIDstr GFimageID,
pImageFormatStr string,
pRuntimeSys *gf_core.RuntimeSys) string {
imageFileNameStr := ImageGetFilepathFromID(pImageIDstr, pImageFormatStr) // fmt.Sprintf("%s.%s", p_gf_image_id_str, pImageFormatStr)
s3FilepathStr := imageFileNameStr
return s3FilepathStr
}
//---------------------------------------------------
func S3getImageThumbsS3filepaths(p_image *GFimage,
pRuntimeSys *gf_core.RuntimeSys) (string, string, string) {
thumb_small__s3_filepath_str := strings.Replace(p_image.ThumbnailSmallURLstr, "/images/d", "", 1)
thumb_medium__s3_filepath_str := strings.Replace(p_image.ThumbnailMediumURLstr, "/images/d", "", 1)
thumb_large__s3_filepath_str := strings.Replace(p_image.ThumbnailLargeURLstr, "/images/d", "", 1)
return thumb_small__s3_filepath_str, thumb_medium__s3_filepath_str, thumb_large__s3_filepath_str
}