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.

80 lines
2.3 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 "lookupDirectoryEntry":
  21. res := filer.LookupDirectoryEntryResult{}
  22. res.Found, res.FileId, err = fs.filer.LookupDirectoryEntry(apiRequest.Directory, apiRequest.FileName)
  23. if err != nil {
  24. res.Error = err.Error()
  25. }
  26. writeJsonQuiet(w, r, http.StatusOK, res)
  27. case "listDirectories":
  28. res := filer.ListDirectoriesResult{}
  29. res.Directories, err = fs.filer.ListDirectories(apiRequest.Directory)
  30. if err != nil {
  31. res.Error = err.Error()
  32. }
  33. writeJsonQuiet(w, r, http.StatusOK, res)
  34. case "listFiles":
  35. res := filer.ListFilesResult{}
  36. res.Files, err = fs.filer.ListFiles(apiRequest.Directory, apiRequest.FileName, 1000)
  37. if err != nil {
  38. res.Error = err.Error()
  39. }
  40. writeJsonQuiet(w, r, http.StatusOK, res)
  41. case "getFileSize":
  42. res := filer.GetFileSizeResult{}
  43. server, err := operation.LookupFileId(fs.getMasterNode(), apiRequest.FileId)
  44. if err != nil {
  45. res.Error = err.Error()
  46. writeJsonQuiet(w, r, http.StatusOK, res)
  47. return
  48. }
  49. head, err := util.Head(server)
  50. if err != nil {
  51. res.Error = err.Error()
  52. writeJsonQuiet(w, r, http.StatusOK, res)
  53. return
  54. }
  55. res.Size, err = strconv.ParseUint(head.Get("Content-Length"), 10, 0)
  56. if err != nil {
  57. res.Error = err.Error()
  58. }
  59. writeJsonQuiet(w, r, http.StatusOK, res)
  60. case "getFileContent":
  61. res := filer.GetFileContentResult{}
  62. server, err := operation.LookupFileId(fs.getMasterNode(), apiRequest.FileId)
  63. if err != nil {
  64. res.Error = err.Error()
  65. writeJsonQuiet(w, r, http.StatusOK, res)
  66. return
  67. }
  68. res.Content, err = util.Get(server)
  69. if err != nil {
  70. res.Error = err.Error()
  71. writeJsonQuiet(w, r, http.StatusOK, res)
  72. return
  73. }
  74. writeJsonQuiet(w, r, http.StatusOK, res)
  75. }
  76. }