-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
113 lines (95 loc) · 2.94 KB
/
file.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
package files
import (
"github.com/go-catupiry/catu"
"gorm.io/gorm"
)
var ImageMimeTypes = []string{"image/png",
"image/jpg",
"image/jpeg",
"image/gif",
"image/bmp",
"image/x-icon",
"image/tiff",
"image/heic",
"image/heif",
"image/vnd.microsoft.icon",
}
// Field configuration interface implements basic file and image fields logic
type FieldConfigurationInterface interface {
IsFormFieldMultiple() bool
SetFormFieldMultiple(v bool) error
GetModelName() string
SetModelName(name string) error
GetFieldName() string
SetFieldName(name string) error
Clear(modelId string) error
ClearField(modelId string) error
}
// File field configuration to associate contents with terms
type FieldConfiguration struct {
DB *gorm.DB
AssociationModel interface{}
ModelToAssociate interface{}
FormFieldMultiple bool
ModelName string
FieldName string
}
func (f *FieldConfiguration) IsFormFieldMultiple() bool {
return f.FormFieldMultiple
}
func (f *FieldConfiguration) SetFormFieldMultiple(v bool) error {
f.FormFieldMultiple = v
return nil
}
func (f *FieldConfiguration) GetModelName() string {
return f.ModelName
}
func (f *FieldConfiguration) SetModelName(name string) error {
f.ModelName = name
return nil
}
func (f *FieldConfiguration) GetFieldName() string {
return f.FieldName
}
func (f *FieldConfiguration) SetFieldName(name string) error {
f.FieldName = name
return nil
}
// Delete all records (fiels, images, etc) associated with that record
func (f *FieldConfiguration) Clear(modelID string) error {
return f.DB.Where("modelId = ? AND modelName = ?", modelID, f.GetModelName()).Delete(&f.AssociationModel).Error
}
func (f *FieldConfiguration) ClearField(modelID string) error {
return f.DB.Where("modelId = ? AND field = ? AND modelName = ?", modelID, f.GetFieldName(), f.GetModelName()).Delete(&f.AssociationModel).Error
}
// Create a new field configuration with default image settings
func NewImageFieldConfiguration(modelName, fieldName string) FieldConfigurationInterface {
db := catu.GetDefaultDatabaseConnection()
return &FieldConfiguration{
DB: db,
FormFieldMultiple: false,
ModelName: modelName,
FieldName: fieldName,
AssociationModel: ImageAssocsModel{},
ModelToAssociate: ImageModel{},
}
}
// Create a new field configuration with default file settings
func NewFileFieldConfiguration(modelName, fieldName string) FieldConfigurationInterface {
db := catu.GetDefaultDatabaseConnection()
return &FieldConfiguration{
DB: db,
FormFieldMultiple: true,
ModelName: modelName,
FieldName: fieldName,
AssociationModel: FileAssocsModel{},
ModelToAssociate: FileModel{},
}
}
func BuidFileBaseURL(app catu.App) string {
cfg := app.GetConfiguration()
port := cfg.GetF("PORT", "8080")
protocol := cfg.GetF("PROTOCOL", "http")
domain := cfg.GetF("DOMAIN", "localhost")
return cfg.GetF("APP_ORIGIN", protocol+"://"+domain+":"+port)
}