Commit 414b47d6 authored by Abdulelah Alfuntukh's avatar Abdulelah Alfuntukh

adds json option for the browse middleware

parent b62d087b
...@@ -4,12 +4,14 @@ package browse ...@@ -4,12 +4,14 @@ package browse
import ( import (
"bytes" "bytes"
"encoding/json"
"errors" "errors"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path" "path"
"sort" "sort"
"strconv"
"strings" "strings"
"text/template" "text/template"
"time" "time"
...@@ -185,7 +187,6 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s ...@@ -185,7 +187,6 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
// ServeHTTP implements the middleware.Handler interface. // ServeHTTP implements the middleware.Handler interface.
func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
filename := b.Root + r.URL.Path filename := b.Root + r.URL.Path
info, err := os.Stat(filename) info, err := os.Stat(filename)
if err != nil { if err != nil {
return b.Next.ServeHTTP(w, r) return b.Next.ServeHTTP(w, r)
...@@ -264,12 +265,47 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -264,12 +265,47 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
listing.applySort() listing.applySort()
var buf bytes.Buffer var buf bytes.Buffer
err = bc.Template.Execute(&buf, listing) // check if we should provide json
if err != nil { acceptHeader := strings.Join(r.Header["Accept"], ",")
return http.StatusInternalServerError, err if strings.Contains(strings.ToLower(acceptHeader), "application/json") {
var marsh []byte
// check if we are limited
if limitQuery := r.URL.Query().Get("limit"); limitQuery != "" {
limit, err := strconv.Atoi(limitQuery)
if err != nil { // if the 'limit' query can't be interpreted as a number, return err
return http.StatusBadRequest, err
}
// if `limit` is equal or less than len(listing.Items) and bigger than 0, list them
if limit <= len(listing.Items) && limit > 0 {
marsh, err = json.Marshal(listing.Items[:limit])
} else { // if the 'limit' query is empty, or has the wrong value, list everything
marsh, err = json.Marshal(listing.Items)
}
if err != nil {
return http.StatusInternalServerError, err
}
} else { // there's no 'limit' query, list them all
marsh, err = json.Marshal(listing.Items)
if err != nil {
return http.StatusInternalServerError, err
}
}
// write the marshaled json to buf
if _, err = buf.Write(marsh); err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
} else { // there's no json query, browse normally
err = bc.Template.Execute(&buf, listing)
if err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf.WriteTo(w) buf.WriteTo(w)
return http.StatusOK, nil return http.StatusOK, nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment