diff options
Diffstat (limited to 'system/item')
-rw-r--r-- | system/item/upload.go (renamed from system/item/file.go) | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/system/item/file.go b/system/item/upload.go index 3b33b04..9253fcc 100644 --- a/system/item/file.go +++ b/system/item/upload.go @@ -23,20 +23,32 @@ func (f *FileUpload) String() string { return f.Name } func (f *FileUpload) MarshalEditor() ([]byte, error) { view, err := editor.Form(f, editor.Field{ - View: []byte(` + View: func() []byte { + if f.Path == "" { + return nil + } + + return []byte(` <div class="input-field col s12"> - <label class="active">{{ .Name }}</label> + <label class="active">` + f.Name + `</label> <!-- Add your custom editor field view here. --> <h4>` + f.Name + `</h4> <img class="preview" src="` + f.Path + `"/> <p>File information:</p> <ul> - <li>Content-Length: ` + fmt.Sprintf("%d", f.ContentLength) + `</li> + <li>Content-Length: ` + fmt.Sprintf("%s", FmtBytes(float64(f.ContentLength))) + `</li> <li>Content-Type: ` + f.ContentType + `</li> </ul> </div> - `), + `) + }(), + }, + editor.Field{ + View: editor.File("Path", f, map[string]string{ + "label": "File Upload", + "placeholder": "Upload the file here", + }), }, ) if err != nil { @@ -94,3 +106,33 @@ func (f *FileUpload) Push() []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) + } + +} |