From 78e5acdfcca964e9e4e5d4be78b4adb561742f38 Mon Sep 17 00:00:00 2001 From: Wim Brand Date: Thu, 4 Oct 2018 23:36:34 +0200 Subject: [PATCH] api update --- pixelapi/file.go | 8 ++--- pixelapi/list.go | 25 +++++++-------- res/static/res/script/DetailsWindow.js | 8 ++--- res/static/res/script/ListNavigator.js | 6 ++-- res/static/res/script/Viewer.js | 16 +++++----- res/static/res/script/history.js | 4 +-- res/static/res/style/layout.css | 2 +- res/template/account/file_manager.html | 4 +-- res/template/account/user_files.html | 4 +-- res/template/account/user_home.html | 4 +-- res/template/fragments/api/file.html | 4 +-- res/template/fragments/api/filesystem.html | 4 +-- res/template/fragments/api/list.html | 14 ++++---- webcontroller/filePreview.go | 8 ++--- webcontroller/fileViewer.go | 6 ++-- webcontroller/ogData.go | 4 +-- webcontroller/webcontroller.go | 37 ++++++++++++---------- 17 files changed, 81 insertions(+), 77 deletions(-) diff --git a/pixelapi/file.go b/pixelapi/file.go index dcfba47..33739a5 100644 --- a/pixelapi/file.go +++ b/pixelapi/file.go @@ -15,11 +15,11 @@ func (p *PixelAPI) GetFile(id string) (io.ReadCloser, error) { type FileInfo struct { Success bool `json:"success"` ID string `json:"id"` - FileName string `json:"file_name"` + Name string `json:"name"` + Size uint64 `json:"size"` + Views int `json:"views"` DateUpload time.Time `json:"date_upload"` - DateLastview time.Time `json:"date_last_view"` - FileSize uint64 `json:"file_size"` - Views uint `json:"views"` + DateLastView time.Time `json:"date_last_view"` MimeType string `json:"mime_type"` MimeImage string `json:"mime_image"` ThumbnailHREF string `json:"thumbnail_href"` diff --git a/pixelapi/list.go b/pixelapi/list.go index 6b099d9..25bdd0c 100644 --- a/pixelapi/list.go +++ b/pixelapi/list.go @@ -9,23 +9,22 @@ const ( // List information object from the pixeldrain API type List struct { - Success bool `json:"success"` - ID string `json:"id"` - Title string `json:"title"` - DateCreated time.Time `json:"date_created"` - FileCount int `json:"file_count"` - Files []ListFile + Success bool `json:"success"` + ID string `json:"id"` + Title string `json:"title"` + DateCreated time.Time `json:"date_created"` + FileCount int `json:"file_count"` + Files []ListFile `json:"files,omitempty"` } // ListFile information object from the pixeldrain API type ListFile struct { - ID string `json:"id"` - DetailHREF string `json:"detail_href"` - FileName string `json:"file_name"` - Description string `json:"description"` - DateCreated time.Time `json:"date_created"` - DateLastView time.Time `json:"date_last_view"` - ListDescription string `json:"list_description"` + ID string `json:"id"` + DetailHREF string `json:"detail_href"` + Name string `json:"name"` + Description string `json:"description"` + DateCreated time.Time `json:"date_created"` + DateLastView time.Time `json:"date_last_view"` } // GetList get a List from the pixeldrain API. Errors will be available through diff --git a/res/static/res/script/DetailsWindow.js b/res/static/res/script/DetailsWindow.js index 5aceaa9..6f34ea1 100644 --- a/res/static/res/script/DetailsWindow.js +++ b/res/static/res/script/DetailsWindow.js @@ -22,11 +22,11 @@ var DetailsWindow = { success: function(data){ $("#info_file_details").html( "" - + "" + + "" + "" + "" + "" - + "" + + "" + "" + "" + "
Name" + escapeHTML(data.file_name) + "
Name" + escapeHTML(data.name) + "
Url/u/" + data.id + "
Mime Type" + escapeHTML(data.mime_type) + "
Id" + data.id + "
Size" + data.file_size + "
Size" + data.size + "
Upload Date" + data.date_upload + "
Description" + escapeHTML(file.description) + "
" @@ -36,10 +36,10 @@ var DetailsWindow = { } else { $("#info_file_details").html( "" - + "" + + "" + "" + "" - + "" + + "" + "" + "
Name" + escapeHTML(file.file_name) + "
Name" + escapeHTML(file.name) + "
Mime Type" + escapeHTML(file.mime_type) + "
Id" + file.id + "
Size" + file.file_size + "
Size" + file.size + "
Upload Date" + file.date_upload + "
" ); diff --git a/res/static/res/script/ListNavigator.js b/res/static/res/script/ListNavigator.js index 50fd2a2..e905e72 100644 --- a/res/static/res/script/ListNavigator.js +++ b/res/static/res/script/ListNavigator.js @@ -127,7 +127,7 @@ var ListNavigator = { for (i = startPos; i <= endPos; i++){ var thumb = "/api/file/" + this.data[i].id + "/thumbnail"; - var name = this.data[i].file_name; + var name = this.data[i].name; var itemHtml = escapeHTML(name) + "
" + "/g, '>') .replace(/"/g, '"'); -} \ No newline at end of file +} diff --git a/res/static/res/script/history.js b/res/static/res/script/history.js index c83a3e6..2049203 100644 --- a/res/static/res/script/history.js +++ b/res/static/res/script/history.js @@ -50,8 +50,8 @@ function historyAddItem(json) { var uploadItem = '' + '" - + ''+json.file_name+'' + + "alt=\"" + json.name + "\" />" + + ''+json.name+'' + "
" + date.getFullYear() + "-" + ("00" + (date.getMonth() + 1)).slice(-2) + "-" diff --git a/res/static/res/style/layout.css b/res/static/res/style/layout.css index 2f6bedd..48be0e0 100644 --- a/res/static/res/style/layout.css +++ b/res/static/res/style/layout.css @@ -250,7 +250,7 @@ select{ border: none; margin: 2px; background: linear-gradient(var(--input_color), var(--input_color_dark)); - padding: 6px 10px 6px 10px; + padding: 6px 8px 6px 8px; box-shadow: 2px 2px 8px var(--shadow_color); font-weight: bold; font-size: 0.85em; diff --git a/res/template/account/file_manager.html b/res/template/account/file_manager.html index 063f8fc..4c941af 100644 --- a/res/template/account/file_manager.html +++ b/res/template/account/file_manager.html @@ -50,8 +50,8 @@ {{$files := .PixelAPI.UserFiles 0 20}} {{range $files.Files}}
- {{.FileName}} - {{.FileName}} + {{.File}} + {{.File}} {{end}} diff --git a/res/template/account/user_files.html b/res/template/account/user_files.html index 00b696e..d3474ed 100644 --- a/res/template/account/user_files.html +++ b/res/template/account/user_files.html @@ -17,8 +17,8 @@ {{$files := .PixelAPI.UserFiles 0 1000}} {{range $files.Files}} - {{.FileName}} - {{.FileName}} + {{.Name}} + {{.Name}}
{{.DateUpload.Format "2006-01-02 15:04:05"}}
diff --git a/res/template/account/user_home.html b/res/template/account/user_home.html index 930f035..cbd5948 100644 --- a/res/template/account/user_home.html +++ b/res/template/account/user_home.html @@ -15,8 +15,8 @@ {{$files := .PixelAPI.UserFiles 0 18}} {{range $files.Files}} - {{.FileName}} - {{.FileName}} + {{.Name}} + {{.Name}}
{{.DateUpload.Format "2006-01-02 15:04:05"}}
diff --git a/res/template/fragments/api/file.html b/res/template/fragments/api/file.html index 3c8d034..d8598fa 100644 --- a/res/template/fragments/api/file.html +++ b/res/template/fragments/api/file.html @@ -144,10 +144,10 @@ { "success": true, "id": "123abc", - "file_name": "screenshot.png", + "name": "screenshot.png", "date_upload": 1485894987, // Timestamp "date_last_view": 1485894987, // Timestamp - "file_size": 5694837, // Bytes + "size": 5694837, // Bytes "views" 1234, // Amount of unique file views "mime_type" "image/png", "description": "File description", diff --git a/res/template/fragments/api/filesystem.html b/res/template/fragments/api/filesystem.html index 11e3a71..8079a02 100644 --- a/res/template/fragments/api/filesystem.html +++ b/res/template/fragments/api/filesystem.html @@ -117,10 +117,10 @@ "file_info": { "success": true, "id": "Jf_u5TI9", - "file_name": "11. Lenny Kravitz - Fly away.ogg", + "name": "11. Lenny Kravitz - Fly away.ogg", "date_upload": "2018-07-04T22:24:48Z", "date_last_view": "2018-07-04T22:24:48Z", - "file_size": 9757269, + "size": 9757269, "views": 0, "mime_type": "application/ogg", "thumbnail_href": "/file/Jf_u5TI9/thumbnail" diff --git a/res/template/fragments/api/list.html b/res/template/fragments/api/list.html index fb49786..4b05ae2 100644 --- a/res/template/fragments/api/list.html +++ b/res/template/fragments/api/list.html @@ -39,9 +39,11 @@
HTTP 422: Unprocessable Entity
 {
 	"success": false,
-	"value": "file_not_found",
-	"id": "Oh42No", // The file you tried to add with this ID does not exist
-	"message": "File Oh42No was not found in the database."
+	"value": "list_file_not_found",
+	"message": "File Oh42No was not found in the database.",
+	"extra": {
+		"file_not_found": "0h42No" // The file you tried to add with this ID does not exist
+	}
 }
 
HTTP 413: Payload too large
@@ -124,7 +126,7 @@
 		{
 			"detail_href": "/file/_SqVWi/info",
 			"id": "_SqVWi",
-			"file_name": "01 Holy Wars... The Punishment Due.mp3",
+			"name": "01 Holy Wars... The Punishment Due.mp3",
 			"description": "",
 			"date_created": 1513033304,
 			"date_last_view": 1513033304
@@ -132,7 +134,7 @@
 		{
 			"detail_href": "/file/RKwgZb/info",
 			"id": "RKwgZb",
-			"file_name": "02 Hangar 18.mp3",
+			"name": "02 Hangar 18.mp3",
 			"description": "",
 			"date_created": 1513033304,
 			"date_last_view": 1513033304
@@ -140,7 +142,7 @@
 		{
 			"detail_href": "/file/DRaL_e/info",
 			"id": "DRaL_e",
-			"file_name": "03 Take No Prisoners.mp3",
+			"name": "03 Take No Prisoners.mp3",
 			"description": "",
 			"date_created": 1513033304,
 			"date_last_view": 1513033304
diff --git a/webcontroller/filePreview.go b/webcontroller/filePreview.go
index 1f6563f..0558528 100644
--- a/webcontroller/filePreview.go
+++ b/webcontroller/filePreview.go
@@ -78,7 +78,7 @@ func (f filePreview) run(inf *pixelapi.FileInfo) string {
 		return f.pdf()
 	case
 		"application/octet-stream": // Fallback for when mime type not recognized
-		switch filepath.Ext(f.FileInfo.FileName) {
+		switch filepath.Ext(f.FileInfo.Name) {
 		case
 			".mp3":
 			return f.audio()
@@ -110,7 +110,7 @@ func (f filePreview) audio() string {
 
 
 `,
-		f.FileInfo.FileName,
+		f.FileInfo.Name,
 		f.FileURL,
 	)
 }
@@ -138,7 +138,7 @@ func (f filePreview) text() string {
 
%s
` - if f.FileInfo.FileSize > 1e6 { // Prevent out of memory errors + if f.FileInfo.Size > 1e6 { // Prevent out of memory errors return fmt.Sprintf(htmlOut, "", "File is too large to view online.\nPlease download and view it locally.", ) @@ -191,7 +191,7 @@ seamless="seamless" frameborder="0" allowtransparency="true" func (f filePreview) def() string { return fmt.Sprintf( `%s
%s
`, - f.FileInfo.FileName, + f.FileInfo.Name, f.FileInfo.MimeType, f.DownloadURL, f.APIURL+f.FileInfo.ThumbnailHREF, diff --git a/webcontroller/fileViewer.go b/webcontroller/fileViewer.go index f4425de..49c72bb 100644 --- a/webcontroller/fileViewer.go +++ b/webcontroller/fileViewer.go @@ -61,7 +61,7 @@ func (wc *WebController) serveFileViewer(w http.ResponseWriter, r *http.Request, }) } else { err = wc.templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{ - "Title": fmt.Sprintf("%s ~ Pixeldrain file", finfo[0].FileName), + "Title": fmt.Sprintf("%s ~ Pixeldrain file", finfo[0].Name), "APIResponse": finfo[0], "Type": "file", "OGData": ogData.FromFile(*finfo[0]), @@ -80,10 +80,10 @@ func (wc *WebController) serveFileViewerDemo(w http.ResponseWriter) { wc.templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{ "APIResponse": map[string]interface{}{ "id": "demo", - "file_name": "Demo file", + "name": "Demo file", "date_upload": "2017-01-01 12:34:56", "date_lastview": "2017-01-01 12:34:56", - "file_size": 123456789, + "size": 123456789, "views": 1, "mime_type": "text/demo", "description": "A file to demonstrate the viewer page", diff --git a/webcontroller/ogData.go b/webcontroller/ogData.go index 89d0383..9214dd5 100644 --- a/webcontroller/ogData.go +++ b/webcontroller/ogData.go @@ -18,10 +18,10 @@ type OGData struct { // FromFile populates the OGData object from FileInfo. It returns itself for // compactness. func (d *OGData) FromFile(f pixelapi.FileInfo) *OGData { - d.Title = f.FileName + d.Title = f.Name d.Type = "website" d.SiteName = "Pixeldrain" - d.Description = "View " + f.FileName + " on Pixeldrain" + d.Description = "View " + f.Name + " on Pixeldrain" d.URL = "/u/" + f.ID d.Image = "/api/file/" + f.ID + "/thumbnail" return d diff --git a/webcontroller/webcontroller.go b/webcontroller/webcontroller.go index 36d4f62..2e72787 100644 --- a/webcontroller/webcontroller.go +++ b/webcontroller/webcontroller.go @@ -36,28 +36,30 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon ) wc.templates.ParseTemplates(false) + var p = prefix + // Serve static files - r.ServeFiles(prefix+"/res/*filepath", http.Dir(wc.staticResourceDir+"/res")) + r.ServeFiles(p+"/res/*filepath", http.Dir(wc.staticResourceDir+"/res")) // General navigation - r.GET(prefix+"/" /* */, wc.serveTemplate("home", false)) - r.GET(prefix+"/favicon.ico" /* */, wc.serveFile("/favicon.ico")) - r.GET(prefix+"/global.css" /* */, wc.globalCSSHandler) - r.GET(prefix+"/api" /* */, wc.serveTemplate("apidoc", false)) - r.GET(prefix+"/history" /* */, wc.serveTemplate("history_cookies", false)) - r.GET(prefix+"/u/:id" /* */, wc.serveFileViewer) - r.GET(prefix+"/u/:id/preview" /* */, wc.serveFilePreview) - r.GET(prefix+"/l/:id" /* */, wc.serveListViewer) - r.GET(prefix+"/t" /* */, wc.serveTemplate("paste", false)) + r.GET(p+"/" /* */, wc.serveTemplate("home", false)) + r.GET(p+"/favicon.ico" /* */, wc.serveFile("/favicon.ico")) + r.GET(p+"/global.css" /* */, wc.globalCSSHandler) + r.GET(p+"/api" /* */, wc.serveTemplate("apidoc", false)) + r.GET(p+"/history" /* */, wc.serveTemplate("history_cookies", false)) + r.GET(p+"/u/:id" /* */, wc.serveFileViewer) + r.GET(p+"/u/:id/preview" /* */, wc.serveFilePreview) + r.GET(p+"/l/:id" /* */, wc.serveListViewer) + r.GET(p+"/t" /* */, wc.serveTemplate("paste", false)) // User account pages - r.GET(prefix+"/register" /* */, wc.serveRegister) - r.GET(prefix+"/login" /* */, wc.serveTemplate("login", false)) - r.GET(prefix+"/logout" /* */, wc.serveTemplate("logout", true)) - r.POST(prefix+"/logout" /* */, wc.serveLogout) - r.GET(prefix+"/user" /* */, wc.serveTemplate("user_home", true)) - r.GET(prefix+"/user/files" /* */, wc.serveTemplate("user_files", true)) - r.GET(prefix+"/user/filemanager" /**/, wc.serveTemplate("file_manager", true)) + r.GET(p+"/register" /* */, wc.serveRegister) + r.GET(p+"/login" /* */, wc.serveTemplate("login", false)) + r.GET(p+"/logout" /* */, wc.serveTemplate("logout", true)) + r.POST(p+"/logout" /* */, wc.serveLogout) + r.GET(p+"/user" /* */, wc.serveTemplate("user_home", true)) + r.GET(p+"/user/files" /* */, wc.serveTemplate("user_files", true)) + r.GET(p+"/user/filemanager" /**/, wc.serveTemplate("file_manager", true)) r.NotFound = http.HandlerFunc(wc.serveNotFound) @@ -97,6 +99,7 @@ func (wc *WebController) serveFile(path string) httprouter.Handle { func (wc *WebController) serveNotFound(w http.ResponseWriter, r *http.Request) { log.Debug("Not Found: %s", r.URL) + w.WriteHeader(http.StatusNotFound) wc.templates.Get().ExecuteTemplate(w, "404", wc.newTemplateData(w, r)) }