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.

73 lines
2.0 KiB

  1. package weed_server
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/operation"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func (fs *FilerServer) apiHandler(w http.ResponseWriter, r *http.Request) {
  12. request := r.FormValue("request")
  13. apiRequest := filer.ApiRequest{}
  14. err := json.Unmarshal([]byte(request), &apiRequest)
  15. if err != nil {
  16. glog.V(0).Infoln("failing to read request", r.RequestURI, request)
  17. writeJsonError(w, r, http.StatusInternalServerError, err)
  18. }
  19. switch apiRequest.Command {
  20. case "listDirectories":
  21. res := filer.ListDirectoriesResult{}
  22. res.Directories, err = fs.filer.ListDirectories(apiRequest.Directory)
  23. if err != nil {
  24. res.Error = err.Error()
  25. }
  26. writeJsonQuiet(w, r, http.StatusOK, res)
  27. case "listFiles":
  28. res := filer.ListFilesResult{}
  29. res.Files, err = fs.filer.ListFiles(apiRequest.Directory, apiRequest.FileName, 100)
  30. if err != nil {
  31. res.Error = err.Error()
  32. }
  33. writeJsonQuiet(w, r, http.StatusOK, res)
  34. case "getFileSize":
  35. res := filer.GetFileSizeResult{}
  36. server, err := operation.LookupFileId(fs.getMasterNode(), apiRequest.FileId)
  37. if err != nil {
  38. res.Error = err.Error()
  39. writeJsonQuiet(w, r, http.StatusOK, res)
  40. return
  41. }
  42. head, err := util.Head(server)
  43. if err != nil {
  44. res.Error = err.Error()
  45. writeJsonQuiet(w, r, http.StatusOK, res)
  46. return
  47. }
  48. res.Size, err = strconv.ParseUint(head.Get("Content-Length"), 10, 0)
  49. if err != nil {
  50. res.Error = err.Error()
  51. }
  52. writeJsonQuiet(w, r, http.StatusOK, res)
  53. case "getFileContent":
  54. res := filer.GetFileContentResult{}
  55. server, err := operation.LookupFileId(fs.getMasterNode(), apiRequest.FileId)
  56. if err != nil {
  57. res.Error = err.Error()
  58. writeJsonQuiet(w, r, http.StatusOK, res)
  59. return
  60. }
  61. res.Content, err = util.Get(server)
  62. if err != nil {
  63. res.Error = err.Error()
  64. writeJsonQuiet(w, r, http.StatusOK, res)
  65. return
  66. }
  67. writeJsonQuiet(w, r, http.StatusOK, res)
  68. }
  69. }