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.

231 lines
6.9 KiB

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