2013-12-02 09:37:36 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-01-08 08:19:32 +00:00
|
|
|
"errors"
|
2013-12-02 09:37:36 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-03-22 19:50:04 +00:00
|
|
|
"time"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2015-04-16 19:18:06 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/go/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/operation"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/security"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/stats"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/storage"
|
|
|
|
"github.com/chrislusf/seaweedfs/go/util"
|
2013-12-02 09:37:36 +00:00
|
|
|
)
|
|
|
|
|
2014-03-25 20:46:59 +00:00
|
|
|
var serverStats *stats.ServerStats
|
2015-03-22 19:50:04 +00:00
|
|
|
var startTime = time.Now()
|
2014-03-25 20:46:59 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
serverStats = stats.NewServerStats()
|
|
|
|
go serverStats.Start()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-08 08:19:32 +00:00
|
|
|
func writeJson(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) (err error) {
|
2013-12-02 09:37:36 +00:00
|
|
|
var bytes []byte
|
|
|
|
if r.FormValue("pretty") != "" {
|
|
|
|
bytes, err = json.MarshalIndent(obj, "", " ")
|
|
|
|
} else {
|
|
|
|
bytes, err = json.Marshal(obj)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
callback := r.FormValue("callback")
|
|
|
|
if callback == "" {
|
2014-06-14 01:16:03 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2015-01-08 08:19:32 +00:00
|
|
|
w.WriteHeader(httpStatus)
|
2013-12-02 09:37:36 +00:00
|
|
|
_, err = w.Write(bytes)
|
|
|
|
} else {
|
2014-06-14 01:16:03 +00:00
|
|
|
w.Header().Set("Content-Type", "application/javascript")
|
2015-01-08 08:19:32 +00:00
|
|
|
w.WriteHeader(httpStatus)
|
2013-12-02 09:37:36 +00:00
|
|
|
if _, err = w.Write([]uint8(callback)); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = w.Write([]uint8("(")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, string(bytes))
|
|
|
|
if _, err = w.Write([]uint8(")")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 08:19:32 +00:00
|
|
|
|
2013-12-02 09:37:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapper for writeJson - just logs errors
|
2015-01-08 08:19:32 +00:00
|
|
|
func writeJsonQuiet(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) {
|
|
|
|
if err := writeJson(w, r, httpStatus, obj); err != nil {
|
2015-01-14 01:04:41 +00:00
|
|
|
glog.V(0).Infof("error writing JSON %s: %v", obj, err)
|
2013-12-02 09:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-08 08:19:32 +00:00
|
|
|
func writeJsonError(w http.ResponseWriter, r *http.Request, httpStatus int, err error) {
|
2013-12-02 09:37:36 +00:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["error"] = err.Error()
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, httpStatus, m)
|
2013-12-02 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func debug(params ...interface{}) {
|
2014-03-30 18:28:04 +00:00
|
|
|
glog.V(4).Infoln(params)
|
2013-12-02 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
|
2015-02-07 23:35:28 +00:00
|
|
|
jwt := security.GetJwt(r)
|
2013-12-02 09:37:36 +00:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
if r.Method != "POST" {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusMethodNotAllowed, errors.New("Only submit via POST!"))
|
2013-12-02 09:37:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("parsing upload file...")
|
2014-09-20 19:38:59 +00:00
|
|
|
fname, data, mimeType, isGzipped, lastModified, _, pe := storage.ParseUpload(r)
|
2013-12-02 09:37:36 +00:00
|
|
|
if pe != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusBadRequest, pe)
|
2013-12-02 09:37:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("assigning file id for", fname)
|
2015-01-06 11:31:26 +00:00
|
|
|
r.ParseForm()
|
2014-09-20 19:38:59 +00:00
|
|
|
assignResult, ae := operation.Assign(masterUrl, 1, r.FormValue("replication"), r.FormValue("collection"), r.FormValue("ttl"))
|
2013-12-02 09:37:36 +00:00
|
|
|
if ae != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, ae)
|
2013-12-02 09:37:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-02 18:16:50 +00:00
|
|
|
url := "http://" + assignResult.Url + "/" + assignResult.Fid
|
2013-12-02 09:37:36 +00:00
|
|
|
if lastModified != 0 {
|
|
|
|
url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("upload file to store", url)
|
2015-02-07 23:35:28 +00:00
|
|
|
uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType, jwt)
|
2013-12-02 09:37:36 +00:00
|
|
|
if err != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2013-12-02 09:37:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m["fileName"] = fname
|
|
|
|
m["fid"] = assignResult.Fid
|
|
|
|
m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
|
|
|
|
m["size"] = uploadResult.Size
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusCreated, m)
|
2013-12-02 09:37:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-14 06:41:34 +00:00
|
|
|
func deleteForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
|
|
|
|
r.ParseForm()
|
|
|
|
fids := r.Form["fid"]
|
2014-04-15 16:09:40 +00:00
|
|
|
ret, err := operation.DeleteFiles(masterUrl, fids)
|
|
|
|
if err != nil {
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
2014-04-15 16:09:40 +00:00
|
|
|
return
|
|
|
|
}
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusAccepted, ret)
|
2014-04-14 06:41:34 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 09:37:36 +00:00
|
|
|
func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
|
|
|
|
switch strings.Count(path, "/") {
|
|
|
|
case 3:
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid, filename = parts[1], parts[2], parts[3]
|
|
|
|
ext = filepath.Ext(filename)
|
|
|
|
case 2:
|
|
|
|
parts := strings.Split(path, "/")
|
|
|
|
vid, fid = parts[1], parts[2]
|
|
|
|
dotIndex := strings.LastIndex(fid, ".")
|
|
|
|
if dotIndex > 0 {
|
|
|
|
ext = fid[dotIndex:]
|
|
|
|
fid = fid[0:dotIndex]
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
sepIndex := strings.LastIndex(path, "/")
|
|
|
|
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
|
|
|
if commaIndex <= 0 {
|
|
|
|
vid, isVolumeIdOnly = path[sepIndex+1:], true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
|
|
|
vid = path[sepIndex+1 : commaIndex]
|
|
|
|
fid = path[commaIndex+1:]
|
|
|
|
ext = ""
|
|
|
|
if dotIndex > 0 {
|
|
|
|
fid = path[commaIndex+1 : dotIndex]
|
|
|
|
ext = path[dotIndex:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-04-14 06:41:34 +00:00
|
|
|
|
2014-03-25 20:46:59 +00:00
|
|
|
func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = util.VERSION
|
2014-03-26 20:22:27 +00:00
|
|
|
m["Counters"] = serverStats
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-03-25 20:46:59 +00:00
|
|
|
}
|
2014-03-26 20:22:27 +00:00
|
|
|
|
2014-03-25 20:46:59 +00:00
|
|
|
func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
m["Version"] = util.VERSION
|
2014-03-26 20:22:27 +00:00
|
|
|
m["Memory"] = stats.MemStat()
|
2015-01-08 08:19:32 +00:00
|
|
|
writeJsonQuiet(w, r, http.StatusOK, m)
|
2014-03-25 20:46:59 +00:00
|
|
|
}
|