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.

108 lines
2.5 KiB

  1. package filer
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. )
  9. type ApiRequest struct {
  10. Command string //"listFiles", "listDirectories", "getFileSize"
  11. Directory string
  12. FileName string
  13. FileId string
  14. }
  15. type ListFilesResult struct {
  16. Files []FileEntry
  17. Error string `json:"error,omitempty"`
  18. }
  19. func ListFiles(server string, directory string, fileName string) (ret *ListFilesResult, err error) {
  20. ret = new(ListFilesResult)
  21. if err = call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, ret); err == nil {
  22. if ret.Error != "" {
  23. return nil, errors.New(ret.Error)
  24. }
  25. return ret, nil
  26. }
  27. return nil, err
  28. }
  29. type GetFileSizeResult struct {
  30. Size uint64
  31. Error string `json:"error,omitempty"`
  32. }
  33. func GetFileSize(server string, fileId string) (ret *GetFileSizeResult, err error) {
  34. ret = new(GetFileSizeResult)
  35. if err = call(server, ApiRequest{Command: "getFileSize", FileId: fileId}, ret); err == nil {
  36. if ret.Error != "" {
  37. return nil, errors.New(ret.Error)
  38. }
  39. return ret, nil
  40. }
  41. return nil, err
  42. }
  43. type GetFileContentResult struct {
  44. Content []byte
  45. Error string `json:"error,omitempty"`
  46. }
  47. func GetFileContent(server string, fileId string) (ret *GetFileContentResult, err error) {
  48. ret = new(GetFileContentResult)
  49. if err = call(server, ApiRequest{Command: "getFileContent", FileId: fileId}, ret); err == nil {
  50. if ret.Error != "" {
  51. return nil, errors.New(ret.Error)
  52. }
  53. return ret, nil
  54. }
  55. return nil, err
  56. }
  57. type ListDirectoriesResult struct {
  58. Directories []DirectoryEntry
  59. Error string `json:"error,omitempty"`
  60. }
  61. func ListDirectories(server string, directory string) (ret *ListDirectoriesResult, err error) {
  62. ret = new(ListDirectoriesResult)
  63. if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, ret); err == nil {
  64. if ret.Error != "" {
  65. return nil, errors.New(ret.Error)
  66. }
  67. return ret, nil
  68. }
  69. return nil, err
  70. }
  71. func DeleteDirectoryOrFile(server string, path string, isDir bool) error {
  72. destUrl := fmt.Sprintf("http://%s%s", server, path)
  73. if isDir {
  74. destUrl += "/?recursive=true"
  75. }
  76. return util.Delete(destUrl, "")
  77. }
  78. func call(server string, request ApiRequest, ret interface{}) error {
  79. b, err := json.Marshal(request)
  80. if err != nil {
  81. fmt.Println("error:", err)
  82. return nil
  83. }
  84. values := make(url.Values)
  85. values.Add("request", string(b))
  86. jsonBlob, err := util.Post("http://"+server+"/__api__", values)
  87. if err != nil {
  88. return err
  89. }
  90. err = json.Unmarshal(jsonBlob, ret)
  91. if err != nil {
  92. return err
  93. }
  94. return nil
  95. }