rename /view/ to /get/, reorganise some server code

This commit is contained in:
mappu 2017-10-08 16:01:41 +13:00
parent 9e6ffc5d82
commit 6c21e2a7fa
4 changed files with 44 additions and 31 deletions

View File

@ -3,6 +3,8 @@ package contented
import (
"encoding/json"
"errors"
"log"
"net/http"
"os"
"time"
@ -73,3 +75,19 @@ func (this *Server) AddMetadata(m Metadata) (string, error) {
return shortRef, nil
}
func (this *Server) handleInformation(w http.ResponseWriter, fileID string) {
m, err := this.Metadata(fileID)
if err != nil {
if os.IsNotExist(err) {
http.Error(w, "Not found", 404)
return
}
log.Println(err.Error())
http.Error(w, "Internal error", 500)
return
}
this.serveJsonObject(w, m)
}

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"log"
"net/http"
"os"
"strings"
"github.com/boltdb/bolt"
@ -55,22 +54,6 @@ func NewServer(opts *ServerOptions) (*Server, error) {
return s, nil
}
func (this *Server) handleInformation(w http.ResponseWriter, fileID string) {
m, err := this.Metadata(fileID)
if err != nil {
if os.IsNotExist(err) {
http.Error(w, "Not found", 404)
return
}
log.Println(err.Error())
http.Error(w, "Internal error", 500)
return
}
this.serveJsonObject(w, m)
}
func (this *Server) serveJsonObject(w http.ResponseWriter, o interface{}) {
jb, err := json.Marshal(o)
if err != nil {
@ -92,6 +75,11 @@ func remoteIP(r *http.Request) string {
return strings.TrimRight(strings.TrimRight(r.RemoteAddr, "0123456789"), ":")
}
const (
downloadUrlPrefix = `/get/`
metadataUrlPrefix = `/info/`
)
func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(`Server`, SERVER_HEADER)
if this.opts.MaxUploadBytes > 0 {
@ -101,19 +89,13 @@ func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Body = flowrate.NewReader(r.Body, this.opts.BandwidthLimit)
}
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, `/view/`) {
err := this.handleView(w, r, r.URL.Path[6:])
if err != nil {
log.Printf("%s View failed: %s\n", r.RemoteAddr, err.Error())
if os.IsNotExist(err) {
http.Error(w, "File not found", 404)
} else {
http.Error(w, "Couldn't provide content", 500)
}
}
//
} else if r.Method == "GET" && strings.HasPrefix(r.URL.Path, `/info/`) {
this.handleInformation(w, r.URL.Path[6:])
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, downloadUrlPrefix) {
this.handleView(w, r, r.URL.Path[len(downloadUrlPrefix):])
} else if r.Method == "GET" && strings.HasPrefix(r.URL.Path, metadataUrlPrefix) {
this.handleInformation(w, r.URL.Path[len(metadataUrlPrefix):])
} else if r.Method == "GET" && r.URL.Path == `/about` {
this.handleAbout(w)

View File

@ -1,12 +1,25 @@
package contented
import (
"log"
"net/http"
"os"
"path/filepath"
)
func (this *Server) handleView(w http.ResponseWriter, r *http.Request, fileID string) error {
func (this *Server) handleView(w http.ResponseWriter, r *http.Request, fileID string) {
err := this.handleViewInternal(w, r, r.URL.Path[len(downloadUrlPrefix):])
if err != nil {
log.Printf("%s View failed: %s\n", r.RemoteAddr, err.Error())
if os.IsNotExist(err) {
http.Error(w, "File not found", 404)
} else {
http.Error(w, "Couldn't provide content", 500)
}
}
}
func (this *Server) handleViewInternal(w http.ResponseWriter, r *http.Request, fileID string) error {
// Load metadata
m, err := this.Metadata(fileID)

View File

@ -38,7 +38,7 @@ contented.init("#surrogate-area", function(items) {
for (var i = 0; i < items.length; ++i) {
$table.append($("<tr>").append([
$("<td>").text(items[i]),
$("<td>").html("<a target='_blank' href='/view/" + items[i] + "'>view</a>"),
$("<td>").html("<a target='_blank' href='/get/" + items[i] + "'>get</a>"),
$("<td>").html("<a target='_blank' href='/info/" + items[i] + "'>info</a>")
]))
}