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.

177 lines
4.6 KiB

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