2017-10-06 07:02:57 +00:00
|
|
|
package contented
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var SERVER_HEADER string = `contented/0.1`
|
|
|
|
|
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 {
|
|
|
|
DataDirectory string
|
|
|
|
DBPath string
|
|
|
|
ServerPublicProperties
|
|
|
|
}
|
|
|
|
|
2017-10-06 07:02:57 +00:00
|
|
|
type Server struct {
|
|
|
|
opts ServerOptions
|
|
|
|
db *bolt.DB
|
|
|
|
metadataBucket []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(opts *ServerOptions) (*Server, error) {
|
|
|
|
s := &Server{
|
|
|
|
opts: *opts,
|
|
|
|
metadataBucket: []byte(`METADATA`),
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Server) handleView(w http.ResponseWriter, r *http.Request, fileID string) error {
|
|
|
|
// Load file
|
|
|
|
f, err := os.Open(filepath.Join(this.opts.DataDirectory, fileID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Load metadata
|
|
|
|
m, err := this.Metadata(fileID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-06 07:19:02 +00:00
|
|
|
// ServeContent only uses the filename to get the mime type, which we can
|
|
|
|
// set accurately (including blacklist)
|
|
|
|
w.Header().Set(`Content-Type`, m.MimeType)
|
|
|
|
if m.MimeType == `application/octet-stream` {
|
|
|
|
w.Header().Set(`Content-Disposition`, `attachment; filename="`+m.Filename+`"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(w, r, "", m.UploadTime, f)
|
|
|
|
|
2017-10-06 07:02:57 +00:00
|
|
|
return 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
|
|
|
|
}
|
|
|
|
|
2017-10-07 05:05:58 +00:00
|
|
|
this.serveJsonObject(w, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-06 07:02:57 +00:00
|
|
|
if this.opts.MaxUploadBytes > 0 {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, this.opts.MaxUploadBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
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:])
|
|
|
|
|
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
|
|
|
|
|
|
|
} else {
|
2017-10-07 05:05:58 +00:00
|
|
|
// TODO embed into binary
|
|
|
|
http.FileServer(http.Dir(`static`)).ServeHTTP(w, r)
|
2017-10-06 07:02:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|