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.

232 lines
6.6 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. ctx := context.Background()
  63. query := r.URL.Query()
  64. replication := query.Get("replication")
  65. if replication == "" {
  66. replication = fs.option.DefaultReplication
  67. }
  68. collection := query.Get("collection")
  69. if collection == "" {
  70. collection = fs.option.Collection
  71. }
  72. dataCenter := query.Get("dataCenter")
  73. if dataCenter == "" {
  74. dataCenter = fs.option.DataCenter
  75. }
  76. if autoChunked := fs.autoChunk(ctx, w, r, replication, collection, dataCenter); autoChunked {
  77. return
  78. }
  79. fileId, urlLocation, auth, err := fs.assignNewFileInfo(w, r, replication, collection, dataCenter)
  80. if err != nil || fileId == "" || urlLocation == "" {
  81. glog.V(0).Infof("fail to allocate volume for %s, collection:%s, datacenter:%s", r.URL.Path, collection, dataCenter)
  82. return
  83. }
  84. glog.V(4).Infof("write %s to %v", r.URL.Path, urlLocation)
  85. u, _ := url.Parse(urlLocation)
  86. // This allows a client to generate a chunk manifest and submit it to the filer -- it is a little off
  87. // because they need to provide FIDs instead of file paths...
  88. cm, _ := strconv.ParseBool(query.Get("cm"))
  89. if cm {
  90. q := u.Query()
  91. q.Set("cm", "true")
  92. u.RawQuery = q.Encode()
  93. }
  94. glog.V(4).Infoln("post to", u)
  95. // send request to volume server
  96. request := &http.Request{
  97. Method: r.Method,
  98. URL: u,
  99. Proto: r.Proto,
  100. ProtoMajor: r.ProtoMajor,
  101. ProtoMinor: r.ProtoMinor,
  102. Header: r.Header,
  103. Body: r.Body,
  104. Host: r.Host,
  105. ContentLength: r.ContentLength,
  106. }
  107. if auth != "" {
  108. request.Header.Set("Authorization", "BEARER "+string(auth))
  109. }
  110. resp, do_err := util.Do(request)
  111. if do_err != nil {
  112. glog.Errorf("failing to connect to volume server %s: %v, %+v", r.RequestURI, do_err, r.Method)
  113. writeJsonError(w, r, http.StatusInternalServerError, do_err)
  114. return
  115. }
  116. defer resp.Body.Close()
  117. etag := resp.Header.Get("ETag")
  118. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  119. if ra_err != nil {
  120. glog.V(0).Infoln("failing to upload to volume server", r.RequestURI, ra_err.Error())
  121. writeJsonError(w, r, http.StatusInternalServerError, ra_err)
  122. return
  123. }
  124. glog.V(4).Infoln("post result", string(resp_body))
  125. var ret operation.UploadResult
  126. unmarshal_err := json.Unmarshal(resp_body, &ret)
  127. if unmarshal_err != nil {
  128. glog.V(0).Infoln("failing to read upload resonse", r.RequestURI, string(resp_body))
  129. writeJsonError(w, r, http.StatusInternalServerError, unmarshal_err)
  130. return
  131. }
  132. if ret.Error != "" {
  133. glog.V(0).Infoln("failing to post to volume server", r.RequestURI, ret.Error)
  134. writeJsonError(w, r, http.StatusInternalServerError, errors.New(ret.Error))
  135. return
  136. }
  137. // find correct final path
  138. path := r.URL.Path
  139. if strings.HasSuffix(path, "/") {
  140. if ret.Name != "" {
  141. path += ret.Name
  142. } else {
  143. fs.filer.DeleteFileByFileId(fileId)
  144. glog.V(0).Infoln("Can not to write to folder", path, "without a file name!")
  145. writeJsonError(w, r, http.StatusInternalServerError,
  146. errors.New("Can not to write to folder "+path+" without a file name"))
  147. return
  148. }
  149. }
  150. // update metadata in filer store
  151. existingEntry, err := fs.filer.FindEntry(ctx, filer2.FullPath(path))
  152. crTime := time.Now()
  153. if err == nil && existingEntry != nil {
  154. // glog.V(4).Infof("existing %s => %+v", path, existingEntry)
  155. if existingEntry.IsDirectory() {
  156. path += "/" + ret.Name
  157. } else {
  158. crTime = existingEntry.Crtime
  159. }
  160. }
  161. entry := &filer2.Entry{
  162. FullPath: filer2.FullPath(path),
  163. Attr: filer2.Attr{
  164. Mtime: time.Now(),
  165. Crtime: crTime,
  166. Mode: 0660,
  167. Uid: OS_UID,
  168. Gid: OS_GID,
  169. Replication: replication,
  170. Collection: collection,
  171. TtlSec: int32(util.ParseInt(r.URL.Query().Get("ttl"), 0)),
  172. },
  173. Chunks: []*filer_pb.FileChunk{{
  174. FileId: fileId,
  175. Size: uint64(ret.Size),
  176. Mtime: time.Now().UnixNano(),
  177. ETag: etag,
  178. }},
  179. }
  180. // glog.V(4).Infof("saving %s => %+v", path, entry)
  181. if db_err := fs.filer.CreateEntry(ctx, entry); db_err != nil {
  182. fs.filer.DeleteFileByFileId(fileId)
  183. glog.V(0).Infof("failing to write %s to filer server : %v", path, db_err)
  184. writeJsonError(w, r, http.StatusInternalServerError, db_err)
  185. return
  186. }
  187. // send back post result
  188. reply := FilerPostResult{
  189. Name: ret.Name,
  190. Size: ret.Size,
  191. Error: ret.Error,
  192. Fid: fileId,
  193. Url: urlLocation,
  194. }
  195. setEtag(w, etag)
  196. writeJsonQuiet(w, r, http.StatusCreated, reply)
  197. }
  198. // curl -X DELETE http://localhost:8888/path/to
  199. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  200. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  201. isRecursive := r.FormValue("recursive") == "true"
  202. err := fs.filer.DeleteEntryMetaAndData(context.Background(), filer2.FullPath(r.URL.Path), isRecursive, true)
  203. if err != nil {
  204. glog.V(1).Infoln("deleting", r.URL.Path, ":", err.Error())
  205. writeJsonError(w, r, http.StatusInternalServerError, err)
  206. return
  207. }
  208. w.WriteHeader(http.StatusNoContent)
  209. }