rename /view/ to /get/, reorganise some server code
This commit is contained in:
parent
9e6ffc5d82
commit
6c21e2a7fa
18
Metadata.go
18
Metadata.go
@ -3,6 +3,8 @@ package contented
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -73,3 +75,19 @@ func (this *Server) AddMetadata(m Metadata) (string, error) {
|
|||||||
|
|
||||||
return shortRef, nil
|
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)
|
||||||
|
}
|
||||||
|
40
Server.go
40
Server.go
@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
@ -55,22 +54,6 @@ func NewServer(opts *ServerOptions) (*Server, error) {
|
|||||||
return s, nil
|
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{}) {
|
func (this *Server) serveJsonObject(w http.ResponseWriter, o interface{}) {
|
||||||
jb, err := json.Marshal(o)
|
jb, err := json.Marshal(o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -92,6 +75,11 @@ func remoteIP(r *http.Request) string {
|
|||||||
return strings.TrimRight(strings.TrimRight(r.RemoteAddr, "0123456789"), ":")
|
return strings.TrimRight(strings.TrimRight(r.RemoteAddr, "0123456789"), ":")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
downloadUrlPrefix = `/get/`
|
||||||
|
metadataUrlPrefix = `/info/`
|
||||||
|
)
|
||||||
|
|
||||||
func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set(`Server`, SERVER_HEADER)
|
w.Header().Set(`Server`, SERVER_HEADER)
|
||||||
if this.opts.MaxUploadBytes > 0 {
|
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)
|
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/`) {
|
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, downloadUrlPrefix) {
|
||||||
this.handleInformation(w, r.URL.Path[6:])
|
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` {
|
} else if r.Method == "GET" && r.URL.Path == `/about` {
|
||||||
this.handleAbout(w)
|
this.handleAbout(w)
|
||||||
|
15
download.go
15
download.go
@ -1,12 +1,25 @@
|
|||||||
package contented
|
package contented
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"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
|
// Load metadata
|
||||||
m, err := this.Metadata(fileID)
|
m, err := this.Metadata(fileID)
|
||||||
|
@ -38,7 +38,7 @@ contented.init("#surrogate-area", function(items) {
|
|||||||
for (var i = 0; i < items.length; ++i) {
|
for (var i = 0; i < items.length; ++i) {
|
||||||
$table.append($("<tr>").append([
|
$table.append($("<tr>").append([
|
||||||
$("<td>").text(items[i]),
|
$("<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>")
|
$("<td>").html("<a target='_blank' href='/info/" + items[i] + "'>info</a>")
|
||||||
]))
|
]))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user