Finish list viewer

This commit is contained in:
2017-12-12 23:33:41 +01:00
parent 98546918e9
commit 49c28476a9
8 changed files with 91 additions and 9 deletions

View File

@@ -28,6 +28,7 @@ func Init(r *httprouter.Router, prefix string) {
r.GET(prefix+"/history", webcontroller.ServeHistory)
r.GET(prefix+"/u/:id", webcontroller.ServeFileViewer)
r.GET(prefix+"/u/:id/preview", webcontroller.ServeFilePreview)
r.GET(prefix+"/l/:id", webcontroller.ServeListViewer)
r.GET(prefix+"/t", webcontroller.ServePaste)
r.NotFound = http.HandlerFunc(webcontroller.ServeNotFound)

44
pixelapi/list.go Normal file
View File

@@ -0,0 +1,44 @@
package pixelapi
import (
"encoding/json"
"fornaxian.com/pixeldrain-web/conf"
"fornaxian.com/pixeldrain-web/log"
)
// List information object from the pixeldrain API
type List struct {
ID string `json:"id"`
Title string `json:"title"`
DateCreated int64 `json:"date_created"`
Files []ListFile
}
// 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 int64 `json:"date_created"`
DateLastView int64 `json:"date_last_view"`
ListDescription string `json:"list_description"`
}
// GetList get a List from the pixeldrain API
func GetList(id string) *List {
body, err := get(conf.ApiURL() + "/list/" + id)
if err != nil {
log.Error("req failed: %v", err)
return nil
}
var list List
err = json.Unmarshal([]byte(body), &list)
if err != nil {
log.Error("unmarshal failed: %v. json: %s", err, body)
return nil
}
return &list
}

View File

@@ -124,7 +124,7 @@ var ListNavigator = {
var navigatorItems = $("#listNavigatorItems").children().toArray();
for (i = startPos; i <= endPos; i++){
var thumb = this.data[i].thumbnail;
var thumb = "/api/file/" + this.data[i].id + "/thumbnail";
var name = this.data[i].file_name;
var itemHtml = escapeHTML(name) + "<br>"

View File

@@ -51,27 +51,26 @@ function createList(){
}
function listCreated(response){
if(response.status === "success"){
resultString = "<div class=\"uploadHistory\">List creation finished!<br/>"
if(response.success){
resultString = "<div class=\"uploadItem\">List creation finished!<br/>"
+ "Your List URL: <br/>"
+ "<a href=\"/l/" + response.id + "\" target=\"_blank\" style=\"font-weight: bold;\">/l/" + response.id + "</a>"
+ "<a href=\"/l/" + response.id + "\" target=\"_blank\" style=\"font-weight: bold;\">"+window.location.hostname+"/l/" + response.id + "</a>"
+ "</div>";
$('#uploads-container').prepend(
$('#uploads-completed').prepend(
$(resultString).hide().fadeIn('slow')
);
}else{
resultString = "<div class=\"uploadHistory\">List creation failed<br/>"
resultString = "<div class=\"uploadItem\">List creation failed<br/>"
+ "The server responded with this: <br/>"
+ response.type + ": " + response.value
+ "</div>";
$('#uploads-container').prepend(
$('#uploads-completed').prepend(
$(resultString).hide().fadeIn('slow')
);
}
}
//$("#btnAddToList").click(function (evt) {
// var fileId = $("#txtListId").val();
// var fileDesc = $("#txtListDesc").val();

View File

@@ -2,7 +2,7 @@
<!DOCTYPE html>
<html>
<head>
<title>{{.FileName}} ~ PixelDrain File</title>
<title>{{.Title}}</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="/res/style/viewer.css"/>

View File

@@ -1,6 +1,7 @@
package webcontroller
import (
"fmt"
"net/http"
"strings"
@@ -50,11 +51,13 @@ func ServeFileViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params
"views": 0,
}
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
"Title": fmt.Sprintf("%d files in Pixeldrain", len(finfo)),
"APIResponse": listdata,
"Type": "list",
})
} else {
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
"Title": fmt.Sprintf("%s ~ Pixeldrain file", finfo[0].FileName),
"APIResponse": finfo[0],
"Type": "file",
})

View File

@@ -0,0 +1,35 @@
package webcontroller
import (
"fmt"
"net/http"
"fornaxian.com/pixeldrain-web/log"
"fornaxian.com/pixeldrain-web/pixelapi"
"fornaxian.com/pixeldrain-web/webcontroller/templates"
"github.com/julienschmidt/httprouter"
)
// ServeListViewer controller for GET /l/:id
func ServeListViewer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var list = pixelapi.GetList(p.ByName("id"))
if list == nil {
ServeNotFound(w, r)
return
}
var err error
listdata := map[string]interface{}{
"data": list.Files,
"date_created": list.DateCreated,
"title": list.Title,
"views": 0,
}
err = templates.Get().ExecuteTemplate(w, "file_viewer", map[string]interface{}{
"Title": fmt.Sprintf("%s ~ Pixeldrain list", list.Title),
"APIResponse": listdata,
"Type": "list",
})
if err != nil {
log.Error("Error executing template file_viewer: %s", err)
}
}