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.

229 lines
6.5 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
6 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. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/operation"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. "github.com/chrislusf/seaweedfs/weed/security"
  17. "github.com/chrislusf/seaweedfs/weed/util"
  18. )
  19. var (
  20. OS_UID = uint32(os.Getuid())
  21. OS_GID = uint32(os.Getgid())
  22. )
  23. type FilerPostResult struct {
  24. Name string `json:"name,omitempty"`
  25. Size uint32 `json:"size,omitempty"`
  26. Error string `json:"error,omitempty"`
  27. Fid string `json:"fid,omitempty"`
  28. Url string `json:"url,omitempty"`
  29. }
  30. func (fs *FilerServer) assignNewFileInfo(w http.ResponseWriter, r *http.Request, replication, collection string, dataCenter string) (fileId, urlLocation string, auth security.EncodedJwt, err error) {
  31. ar := &operation.VolumeAssignRequest{
  32. Count: 1,
  33. Replication: replication,
  34. Collection: collection,
  35. Ttl: r.URL.Query().Get("ttl"),
  36. DataCenter: dataCenter,
  37. }
  38. var altRequest *operation.VolumeAssignRequest
  39. if dataCenter != "" {
  40. altRequest = &operation.VolumeAssignRequest{
  41. Count: 1,
  42. Replication: replication,
  43. Collection: collection,
  44. Ttl: r.URL.Query().Get("ttl"),
  45. DataCenter: "",
  46. }
  47. }
  48. assignResult, ae := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, ar, altRequest)
  49. if ae != nil {
  50. glog.Errorf("failing to assign a file id: %v", ae)
  51. writeJsonError(w, r, http.StatusInternalServerError, ae)
  52. err = ae
  53. return
  54. }
  55. fileId = assignResult.Fid
  56. urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
  57. auth = assignResult.Auth
  58. return
  59. }
  60. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request) {
  61. query := r.URL.Query()
  62. replication := query.Get("replication")
  63. if replication == "" {
  64. replication = fs.option.DefaultReplication
  65. }
  66. collection := query.Get("collection")
  67. if collection == "" {
  68. collection = fs.option.Collection
  69. }
  70. dataCenter := query.Get("dataCenter")
  71. if dataCenter == "" {
  72. dataCenter = fs.option.DataCenter
  73. }
  74. if autoChunked := fs.autoChunk(w, r, replication, collection, dataCenter); autoChunked {
  75. return
  76. }
  77. fileId, urlLocation, auth, err := fs.assignNewFileInfo(w, r, replication, collection, dataCenter)
  78. if err != nil || fileId == "" || urlLocation == "" {
  79. glog.V(0).Infof("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, collection, dataCenter)
  80. return
  81. }
  82. glog.V(4).Infof("write %s to %v", r.URL.Path, urlLocation)
  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. // send request to volume server
  94. request := &http.Request{
  95. Method: r.Method,
  96. URL: u,
  97. Proto: r.Proto,
  98. ProtoMajor: r.ProtoMajor,
  99. ProtoMinor: r.ProtoMinor,
  100. Header: r.Header,
  101. Body: r.Body,
  102. Host: r.Host,
  103. ContentLength: r.ContentLength,
  104. }
  105. if auth != "" {
  106. request.Header.Set("Authorization", "BEARER "+string(auth))
  107. }
  108. resp, do_err := util.Do(request)
  109. if do_err != nil {
  110. glog.Errorf("failing to connect to volume server %s: %v, %+v", r.RequestURI, do_err, r.Method)
  111. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  112. return
  113. }
  114. defer resp.Body.Close()
  115. etag := resp.Header.Get("ETag")
  116. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  117. if ra_err != nil {
  118. glog.V(0).Infoln("failing to upload to volume server", r.RequestURI, ra_err.Error())
  119. writeJsonError(w, r, http.StatusInternalServerError, ra_err)
  120. return
  121. }
  122. glog.V(4).Infoln("post result", string(resp_body))
  123. var ret operation.UploadResult
  124. unmarshal_err := json.Unmarshal(resp_body, &ret)
  125. if unmarshal_err != nil {
  126. glog.V(0).Infoln("failing to read upload resonse", r.RequestURI, string(resp_body))
  127. writeJsonError(w, r, http.StatusInternalServerError, unmarshal_err)
  128. return
  129. }
  130. if ret.Error != "" {
  131. glog.V(0).Infoln("failing to post to volume server", r.RequestURI, ret.Error)
  132. writeJsonError(w, r, http.StatusInternalServerError, errors.New(ret.Error))
  133. return
  134. }
  135. // find correct final path
  136. path := r.URL.Path
  137. if strings.HasSuffix(path, "/") {
  138. if ret.Name != "" {
  139. path += ret.Name
  140. } else {
  141. fs.filer.DeleteFileByFileId(fileId)
  142. glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
  143. writeJsonError(w, r, http.StatusInternalServerError,
  144. errors.New("Can not to write to folder "+path+" without a file name"))
  145. return
  146. }
  147. }
  148. // update metadata in filer store
  149. existingEntry, err := fs.filer.FindEntry(filer2.FullPath(path))
  150. crTime := time.Now()
  151. if err == nil && existingEntry != nil {
  152. // glog.V(4).Infof("existing %s => %+v", path, existingEntry)
  153. if existingEntry.IsDirectory() {
  154. path += "/" + ret.Name
  155. } else {
  156. crTime = existingEntry.Crtime
  157. }
  158. }
  159. entry := &filer2.Entry{
  160. FullPath: filer2.FullPath(path),
  161. Attr: filer2.Attr{
  162. Mtime: time.Now(),
  163. Crtime: crTime,
  164. Mode: 0660,
  165. Uid: OS_UID,
  166. Gid: OS_GID,
  167. Replication: replication,
  168. Collection: collection,
  169. TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
  170. },
  171. Chunks: []*filer_pb.FileChunk{{
  172. FileId: fileId,
  173. Size: uint64(ret.Size),
  174. Mtime: time.Now().UnixNano(),
  175. ETag: etag,
  176. }},
  177. }
  178. // glog.V(4).Infof("saving %s => %+v", path, entry)
  179. if db_err := fs.filer.CreateEntry(entry); db_err != nil {
  180. fs.filer.DeleteFileByFileId(fileId)
  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. // send back post result
  186. reply := FilerPostResult{
  187. Name: ret.Name,
  188. Size: ret.Size,
  189. Error: ret.Error,
  190. Fid: fileId,
  191. Url: urlLocation,
  192. }
  193. setEtag(w, etag)
  194. writeJsonQuiet(w, r, http.StatusCreated, reply)
  195. }
  196. // curl -X DELETE http://localhost:8888/path/to
  197. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  198. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  199. isRecursive := r.FormValue("recursive") == "true"
  200. err := fs.filer.DeleteEntryMetaAndData(filer2.FullPath(r.URL.Path), isRecursive, true)
  201. if err != nil {
  202. glog.V(1).Infoln("deleting", r.URL.Path, ":", err.Error())
  203. writeJsonError(w, r, http.StatusInternalServerError, err)
  204. return
  205. }
  206. w.WriteHeader(http.StatusNoContent)
  207. }