From 7fffb0b422f8306f709f38b029cc0a03e583184c Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Sat, 29 Apr 2017 11:21:14 -0700 Subject: adding view and links to admin/manager --- system/admin/admin.go | 1 + system/admin/handlers.go | 260 +++++++++++++++++++++++++++++++++++++++++++++++ system/admin/server.go | 2 + 3 files changed, 263 insertions(+) (limited to 'system') diff --git a/system/admin/admin.go b/system/admin/admin.go index 09750f0..9e57678 100644 --- a/system/admin/admin.go +++ b/system/admin/admin.go @@ -66,6 +66,7 @@ var mainAdminHTML = `
  • settingsConfiguration
  • supervisor_accountAdmin Users
  • +
  • present_to_allAddons
  • settings_input_svideoAddons
  • diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 4734ba0..0433b76 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -827,6 +827,266 @@ func recoveryKeyHandler(res http.ResponseWriter, req *http.Request) { } } +func uploadContentsHandler(res http.ResponseWriter, req *http.Request) { + q := req.URL.Query() + + order := strings.ToLower(q.Get("order")) + if order != "asc" { + order = "desc" + } + + pt := interface{}(&item.FileUpload{}) + + p, ok := pt.(editor.Editable) + if !ok { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + + count, err := strconv.Atoi(q.Get("count")) // int: determines number of posts to return (10 default, -1 is all) + if err != nil { + if q.Get("count") == "" { + count = 10 + } else { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + } + + offset, err := strconv.Atoi(q.Get("offset")) // int: multiplier of count for pagination (0 default) + if err != nil { + if q.Get("offset") == "" { + offset = 0 + } else { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + } + + opts := db.QueryOptions{ + Count: count, + Offset: offset, + Order: order, + } + + b := &bytes.Buffer{} + var total int + var posts [][]byte + + html := `
    +
    +
    +
    +
    +
    Uploaded Items
    +
    + + +
    + +
    +
    +
    +
    + + search + + +
    +
    +
    ` + + t := "__uploads" + status := "" + total, posts = db.Query(t, opts) + + for i := range posts { + err := json.Unmarshal(posts[i], &p) + if err != nil { + log.Println("Error unmarshal json into", t, err, string(posts[i])) + + post := `
  • Error decoding data. Possible file corruption.
  • ` + _, err := b.Write([]byte(post)) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + continue + } + + post := adminPostListItem(p, t, status) + _, err = b.Write(post) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + } + + html += `
      ` + + _, err = b.Write([]byte(`
    `)) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + + statusDisabled := "disabled" + prevStatus := "" + nextStatus := "" + // total may be less than 10 (default count), so reset count to match total + if total < count { + count = total + } + // nothing previous to current list + if offset == 0 { + prevStatus = statusDisabled + } + // nothing after current list + if (offset+1)*count >= total { + nextStatus = statusDisabled + } + + // set up pagination values + urlFmt := req.URL.Path + "?count=%d&offset=%d&&order=%s" + prevURL := fmt.Sprintf(urlFmt, count, offset-1, order) + nextURL := fmt.Sprintf(urlFmt, count, offset+1, order) + start := 1 + count*offset + end := start + count - 1 + + if total < end { + end = total + } + + pagination := fmt.Sprintf(` + + `, prevStatus, prevURL, start, end, total, nextStatus, nextURL) + + // show indicator that a collection of items will be listed implicitly, but + // that none are created yet + if total < 1 { + pagination = ` + + ` + } + + _, err = b.Write([]byte(pagination + `
    `)) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + + script := ` + + ` + + btn := `
    New Upload
    ` + html = html + b.String() + script + btn + + adminView, err := Admin([]byte(html)) + if err != nil { + log.Println(err) + res.WriteHeader(http.StatusInternalServerError) + return + } + + res.Header().Set("Content-Type", "text/html") + res.Write(adminView) +} + func contentsHandler(res http.ResponseWriter, req *http.Request) { q := req.URL.Query() t := q.Get("type") diff --git a/system/admin/server.go b/system/admin/server.go index df00c21..94aca78 100644 --- a/system/admin/server.go +++ b/system/admin/server.go @@ -32,6 +32,8 @@ func Run() { http.HandleFunc("/admin/configure/users/edit", user.Auth(configUsersEditHandler)) http.HandleFunc("/admin/configure/users/delete", user.Auth(configUsersDeleteHandler)) + http.HandleFunc("/admin/uploads", user.Auth(uploadContentsHandler)) + http.HandleFunc("/admin/contents", user.Auth(contentsHandler)) http.HandleFunc("/admin/contents/search", user.Auth(searchHandler)) -- cgit v1.2.3