diff --git a/Metadata.go b/Metadata.go index b635e28..0efda4a 100644 --- a/Metadata.go +++ b/Metadata.go @@ -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) +} diff --git a/Server.go b/Server.go index 8f65c68..000415c 100644 --- a/Server.go +++ b/Server.go @@ -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) diff --git a/download.go b/download.go index fc09b43..ee62e54 100644 --- a/download.go +++ b/download.go @@ -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) diff --git a/static/index.html b/static/index.html index 5fba33a..36982ec 100644 --- a/static/index.html +++ b/static/index.html @@ -38,7 +38,7 @@ contented.init("#surrogate-area", function(items) { for (var i = 0; i < items.length; ++i) { $table.append($("