2017-10-06 07:02:57 +00:00
|
|
|
package contented
|
|
|
|
|
|
|
|
import (
|
2017-10-08 03:34:11 +00:00
|
|
|
"bytes"
|
2017-10-06 07:02:57 +00:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2017-11-18 00:11:39 +00:00
|
|
|
"regexp"
|
2017-10-06 07:02:57 +00:00
|
|
|
"strings"
|
2017-10-08 03:34:11 +00:00
|
|
|
"time"
|
2017-10-06 07:02:57 +00:00
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2017-10-08 02:06:46 +00:00
|
|
|
"github.com/mxk/go-flowrate/flowrate"
|
2017-10-06 07:02:57 +00:00
|
|
|
)
|
|
|
|
|
2017-10-08 03:30:52 +00:00
|
|
|
var SERVER_HEADER string = `contented/0.0.0-dev`
|
2017-10-06 07:02:57 +00:00
|
|
|
|
2017-10-07 05:05:58 +00:00
|
|
|
type ServerPublicProperties struct {
|
2017-10-06 07:02:57 +00:00
|
|
|
AppTitle string
|
|
|
|
MaxUploadBytes int64
|
|
|
|
}
|
|
|
|
|
2017-10-07 05:05:58 +00:00
|
|
|
type ServerOptions struct {
|
2017-10-15 06:09:14 +00:00
|
|
|
DataDirectory string
|
|
|
|
DBPath string
|
|
|
|
BandwidthLimit int64
|
|
|
|
TrustXForwardedFor bool
|
2017-10-15 06:16:22 +00:00
|
|
|
EnableHomepage bool
|
2017-10-07 05:05:58 +00:00
|
|
|
ServerPublicProperties
|
|
|
|
}
|
|
|
|
|
2017-10-06 07:02:57 +00:00
|
|
|
type Server struct {
|
|
|
|
opts ServerOptions
|
|
|
|
db *bolt.DB
|
2017-10-08 03:34:11 +00:00
|
|
|
startTime time.Time
|
2017-10-06 07:02:57 +00:00
|
|
|
metadataBucket []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(opts *ServerOptions) (*Server, error) {
|
|
|
|
s := &Server{
|
|
|
|
opts: *opts,
|
|
|
|
metadataBucket: []byte(`METADATA`),
|
2017-10-08 03:34:11 +00:00
|
|
|
startTime: time.Now(),
|
2017-10-06 07:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := bolt.Open(opts.DBPath, 0644, bolt.DefaultOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(s.metadataBucket)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.db = b
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2017-10-07 05:05:58 +00:00
|
|
|
func (this *Server) serveJsonObject(w http.ResponseWriter, o interface{}) {
|
|
|
|
jb, err := json.Marshal(o)
|
2017-10-06 07:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
http.Error(w, "Internal error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set(`Content-Type`, `application/json`)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write(jb)
|
|
|
|
}
|
|
|
|
|
2017-10-07 05:05:58 +00:00
|
|
|
func (this *Server) handleAbout(w http.ResponseWriter) {
|
|
|
|
this.serveJsonObject(w, this.opts.ServerPublicProperties)
|
|
|
|
}
|
|
|
|
|
2017-10-15 06:09:14 +00:00
|
|
|
func (this *Server) remoteIP(r *http.Request) string {
|
|
|
|
if this.opts.TrustXForwardedFor {
|
|
|
|
if xff := r.Header.Get("X-Forwarded-For"); len(xff) > 0 {
|
|
|
|
return xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 02:25:01 +00:00
|
|
|
return strings.TrimRight(strings.TrimRight(r.RemoteAddr, "0123456789"), ":")
|
|
|
|
}
|
|
|
|
|
2017-10-08 03:01:41 +00:00
|
|
|
const (
|
|
|
|
downloadUrlPrefix = `/get/`
|
|
|
|
metadataUrlPrefix = `/info/`
|
2017-11-18 00:48:34 +00:00
|
|
|
previewUrlPrefix = `/p/`
|
2017-10-08 03:01:41 +00:00
|
|
|
)
|
|
|
|
|
2017-11-18 00:11:39 +00:00
|
|
|
var rxThumbUrl = regexp.MustCompile(`^/thumb/(.)/(.*)$`)
|
|
|
|
|
2017-10-06 07:02:57 +00:00
|
|
|
func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-10-07 05:05:58 +00:00
|
|
|
w.Header().Set(`Server`, SERVER_HEADER)
|
2017-10-08 04:06:24 +00:00
|
|
|
w.Header().Set(`Access-Control-Allow-Origin`, `*`) // Blanket allow CORS
|
2017-10-06 07:02:57 +00:00
|
|
|
if this.opts.MaxUploadBytes > 0 {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, this.opts.MaxUploadBytes)
|
|
|
|
}
|
2017-10-08 02:06:46 +00:00
|
|
|
if this.opts.BandwidthLimit > 0 {
|
|
|
|
r.Body = flowrate.NewReader(r.Body, this.opts.BandwidthLimit)
|
|
|
|
}
|
2017-10-06 07:02:57 +00:00
|
|
|
|
2017-10-08 03:01:41 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
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):])
|
2017-10-06 07:02:57 +00:00
|
|
|
|
2017-11-18 00:48:34 +00:00
|
|
|
} else if r.Method == "GET" && strings.HasPrefix(r.URL.Path, previewUrlPrefix) {
|
|
|
|
this.handlePreview(w, r.URL.Path[len(previewUrlPrefix):])
|
|
|
|
|
2017-11-18 00:11:39 +00:00
|
|
|
} else if r.Method == "GET" && rxThumbUrl.MatchString(r.URL.Path) {
|
|
|
|
parts := rxThumbUrl.FindStringSubmatch(r.URL.Path)
|
|
|
|
this.handleThumb(w, r, parts[1][0], parts[2])
|
|
|
|
|
2017-10-07 05:05:58 +00:00
|
|
|
} else if r.Method == "GET" && r.URL.Path == `/about` {
|
|
|
|
this.handleAbout(w)
|
|
|
|
|
2017-10-06 07:02:57 +00:00
|
|
|
} else if r.Method == "POST" && r.URL.Path == `/upload` {
|
2017-10-08 01:10:15 +00:00
|
|
|
this.handleUpload(w, r)
|
2017-10-06 07:02:57 +00:00
|
|
|
|
2017-10-08 03:42:06 +00:00
|
|
|
} else if r.Method == "OPTIONS" {
|
2017-10-08 04:06:24 +00:00
|
|
|
// Blanket allow (headers already set)
|
2017-10-08 03:42:06 +00:00
|
|
|
w.WriteHeader(200)
|
|
|
|
|
2017-10-15 06:16:22 +00:00
|
|
|
} else if r.Method == "GET" && r.URL.Path == `/` && this.opts.EnableHomepage {
|
2017-10-08 03:54:26 +00:00
|
|
|
http.Redirect(w, r, `/index.html`, http.StatusFound)
|
|
|
|
|
2017-10-15 06:16:22 +00:00
|
|
|
} else if static, err := Asset(r.URL.Path[1:]); err == nil && r.Method == "GET" && (this.opts.EnableHomepage || r.URL.Path != `/index.html`) {
|
2017-10-08 03:34:11 +00:00
|
|
|
http.ServeContent(w, r, r.URL.Path[1:], this.startTime, bytes.NewReader(static))
|
2017-10-06 07:02:57 +00:00
|
|
|
|
2017-10-08 03:34:11 +00:00
|
|
|
} else {
|
|
|
|
http.Error(w, "Not found", 404)
|
2017-10-06 07:02:57 +00:00
|
|
|
}
|
|
|
|
}
|