-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
116 lines (103 loc) · 3 KB
/
helper.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
package gofm
import (
"errors"
"github.com/google/uuid"
"github.com/kyaxcorp/gofile/driver/filesystem/helper"
"os"
"path/filepath"
"reflect"
"time"
)
func structFieldExists(obj reflect.Value, fieldName string) bool {
fieldVal := obj.FieldByName(fieldName)
if fieldVal == (reflect.Value{}) {
return false
}
return true
}
func structGetFieldVal(obj reflect.Value, fieldName string) (interface{}, error) {
fieldVal := obj.FieldByName(fieldName)
if fieldVal == (reflect.Value{}) {
return nil, errors.New("struct field doesn't exist")
}
return fieldVal.Interface(), nil
}
func structSetFieldVal(obj reflect.Value, fieldName string, fieldValue interface{}) error {
if !structFieldExists(obj, fieldName) {
return errors.New("struct field " + fieldName + " doesn't exist")
}
f := obj.FieldByName(fieldName)
v := reflect.ValueOf(fieldValue)
objFieldType := f.Type().String()
valFieldType := v.Type().String()
if f.CanSet() {
if objFieldType == "uuid.UUID" && valFieldType == "*uuid.UUID" {
realVal := fieldValue.(*uuid.UUID)
v = reflect.ValueOf(*realVal)
f.Set(v)
return nil
} else if objFieldType == "*uuid.UUID" && valFieldType == "uuid.UUID" {
realVal := fieldValue.(uuid.UUID)
v = reflect.ValueOf(&realVal)
f.Set(v)
return nil
} else if objFieldType == "gofm.UUID" && valFieldType == "*gofm.UUID" {
realVal := fieldValue.(*UUID)
v = reflect.ValueOf(*realVal)
f.Set(v)
return nil
} else if objFieldType == "*gofm.UUID" && valFieldType == "gofm.UUID" {
realVal := fieldValue.(UUID)
v = reflect.ValueOf(&realVal)
f.Set(v)
return nil
} else if objFieldType == "time.Time" && valFieldType == "*time.Time" {
realVal := fieldValue.(*time.Time)
v = reflect.ValueOf(*realVal)
f.Set(v)
return nil
} else if objFieldType == "*time.Time" && valFieldType == "time.Time" {
realVal := fieldValue.(time.Time)
v = reflect.ValueOf(&realVal)
f.Set(v)
return nil
}
f.Set(v)
return nil
}
return errors.New("can't set field value for struct field " + fieldName)
}
type temporaryFileOptions struct {
SelfDestruct bool
SelfDestructIn time.Duration
FileName string
FileData []byte
}
func generateTmpFile(o temporaryFileOptions) (string, error) {
tmpFolderID, _err := uuid.NewRandom()
if _err != nil {
return "", _err
}
// create the temporary folder where to save the file
tmpFolder := os.TempDir() + filepath.FromSlash("/") + tmpFolderID.String()
if !helper.FolderExists(tmpFolder) {
_err = helper.MkDir(tmpFolder, 0751)
if _err != nil {
return "", _err
}
}
// Delete the folder and the files inside it after copying the destination locations
if o.SelfDestruct {
time.AfterFunc(o.SelfDestructIn, func() {
helper.FolderDelete(tmpFolder)
})
}
//defer helper.FolderDelete(tmpFolder)
// Write the file to that generated folder
tmpFileFullPath := tmpFolder + filepath.FromSlash("/") + o.FileName
_err = os.WriteFile(tmpFileFullPath, o.FileData, 0751)
if _err != nil {
return "", _err
}
return tmpFileFullPath, nil
}