-
Notifications
You must be signed in to change notification settings - Fork 18
/
base.go
267 lines (236 loc) · 6.98 KB
/
base.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
package media
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"image"
"io"
"mime/multipart"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"time"
"github.com/gosimple/slug"
"github.com/jinzhu/gorm"
"github.com/jinzhu/inflection"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor/resource"
"github.com/qor/qor/utils"
)
// CropOption includes crop options
type CropOption struct {
X, Y, Width, Height int
}
// FileHeader is an interface, for matched values, when call its `Open` method will return `multipart.File`
type FileHeader interface {
Open() (multipart.File, error)
}
type fileWrapper struct {
*os.File
}
func (fileWrapper *fileWrapper) Open() (multipart.File, error) {
return fileWrapper.File, nil
}
// Base defined a base struct for storages
type Base struct {
FileName string
Url string
CropOptions map[string]*CropOption `json:",omitempty"`
Delete bool `json:"-"`
Crop bool `json:"-"`
FileHeader FileHeader `json:"-"`
Reader io.Reader `json:"-"`
Options map[string]string `json:",omitempty"`
cropped bool
}
// Scan scan files, crop options, db values into struct
func (b *Base) Scan(data interface{}) (err error) {
switch values := data.(type) {
case *os.File:
b.FileHeader = &fileWrapper{values}
b.FileName = filepath.Base(values.Name())
case *multipart.FileHeader:
b.FileHeader, b.FileName = values, values.Filename
case []*multipart.FileHeader:
if len(values) > 0 {
if file := values[0]; file.Size > 0 {
b.FileHeader, b.FileName = file, file.Filename
b.Delete = false
}
}
case []byte:
if string(values) != "" {
if err = json.Unmarshal(values, b); err == nil {
var options struct {
Crop bool
Delete bool
}
if err = json.Unmarshal(values, &options); err == nil {
if options.Crop {
b.Crop = true
}
if options.Delete {
b.Delete = true
}
}
}
}
case string:
return b.Scan([]byte(values))
case []string:
for _, str := range values {
if err := b.Scan(str); err != nil {
return err
}
}
default:
err = errors.New("unsupported driver -> Scan pair for MediaLibrary")
}
// If image is deleted, then clean up all values, for serialized fields
if b.Delete {
b.Url = ""
b.FileName = ""
b.CropOptions = nil
}
return
}
// Value return struct's Value
func (b Base) Value() (driver.Value, error) {
if b.Delete {
return nil, nil
}
results, err := json.Marshal(b)
return string(results), err
}
func (b Base) Ext() string {
return strings.ToLower(path.Ext(b.Url))
}
// URL return file's url with given style
func (b Base) URL(styles ...string) string {
if b.Url != "" && len(styles) > 0 {
ext := path.Ext(b.Url)
return fmt.Sprintf("%v.%v%v", strings.TrimSuffix(b.Url, ext), styles[0], ext)
}
return b.Url
}
// String return file's url
func (b Base) String() string {
return b.URL()
}
// GetFileName get file's name
func (b Base) GetFileName() string {
if b.FileName != "" {
return b.FileName
}
if b.Url != "" {
return filepath.Base(b.Url)
}
return ""
}
// GetFileHeader get file's header, this value only exists when saving files
func (b Base) GetFileHeader() FileHeader {
return b.FileHeader
}
// GetURLTemplate get url template
func (b Base) GetURLTemplate(option *Option) (path string) {
if path = option.Get("URL"); path == "" {
path = "/system/{{class}}/{{primary_key}}/{{column}}/{{filename_with_hash}}"
}
return
}
var urlReplacer = regexp.MustCompile("(\\s|\\+)+")
func getFuncMap(scope *gorm.Scope, field *gorm.Field, filename string) template.FuncMap {
hash := func() string { return strings.Replace(time.Now().Format("20060102150405.000000"), ".", "", -1) }
shortHash := func() string { return time.Now().Format("20060102150405") }
return template.FuncMap{
"class": func() string { return inflection.Plural(utils.ToParamString(scope.GetModelStruct().ModelType.Name())) },
"primary_key": func() string { return fmt.Sprintf("%v", scope.PrimaryKeyValue()) },
"column": func() string { return strings.ToLower(field.Name) },
"filename": func() string { return filename },
"basename": func() string { return strings.TrimSuffix(path.Base(filename), path.Ext(filename)) },
"hash": hash,
"short_hash": shortHash,
"filename_with_hash": func() string {
return urlReplacer.ReplaceAllString(fmt.Sprintf("%s.%v%v", slug.Make(strings.TrimSuffix(path.Base(filename), path.Ext(filename))), hash(), path.Ext(filename)), "-")
},
"filename_with_short_hash": func() string {
return urlReplacer.ReplaceAllString(fmt.Sprintf("%s.%v%v", slug.Make(strings.TrimSuffix(path.Base(filename), path.Ext(filename))), shortHash(), path.Ext(filename)), "-")
},
"extension": func() string { return strings.TrimPrefix(path.Ext(filename), ".") },
}
}
// GetURL get default URL for a model based on its options
func (b Base) GetURL(option *Option, scope *gorm.Scope, field *gorm.Field, templater URLTemplater) string {
if path := templater.GetURLTemplate(option); path != "" {
tmpl := template.New("").Funcs(getFuncMap(scope, field, b.GetFileName()))
if tmpl, err := tmpl.Parse(path); err == nil {
var result = bytes.NewBufferString("")
if err := tmpl.Execute(result, scope.Value); err == nil {
return result.String()
}
}
}
return ""
}
// Cropped mark the image to be cropped
func (b *Base) Cropped(values ...bool) (result bool) {
result = b.cropped
for _, value := range values {
b.cropped = value
}
return result
}
// NeedCrop return the file needs to be cropped or not
func (b *Base) NeedCrop() bool {
return b.Crop
}
// GetCropOption get crop options
func (b *Base) GetCropOption(name string) *image.Rectangle {
if cropOption := b.CropOptions[strings.Split(name, "@")[0]]; cropOption != nil {
return &image.Rectangle{
Min: image.Point{X: cropOption.X, Y: cropOption.Y},
Max: image.Point{X: cropOption.X + cropOption.Width, Y: cropOption.Y + cropOption.Height},
}
}
return nil
}
// Retrieve retrieve file content with url
func (b Base) Retrieve(url string) (*os.File, error) {
return nil, errors.New("not implemented")
}
// GetSizes get configured sizes, it will be used to crop images accordingly
func (b Base) GetSizes() map[string]*Size {
return map[string]*Size{}
}
// IsImage return if it is an image
func (b Base) IsImage() bool {
return IsImageFormat(b.URL())
}
func (b Base) IsVideo() bool {
return IsVideoFormat(b.URL())
}
func (b Base) IsSVG() bool {
return IsSVGFormat(b.URL())
}
func init() {
admin.RegisterViewPath("github.com/qor/media/views")
}
// ConfigureQorMetaBeforeInitialize configure this field for Qor Admin
func (Base) ConfigureQorMetaBeforeInitialize(meta resource.Metaor) {
if meta, ok := meta.(*admin.Meta); ok {
if meta.Type == "" {
meta.Type = "file"
}
if meta.GetFormattedValuer() == nil {
meta.SetFormattedValuer(func(value interface{}, context *qor.Context) interface{} {
return utils.Stringify(meta.GetValuer()(value, context))
})
}
}
}