forked from ponzu-cms/ponzu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
171 lines (145 loc) · 4.41 KB
/
upload.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
package entities
import (
"fmt"
"github.com/fanky5g/ponzu/config"
"github.com/fanky5g/ponzu/content/editor"
"github.com/fanky5g/ponzu/content/item"
"path/filepath"
"reflect"
"time"
)
// FileUpload represents the file uploaded to the system
type FileUpload struct {
item.Item
Name string `json:"name"`
Path string `json:"path"`
ContentLength int64 `json:"content_length"`
ContentType string `json:"content_type"`
}
// String partially implements item.Identifiable and overrides Item's String()
func (f *FileUpload) String() string { return f.Name }
// MarshalEditor writes a buffer of templates to edit a Post and partially implements editor.Editable
func (f *FileUpload) MarshalEditor(paths config.Paths) ([]byte, error) {
f.Path = filepath.Join(paths.PublicPath, f.Path)
view, err := editor.Form(f,
paths,
editor.Field{
View: func() []byte {
if f.Path == "" {
return nil
}
return []byte(`
<div class="input-field col s12">
<h5>` + f.Name + `</h5>
<ul>
<li><span class="grey-text text-lighten-1">Content-Length:</span> ` + fmt.Sprintf("%s", FmtBytes(float64(f.ContentLength))) + `</li>
<li><span class="grey-text text-lighten-1">Content-Type:</span> ` + f.ContentType + `</li>
<li><span class="grey-text text-lighten-1">Uploaded:</span> ` + FmtTime(f.Timestamp) + `</li>
</ul>
</div>
`)
}(),
},
editor.Field{
View: editor.File("Path", f, map[string]string{
"label": "File Upload",
"placeholder": "Upload the file here",
}),
},
)
if err != nil {
return nil, err
}
script := []byte(`
<script>
$(function() {
// change form action to storage-specific endpoint
var form = $('form');
form.attr('action', '` + paths.PublicPath + `/edit/upload');
// hide default fields & labels unnecessary for the config
var fields = $('.default-fields');
fields.css('position', 'relative');
fields.find('input:not([type=submit])').remove();
fields.find('label').remove();
fields.find('button').css({
position: 'absolute',
top: '-10px',
right: '0px'
});
var contentOnly = $('.entities-only.__ponzu');
contentOnly.hide();
contentOnly.find('input, textarea, select').attr('name', '');
// adjust layout of td so save button is in same location as usual
fields.find('td').css('float', 'right');
// stop some fixed config settings from being modified
fields.find('input[name=client_secret]').attr('name', '');
// hide save, show delete
if ($('h5').length > 0) {
fields.find('.save-post').hide();
fields.find('.delete-post').show();
} else {
fields.find('.save-post').show();
fields.find('.delete-post').hide();
}
});
</script>
`)
view = append(view, script...)
return view, nil
}
func (f *FileUpload) Push() []string {
return []string{
"path",
}
}
// FmtBytes converts the numeric byte size value to the appropriate magnitude
// size in KB, MB, GB, TB, PB, or EB.
func FmtBytes(size float64) string {
unit := float64(1024)
BYTE := unit
KBYTE := BYTE * unit
MBYTE := KBYTE * unit
GBYTE := MBYTE * unit
TBYTE := GBYTE * unit
PBYTE := TBYTE * unit
switch {
case size < BYTE:
return fmt.Sprintf("%0.f B", size)
case size < KBYTE:
return fmt.Sprintf("%.1f KB", size/BYTE)
case size < MBYTE:
return fmt.Sprintf("%.1f MB", size/KBYTE)
case size < GBYTE:
return fmt.Sprintf("%.1f GB", size/MBYTE)
case size < TBYTE:
return fmt.Sprintf("%.1f TB", size/GBYTE)
case size < PBYTE:
return fmt.Sprintf("%.1f PB", size/TBYTE)
default:
return fmt.Sprintf("%0.f B", size)
}
}
// FmtTime shows a human-readable time based on the timestamp
func FmtTime(t int64) string {
return time.Unix(t/1000, 0).Format("03:04 PM Jan 2, 2006") + " (UTC)"
}
// IndexContent determines if FileUpload should be indexed for searching
func (f *FileUpload) IndexContent() bool {
return true
}
// GetSearchableAttributes defines fields that should be indexed
func (f *FileUpload) GetSearchableAttributes() map[string]reflect.Type {
searchableAttributes := make(map[string]reflect.Type)
idField := "ID"
v := reflect.Indirect(reflect.ValueOf(f))
t := v.Type()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldName := t.Field(i).Name
if fieldName != idField && field.Kind() == reflect.String {
searchableAttributes[fieldName] = field.Type()
}
}
searchableAttributes[idField] = v.FieldByName(idField).Type()
return searchableAttributes
}