You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.5 KiB

  1. package weed_server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "github.com/chrislusf/weed-fs/go/glog"
  12. "github.com/chrislusf/weed-fs/go/operation"
  13. "github.com/chrislusf/weed-fs/go/stats"
  14. "github.com/chrislusf/weed-fs/go/storage"
  15. "github.com/chrislusf/weed-fs/go/util"
  16. )
  17. var serverStats *stats.ServerStats
  18. func init() {
  19. serverStats = stats.NewServerStats()
  20. go serverStats.Start()
  21. }
  22. func writeJson(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) (err error) {
  23. var bytes []byte
  24. if r.FormValue("pretty") != "" {
  25. bytes, err = json.MarshalIndent(obj, "", " ")
  26. } else {
  27. bytes, err = json.Marshal(obj)
  28. }
  29. if err != nil {
  30. return
  31. }
  32. callback := r.FormValue("callback")
  33. if callback == "" {
  34. w.Header().Set("Content-Type", "application/json")
  35. w.WriteHeader(httpStatus)
  36. _, err = w.Write(bytes)
  37. } else {
  38. w.Header().Set("Content-Type", "application/javascript")
  39. w.WriteHeader(httpStatus)
  40. if _, err = w.Write([]uint8(callback)); err != nil {
  41. return
  42. }
  43. if _, err = w.Write([]uint8("(")); err != nil {
  44. return
  45. }
  46. fmt.Fprint(w, string(bytes))
  47. if _, err = w.Write([]uint8(")")); err != nil {
  48. return
  49. }
  50. }
  51. return
  52. }
  53. // wrapper for writeJson - just logs errors
  54. func writeJsonQuiet(w http.ResponseWriter, r *http.Request, httpStatus int, obj interface{}) {
  55. if err := writeJson(w, r, httpStatus, obj); err != nil {
  56. glog.V(0).Infof("error writing JSON %s: %v", obj, err)
  57. }
  58. }
  59. func writeJsonError(w http.ResponseWriter, r *http.Request, httpStatus int, err error) {
  60. m := make(map[string]interface{})
  61. m["error"] = err.Error()
  62. writeJsonQuiet(w, r, httpStatus, m)
  63. }
  64. func debug(params ...interface{}) {
  65. glog.V(4).Infoln(params)
  66. }
  67. func submitForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
  68. m := make(map[string]interface{})
  69. if r.Method != "POST" {
  70. writeJsonError(w, r, http.StatusMethodNotAllowed, errors.New("Only submit via POST!"))
  71. return
  72. }
  73. debug("parsing upload file...")
  74. fname, data, mimeType, isGzipped, lastModified, _, pe := storage.ParseUpload(r)
  75. if pe != nil {
  76. writeJsonError(w, r, http.StatusBadRequest, pe)
  77. return
  78. }
  79. debug("assigning file id for", fname)
  80. r.ParseForm()
  81. assignResult, ae := operation.Assign(masterUrl, 1, r.FormValue("replication"), r.FormValue("collection"), r.FormValue("ttl"))
  82. if ae != nil {
  83. writeJsonError(w, r, http.StatusInternalServerError, ae)
  84. return
  85. }
  86. url := "http://" + assignResult.PublicUrl + "/" + assignResult.Fid
  87. if lastModified != 0 {
  88. url = url + "?ts=" + strconv.FormatUint(lastModified, 10)
  89. }
  90. debug("upload file to store", url)
  91. uploadResult, err := operation.Upload(url, fname, bytes.NewReader(data), isGzipped, mimeType)
  92. if err != nil {
  93. writeJsonError(w, r, http.StatusInternalServerError, err)
  94. return
  95. }
  96. m["fileName"] = fname
  97. m["fid"] = assignResult.Fid
  98. m["fileUrl"] = assignResult.PublicUrl + "/" + assignResult.Fid
  99. m["size"] = uploadResult.Size
  100. writeJsonQuiet(w, r, http.StatusCreated, m)
  101. return
  102. }
  103. func deleteForClientHandler(w http.ResponseWriter, r *http.Request, masterUrl string) {
  104. r.ParseForm()
  105. fids := r.Form["fid"]
  106. ret, err := operation.DeleteFiles(masterUrl, fids)
  107. if err != nil {
  108. writeJsonError(w, r, http.StatusInternalServerError, err)
  109. return
  110. }
  111. writeJsonQuiet(w, r, http.StatusAccepted, ret)
  112. }
  113. func parseURLPath(path string) (vid, fid, filename, ext string, isVolumeIdOnly bool) {
  114. switch strings.Count(path, "/") {
  115. case 3:
  116. parts := strings.Split(path, "/")
  117. vid, fid, filename = parts[1], parts[2], parts[3]
  118. ext = filepath.Ext(filename)
  119. case 2:
  120. parts := strings.Split(path, "/")
  121. vid, fid = parts[1], parts[2]
  122. dotIndex := strings.LastIndex(fid, ".")
  123. if dotIndex > 0 {
  124. ext = fid[dotIndex:]
  125. fid = fid[0:dotIndex]
  126. }
  127. default:
  128. sepIndex := strings.LastIndex(path, "/")
  129. commaIndex := strings.LastIndex(path[sepIndex:], ",")
  130. if commaIndex <= 0 {
  131. vid, isVolumeIdOnly = path[sepIndex+1:], true
  132. return
  133. }
  134. dotIndex := strings.LastIndex(path[sepIndex:], ".")
  135. vid = path[sepIndex+1 : commaIndex]
  136. fid = path[commaIndex+1:]
  137. ext = ""
  138. if dotIndex > 0 {
  139. fid = path[commaIndex+1 : dotIndex]
  140. ext = path[dotIndex:]
  141. }
  142. }
  143. return
  144. }
  145. func statsCounterHandler(w http.ResponseWriter, r *http.Request) {
  146. m := make(map[string]interface{})
  147. m["Version"] = util.VERSION
  148. m["Counters"] = serverStats
  149. writeJsonQuiet(w, r, http.StatusOK, m)
  150. }
  151. func statsMemoryHandler(w http.ResponseWriter, r *http.Request) {
  152. m := make(map[string]interface{})
  153. m["Version"] = util.VERSION
  154. m["Memory"] = stats.MemStat()
  155. writeJsonQuiet(w, r, http.StatusOK, m)
  156. }