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.

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