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.

211 lines
6.3 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
  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. }
  46. assignResult, ae := operation.Assign(fs.filer.GetMaster(), ar)
  47. if ae != nil {
  48. glog.V(0).Infoln("failing to assign a file id", ae.Error())
  49. writeJsonError(w, r, http.StatusInternalServerError, ae)
  50. err = ae
  51. return
  52. }
  53. fileId = assignResult.Fid
  54. urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
  55. return
  56. }
  57. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  58. query := r.URL.Query()
  59. replication := query.Get("replication")
  60. if replication == "" {
  61. replication = fs.defaultReplication
  62. }
  63. collection := query.Get("collection")
  64. if collection == "" {
  65. collection = fs.collection
  66. }
  67. if autoChunked := fs.autoChunk(w, r, replication, collection); autoChunked {
  68. return
  69. }
  70. var fileId, urlLocation string
  71. var err error
  72. if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data; boundary=") {
  73. fileId, urlLocation, err = fs.multipartUploadAnalyzer(w, r, replication, collection)
  74. if err != nil {
  75. return
  76. }
  77. } else {
  78. fileId, urlLocation, err = fs.monolithicUploadAnalyzer(w, r, replication, collection)
  79. if err != nil {
  80. return
  81. }
  82. }
  83. u, _ := url.Parse(urlLocation)
  84. // This allows a client to generate a chunk manifest and submit it to the filer -- it is a little off
  85. // because they need to provide FIDs instead of file paths...
  86. cm, _ := strconv.ParseBool(query.Get("cm"))
  87. if cm {
  88. q := u.Query()
  89. q.Set("cm", "true")
  90. u.RawQuery = q.Encode()
  91. }
  92. glog.V(4).Infoln("post to", u)
  93. request := &http.Request{
  94. Method: r.Method,
  95. URL: u,
  96. Proto: r.Proto,
  97. ProtoMajor: r.ProtoMajor,
  98. ProtoMinor: r.ProtoMinor,
  99. Header: r.Header,
  100. Body: r.Body,
  101. Host: r.Host,
  102. ContentLength: r.ContentLength,
  103. }
  104. resp, do_err := util.Do(request)
  105. if do_err != nil {
  106. glog.V(0).Infoln("failing to connect to volume server", r.RequestURI, do_err.Error())
  107. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  108. return
  109. }
  110. defer resp.Body.Close()
  111. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  112. if ra_err != nil {
  113. glog.V(0).Infoln("failing to upload to volume server", r.RequestURI, ra_err.Error())
  114. writeJsonError(w, r, http.StatusInternalServerError, ra_err)
  115. return
  116. }
  117. glog.V(4).Infoln("post result", string(resp_body))
  118. var ret operation.UploadResult
  119. unmarshal_err := json.Unmarshal(resp_body, &ret)
  120. if unmarshal_err != nil {
  121. glog.V(0).Infoln("failing to read upload resonse", r.RequestURI, string(resp_body))
  122. writeJsonError(w, r, http.StatusInternalServerError, unmarshal_err)
  123. return
  124. }
  125. if ret.Error != "" {
  126. glog.V(0).Infoln("failing to post to volume server", r.RequestURI, ret.Error)
  127. writeJsonError(w, r, http.StatusInternalServerError, errors.New(ret.Error))
  128. return
  129. }
  130. path := r.URL.Path
  131. if strings.HasSuffix(path, "/") {
  132. if ret.Name != "" {
  133. path += ret.Name
  134. } else {
  135. operation.DeleteFile(fs.filer.GetMaster(), fileId, fs.jwt(fileId)) //clean up
  136. glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
  137. writeJsonError(w, r, http.StatusInternalServerError,
  138. errors.New("Can not to write to folder "+path+" without a file name"))
  139. return
  140. }
  141. }
  142. // also delete the old fid unless PUT operation
  143. if r.Method != "PUT" {
  144. if entry, err := fs.filer.FindEntry(filer2.FullPath(path)); err == nil {
  145. oldFid := entry.Chunks[0].FileId
  146. operation.DeleteFile(fs.filer.GetMaster(), oldFid, fs.jwt(oldFid))
  147. } else if err != nil && err != filer2.ErrNotFound {
  148. glog.V(0).Infof("error %v occur when finding %s in filer store", err, path)
  149. }
  150. }
  151. glog.V(4).Infoln("saving", path, "=>", fileId)
  152. entry := &filer2.Entry{
  153. FullPath: filer2.FullPath(path),
  154. Attr: filer2.Attr{
  155. Mtime: time.Now(),
  156. Crtime: time.Now(),
  157. Mode: 0660,
  158. Replication: replication,
  159. Collection: collection,
  160. TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
  161. },
  162. Chunks: []*filer_pb.FileChunk{{
  163. FileId: fileId,
  164. Size: uint64(r.ContentLength),
  165. Mtime: time.Now().UnixNano(),
  166. }},
  167. }
  168. if db_err := fs.filer.CreateEntry(entry); db_err != nil {
  169. operation.DeleteFile(fs.filer.GetMaster(), fileId, fs.jwt(fileId)) //clean up
  170. glog.V(0).Infof("failing to write %s to filer server : %v", path, db_err)
  171. writeJsonError(w, r, http.StatusInternalServerError, db_err)
  172. return
  173. }
  174. reply := FilerPostResult{
  175. Name: ret.Name,
  176. Size: ret.Size,
  177. Error: ret.Error,
  178. Fid: fileId,
  179. Url: urlLocation,
  180. }
  181. writeJsonQuiet(w, r, http.StatusCreated, reply)
  182. }
  183. // curl -X DELETE http://localhost:8888/path/to
  184. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  185. err := fs.filer.DeleteEntryMetaAndData(filer2.FullPath(r.URL.Path), true)
  186. if err != nil {
  187. glog.V(4).Infoln("deleting", r.URL.Path, ":", err.Error())
  188. writeJsonError(w, r, http.StatusInternalServerError, err)
  189. return
  190. }
  191. writeJsonQuiet(w, r, http.StatusAccepted, map[string]string{"error": ""})
  192. }