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.

222 lines
6.6 KiB

9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/chrislusf/seaweedfs/weed/filer2"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/operation"
  14. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. type FilerPostResult struct {
  18. Name string `json:"name,omitempty"`
  19. Size uint32 `json:"size,omitempty"`
  20. Error string `json:"error,omitempty"`
  21. Fid string `json:"fid,omitempty"`
  22. Url string `json:"url,omitempty"`
  23. }
  24. func (fs *FilerServer) queryFileInfoByPath(w http.ResponseWriter, r *http.Request, path string) (fileId, urlLocation string, err error) {
  25. var entry *filer2.Entry
  26. if entry, err = fs.filer.FindEntry(filer2.FullPath(path)); err != nil {
  27. glog.V(0).Infoln("failing to find path in filer store", path, err.Error())
  28. writeJsonError(w, r, http.StatusInternalServerError, err)
  29. } else {
  30. fileId = entry.Chunks[0].FileId
  31. urlLocation, err = operation.LookupFileId(fs.filer.GetMaster(), fileId)
  32. if err != nil {
  33. glog.V(1).Infof("operation LookupFileId %s failed, err is %s", fileId, err.Error())
  34. w.WriteHeader(http.StatusNotFound)
  35. }
  36. }
  37. return
  38. }
  39. func (fs *FilerServer) assignNewFileInfo(w http.ResponseWriter, r *http.Request, replication, collection string) (fileId, urlLocation string, err error) {
  40. ar := &operation.VolumeAssignRequest{
  41. Count: 1,
  42. Replication: replication,
  43. Collection: collection,
  44. Ttl: r.URL.Query().Get("ttl"),
  45. DataCenter: fs.option.DataCenter,
  46. }
  47. var altRequest *operation.VolumeAssignRequest
  48. if fs.option.DataCenter != "" {
  49. altRequest = &operation.VolumeAssignRequest{
  50. Count: 1,
  51. Replication: replication,
  52. Collection: collection,
  53. Ttl: r.URL.Query().Get("ttl"),
  54. DataCenter: "",
  55. }
  56. }
  57. assignResult, ae := operation.Assign(fs.filer.GetMaster(), ar, altRequest)
  58. if ae != nil {
  59. glog.V(0).Infoln("failing to assign a file id", ae.Error())
  60. writeJsonError(w, r, http.StatusInternalServerError, ae)
  61. err = ae
  62. return
  63. }
  64. fileId = assignResult.Fid
  65. urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
  66. return
  67. }
  68. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  69. query := r.URL.Query()
  70. replication := query.Get("replication")
  71. if replication == "" {
  72. replication = fs.option.DefaultReplication
  73. }
  74. collection := query.Get("collection")
  75. if collection == "" {
  76. collection = fs.option.Collection
  77. }
  78. if autoChunked := fs.autoChunk(w, r, replication, collection); autoChunked {
  79. return
  80. }
  81. var fileId, urlLocation string
  82. var err error
  83. if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data; boundary=") {
  84. fileId, urlLocation, err = fs.multipartUploadAnalyzer(w, r, replication, collection)
  85. if err != nil {
  86. return
  87. }
  88. } else {
  89. fileId, urlLocation, err = fs.monolithicUploadAnalyzer(w, r, replication, collection)
  90. if err != nil {
  91. return
  92. }
  93. }
  94. u, _ := url.Parse(urlLocation)
  95. // This allows a client to generate a chunk manifest and submit it to the filer -- it is a little off
  96. // because they need to provide FIDs instead of file paths...
  97. cm, _ := strconv.ParseBool(query.Get("cm"))
  98. if cm {
  99. q := u.Query()
  100. q.Set("cm", "true")
  101. u.RawQuery = q.Encode()
  102. }
  103. glog.V(4).Infoln("post to", u)
  104. request := &http.Request{
  105. Method: r.Method,
  106. URL: u,
  107. Proto: r.Proto,
  108. ProtoMajor: r.ProtoMajor,
  109. ProtoMinor: r.ProtoMinor,
  110. Header: r.Header,
  111. Body: r.Body,
  112. Host: r.Host,
  113. ContentLength: r.ContentLength,
  114. }
  115. resp, do_err := util.Do(request)
  116. if do_err != nil {
  117. glog.V(0).Infoln("failing to connect to volume server", r.RequestURI, do_err.Error())
  118. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  119. return
  120. }
  121. defer resp.Body.Close()
  122. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  123. if ra_err != nil {
  124. glog.V(0).Infoln("failing to upload to volume server", r.RequestURI, ra_err.Error())
  125. writeJsonError(w, r, http.StatusInternalServerError, ra_err)
  126. return
  127. }
  128. glog.V(4).Infoln("post result", string(resp_body))
  129. var ret operation.UploadResult
  130. unmarshal_err := json.Unmarshal(resp_body, &ret)
  131. if unmarshal_err != nil {
  132. glog.V(0).Infoln("failing to read upload resonse", r.RequestURI, string(resp_body))
  133. writeJsonError(w, r, http.StatusInternalServerError, unmarshal_err)
  134. return
  135. }
  136. if ret.Error != "" {
  137. glog.V(0).Infoln("failing to post to volume server", r.RequestURI, ret.Error)
  138. writeJsonError(w, r, http.StatusInternalServerError, errors.New(ret.Error))
  139. return
  140. }
  141. path := r.URL.Path
  142. if strings.HasSuffix(path, "/") {
  143. if ret.Name != "" {
  144. path += ret.Name
  145. } else {
  146. operation.DeleteFile(fs.filer.GetMaster(), fileId, fs.jwt(fileId)) //clean up
  147. glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
  148. writeJsonError(w, r, http.StatusInternalServerError,
  149. errors.New("Can not to write to folder "+path+" without a file name"))
  150. return
  151. }
  152. }
  153. // also delete the old fid unless PUT operation
  154. if r.Method != "PUT" {
  155. if entry, err := fs.filer.FindEntry(filer2.FullPath(path)); err == nil {
  156. oldFid := entry.Chunks[0].FileId
  157. operation.DeleteFile(fs.filer.GetMaster(), oldFid, fs.jwt(oldFid))
  158. } else if err != nil && err != filer2.ErrNotFound {
  159. glog.V(0).Infof("error %v occur when finding %s in filer store", err, path)
  160. }
  161. }
  162. glog.V(4).Infoln("saving", path, "=>", fileId)
  163. entry := &filer2.Entry{
  164. FullPath: filer2.FullPath(path),
  165. Attr: filer2.Attr{
  166. Mtime: time.Now(),
  167. Crtime: time.Now(),
  168. Mode: 0660,
  169. Replication: replication,
  170. Collection: collection,
  171. TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
  172. },
  173. Chunks: []*filer_pb.FileChunk{{
  174. FileId: fileId,
  175. Size: uint64(r.ContentLength),
  176. Mtime: time.Now().UnixNano(),
  177. }},
  178. }
  179. if db_err := fs.filer.CreateEntry(entry); db_err != nil {
  180. operation.DeleteFile(fs.filer.GetMaster(), fileId, fs.jwt(fileId)) //clean up
  181. glog.V(0).Infof("failing to write %s to filer server : %v", path, db_err)
  182. writeJsonError(w, r, http.StatusInternalServerError, db_err)
  183. return
  184. }
  185. reply := FilerPostResult{
  186. Name: ret.Name,
  187. Size: ret.Size,
  188. Error: ret.Error,
  189. Fid: fileId,
  190. Url: urlLocation,
  191. }
  192. writeJsonQuiet(w, r, http.StatusCreated, reply)
  193. }
  194. // curl -X DELETE http://localhost:8888/path/to
  195. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  196. err := fs.filer.DeleteEntryMetaAndData(filer2.FullPath(r.URL.Path), true)
  197. if err != nil {
  198. glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
  199. writeJsonError(w, r, http.StatusInternalServerError, err)
  200. return
  201. }
  202. writeJsonQuiet(w, r, http.StatusAccepted, map[string]string{"error": ""})
  203. }