-
Notifications
You must be signed in to change notification settings - Fork 13
/
file.go
75 lines (60 loc) · 1.4 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
package file
import (
"io"
"mime/multipart"
"github.com/coretrix/hitrix/service/component/oss"
)
type File struct {
ID uint64
URL string
Filename string
Namespace oss.Namespace
IDType IDType
}
type Upload struct {
File io.Reader `swaggerignore:"true"`
Filename string
Size int64
ContentType string
}
type RequestDTOUploadFile struct {
Namespace string `form:"namespace" json:"namespace" binding:"required"`
File *multipart.FileHeader `form:"file" json:"file" binding:"required" swaggerignore:"true"`
}
func (r *RequestDTOUploadFile) ToUploadImage() (*RequestDTOUploadImage, func() error, error) {
deferFn := func() error { return nil }
f, err := r.File.Open()
if err != nil {
return nil, deferFn, err
}
deferFn = f.Close
return &RequestDTOUploadImage{
Image: Upload{
File: f,
Filename: r.File.Filename,
Size: r.File.Size,
ContentType: r.File.Header.Get("Content-Type"),
},
Namespace: oss.Namespace(r.Namespace),
}, deferFn, nil
}
type RequestDTOUploadImage struct {
Image Upload
Namespace oss.Namespace
}
type IDType string
const (
IDTypeOSSCounterID IDType = "oss_counter_id"
IDTypeFileID IDType = "file_id"
)
var AllFileIDType = []IDType{
IDTypeOSSCounterID,
IDTypeFileID,
}
func (f IDType) IsValid() bool {
switch f {
case IDTypeOSSCounterID, IDTypeFileID:
return true
}
return false
}