-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload-methods.go
137 lines (111 loc) · 3.46 KB
/
upload-methods.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
package files
import (
"os"
"strconv"
"strings"
"github.com/go-catupiry/catu"
files_helpers "github.com/go-catupiry/files/helpers"
files_processor "github.com/go-catupiry/files/processor"
"github.com/google/uuid"
"github.com/pkg/errors"
)
var defaultExtension = "webp"
var defaultMime = "image/webp"
func UploadFileFromLocalhost(fileName string, description string, filePath string, storageName string, record *FileModel, app catu.App) error {
var err error
filePlugin := app.GetPlugin("files").(*FilePlugin)
storage := filePlugin.GetStorage(storageName)
fileUUID := uuid.New().String()
mimeType, extension, _ := files_helpers.GetFileExtensionAndMimeType(filePath)
if extension != "" {
record.Extension = &extension
record.Mime = &mimeType
} else {
fileNameSplits := strings.Split(fileName, ".")
if len(fileNameSplits) > 1 {
ext := fileNameSplits[len(fileNameSplits)-1]
record.Extension = &ext
fileUUID += "." + ext
}
}
fileStatus, err := os.Stat(filePath)
if err != nil {
return err
}
size := fileStatus.Size()
record.Active = true
record.Name = fileUUID
record.Description = &description
record.Size = &size
record.Originalname = fileName
record.StorageName = storageName
originalDest, _ := storage.GetUploadPathFromFile("original", "", record)
err = storage.UploadFile(record, filePath, originalDest)
if err != nil {
return errors.Wrap(err, "UploadFileFromLocalhost Error on upload file")
}
urls := ImageURL{}
urls["original"], _ = storage.GetUrlFromFile("original", record)
record.SetURLs(urls)
return nil
}
func UploadImageFromLocalhost(fileName string, description string, filePath string, storageName string, record *ImageModel, app catu.App) error {
var err error
filePlugin := app.GetPlugin("files").(*FilePlugin)
storage := filePlugin.GetStorage(storageName)
processor := filePlugin.Processor
fileUUID := uuid.New().String()
styles := filePlugin.ImageStyles
mimeType, extension, _ := files_helpers.GetFileExtensionAndMimeType(filePath)
if extension != "" {
record.Extension = &extension
record.Mime = &mimeType
} else {
fileNameSplits := strings.Split(fileName, ".")
if len(fileNameSplits) > 1 {
ext := fileNameSplits[len(fileNameSplits)-1]
record.Extension = &ext
fileUUID += "." + ext
}
}
fileStatus, err := os.Stat(filePath)
if err != nil {
return err
}
size := fileStatus.Size()
record.Active = true
record.Description = &description
record.Size = &size
record.Originalname = fileName
record.StorageName = storageName
record.Extension = &defaultExtension
record.Mime = &defaultMime
record.Name = fileUUID + "." + defaultExtension
originalDest, _ := storage.GetUploadPathFromFile("original", "", record)
var resizeOpts files_processor.Options
// ORIGINAL:
if style, ok := styles["original"]; ok {
// set original format to override / resize default format
resizeOpts = files_processor.Options{
"width": strconv.Itoa(int(style.Width)),
"height": strconv.Itoa(int(style.Height)),
"format": style.Format,
}
} else {
// Default:
resizeOpts = files_processor.Options{
"width": strconv.Itoa(int(filePlugin.MaxImageWidth)),
"height": strconv.Itoa(int(filePlugin.MaxImageHeight)),
}
}
err = processor.Resize(filePath, filePath, record.Name, resizeOpts)
if err != nil {
return err
}
err = storage.UploadFile(record, filePath, originalDest)
if err != nil {
return errors.Wrap(err, "UploadImageFromLocalhost Error on upload file")
}
record.ResetURLs(app)
return nil
}