github.com/rpdict/ponzu@v0.10.1-0.20190226054626-477f29d6bf5e/system/item/upload.go (about) 1 package item 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/rpdict/ponzu/management/editor" 8 ) 9 10 // FileUpload represents the file uploaded to the system 11 type FileUpload struct { 12 Item 13 14 Name string `json:"name"` 15 Path string `json:"path"` 16 ContentLength int64 `json:"content_length"` 17 ContentType string `json:"content_type"` 18 } 19 20 // String partially implements item.Identifiable and overrides Item's String() 21 func (f *FileUpload) String() string { return f.Name } 22 23 // MarshalEditor writes a buffer of html to edit a Post and partially implements editor.Editable 24 func (f *FileUpload) MarshalEditor() ([]byte, error) { 25 view, err := editor.Form(f, 26 editor.Field{ 27 View: func() []byte { 28 if f.Path == "" { 29 return nil 30 } 31 32 return []byte(` 33 <div class="input-field col s12"> 34 <!-- Add your custom editor field view here. --> 35 <h5>` + f.Name + `</h5> 36 <ul> 37 <li><span class="grey-text text-lighten-1">Content-Length:</span> ` + fmt.Sprintf("%s", FmtBytes(float64(f.ContentLength))) + `</li> 38 <li><span class="grey-text text-lighten-1">Content-Type:</span> ` + f.ContentType + `</li> 39 <li><span class="grey-text text-lighten-1">Uploaded:</span> ` + FmtTime(f.Timestamp) + `</li> 40 </ul> 41 </div> 42 `) 43 }(), 44 }, 45 editor.Field{ 46 View: editor.File("Path", f, map[string]string{ 47 "label": "File Upload", 48 "placeholder": "Upload the file here", 49 }), 50 }, 51 ) 52 if err != nil { 53 return nil, err 54 } 55 56 script := []byte(` 57 <script> 58 $(function() { 59 // change form action to upload-specific endpoint 60 var form = $('form'); 61 form.attr('action', '/admin/edit/upload'); 62 63 // hide default fields & labels unnecessary for the config 64 var fields = $('.default-fields'); 65 fields.css('position', 'relative'); 66 fields.find('input:not([type=submit])').remove(); 67 fields.find('label').remove(); 68 fields.find('button').css({ 69 position: 'absolute', 70 top: '-10px', 71 right: '0px' 72 }); 73 74 var contentOnly = $('.content-only.__ponzu'); 75 contentOnly.hide(); 76 contentOnly.find('input, textarea, select').attr('name', ''); 77 78 // adjust layout of td so save button is in same location as usual 79 fields.find('td').css('float', 'right'); 80 81 // stop some fixed config settings from being modified 82 fields.find('input[name=client_secret]').attr('name', ''); 83 84 // hide save, show delete 85 if ($('h5').length > 0) { 86 fields.find('.save-post').hide(); 87 fields.find('.delete-post').show(); 88 } else { 89 fields.find('.save-post').show(); 90 fields.find('.delete-post').hide(); 91 } 92 }); 93 </script> 94 `) 95 96 view = append(view, script...) 97 98 return view, nil 99 } 100 101 func (f *FileUpload) Push() []string { 102 return []string{ 103 "path", 104 } 105 } 106 107 // FmtBytes converts the numeric byte size value to the appropriate magnitude 108 // size in KB, MB, GB, TB, PB, or EB. 109 func FmtBytes(size float64) string { 110 unit := float64(1024) 111 BYTE := unit 112 KBYTE := BYTE * unit 113 MBYTE := KBYTE * unit 114 GBYTE := MBYTE * unit 115 TBYTE := GBYTE * unit 116 PBYTE := TBYTE * unit 117 118 switch { 119 case size < BYTE: 120 return fmt.Sprintf("%0.f B", size) 121 case size < KBYTE: 122 return fmt.Sprintf("%.1f KB", size/BYTE) 123 case size < MBYTE: 124 return fmt.Sprintf("%.1f MB", size/KBYTE) 125 case size < GBYTE: 126 return fmt.Sprintf("%.1f GB", size/MBYTE) 127 case size < TBYTE: 128 return fmt.Sprintf("%.1f TB", size/GBYTE) 129 case size < PBYTE: 130 return fmt.Sprintf("%.1f PB", size/TBYTE) 131 default: 132 return fmt.Sprintf("%0.f B", size) 133 } 134 135 } 136 137 // FmtTime shows a human readable time based on the timestamp 138 func FmtTime(t int64) string { 139 return time.Unix(t/1000, 0).Format("03:04 PM Jan 2, 2006") + " (UTC)" 140 }