From 099d000119447708d7d0d0482758d352438fa7e5 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Apr 2017 13:23:37 -0700 Subject: adding support for file upload type and API handler to fetch file info --- system/item/file.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 system/item/file.go (limited to 'system/item') diff --git a/system/item/file.go b/system/item/file.go new file mode 100644 index 0000000..3b33b04 --- /dev/null +++ b/system/item/file.go @@ -0,0 +1,96 @@ +package item + +import ( + "fmt" + + "github.com/ponzu-cms/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: []byte(` +
+ + +

` + f.Name + `

+ + +

File information:

+ +
+ `), + }, + ) + if err != nil { + return nil, err + } + + open := []byte(` +
+
+
File Uploads
+
+
+ `) + close := []byte(`
`) + script := []byte(` + + `) + + view = append(open, view...) + view = append(view, close...) + view = append(view, script...) + + return view, nil +} + +func (f *FileUpload) Push() []string { + return []string{ + "path", + } +} -- cgit v1.2.3 From a48ed8dc7f7ddb3ddbc8ea54ad0b0d2feb7c87f0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Sat, 29 Apr 2017 14:39:35 -0700 Subject: fmt bytes to readable and update setupload logic to allow edits --- system/item/file.go | 96 ----------------------------------- system/item/upload.go | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 96 deletions(-) delete mode 100644 system/item/file.go create mode 100644 system/item/upload.go (limited to 'system/item') diff --git a/system/item/file.go b/system/item/file.go deleted file mode 100644 index 3b33b04..0000000 --- a/system/item/file.go +++ /dev/null @@ -1,96 +0,0 @@ -package item - -import ( - "fmt" - - "github.com/ponzu-cms/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: []byte(` -
- - -

` + f.Name + `

- - -

File information:

-
    -
  • Content-Length: ` + fmt.Sprintf("%d", f.ContentLength) + `
  • -
  • Content-Type: ` + f.ContentType + `
  • -
-
- `), - }, - ) - if err != nil { - return nil, err - } - - open := []byte(` -
-
-
File Uploads
-
-
- `) - close := []byte(`
`) - script := []byte(` - - `) - - view = append(open, view...) - view = append(view, close...) - view = append(view, script...) - - return view, nil -} - -func (f *FileUpload) Push() []string { - return []string{ - "path", - } -} diff --git a/system/item/upload.go b/system/item/upload.go new file mode 100644 index 0000000..9253fcc --- /dev/null +++ b/system/item/upload.go @@ -0,0 +1,138 @@ +package item + +import ( + "fmt" + + "github.com/ponzu-cms/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(` +
+ + +

` + f.Name + `

+ + +

File information:

+
    +
  • Content-Length: ` + fmt.Sprintf("%s", FmtBytes(float64(f.ContentLength))) + `
  • +
  • Content-Type: ` + f.ContentType + `
  • +
+
+ `) + }(), + }, + editor.Field{ + View: editor.File("Path", f, map[string]string{ + "label": "File Upload", + "placeholder": "Upload the file here", + }), + }, + ) + if err != nil { + return nil, err + } + + open := []byte(` +
+
+
File Uploads
+
+
+ `) + close := []byte(`
`) + script := []byte(` + + `) + + view = append(open, view...) + view = append(view, close...) + 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) + } + +} -- cgit v1.2.3 From 3c8c848606b996e2c7a06331401e622f888b84c5 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Sat, 29 Apr 2017 22:43:44 -0500 Subject: adding search, edit/new, and list view for uploads --- system/item/upload.go | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'system/item') diff --git a/system/item/upload.go b/system/item/upload.go index 9253fcc..800f663 100644 --- a/system/item/upload.go +++ b/system/item/upload.go @@ -2,6 +2,7 @@ package item import ( "fmt" + "time" "github.com/ponzu-cms/ponzu/management/editor" ) @@ -30,15 +31,12 @@ func (f *FileUpload) MarshalEditor() ([]byte, error) { return []byte(`
- -

` + f.Name + `

- - -

File information:

+
` + f.Name + `
    -
  • Content-Length: ` + fmt.Sprintf("%s", FmtBytes(float64(f.ContentLength))) + `
  • -
  • Content-Type: ` + f.ContentType + `
  • +
  • Content-Length: ` + fmt.Sprintf("%s", FmtBytes(float64(f.ContentLength))) + `
  • +
  • Content-Type: ` + f.ContentType + `
  • +
  • Uploaded: ` + FmtTime(f.Timestamp) + `
`) @@ -55,17 +53,13 @@ func (f *FileUpload) MarshalEditor() ([]byte, error) { return nil, err } - open := []byte(` -
-
-
File Uploads
-
-
- `) - close := []byte(`
`) script := []byte(` `) - view = append(open, view...) - view = append(view, close...) view = append(view, script...) return view, nil @@ -136,3 +133,8 @@ func FmtBytes(size float64) string { } } + +// 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)" +} -- cgit v1.2.3