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.

173 lines
4.2 KiB

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