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.

230 lines
6.5 KiB

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