package item
import (
"fmt"
"time"
"github.com/haturatu/ponzu/management/editor"
)
// FileUpload represents the file uploaded to the system
type FileUpload struct {
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 html to edit a Post and partially implements editor.Editable
func (f *FileUpload) MarshalEditor() ([]byte, error) {
view, err := editor.Form(f,
editor.Field{
View: func() []byte {
if f.Path == "" {
return nil
}
return []byte(`
`)
}(),
},
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(`
`)
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)"
}