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.

125 lines
3.0 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 []DirectoryName
  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. type LookupDirectoryEntryResult struct {
  72. Found bool
  73. FileId string
  74. Error string `json:"error,omitempty"`
  75. }
  76. func LookupDirectoryEntry(server string, directory string, name string) (ret *LookupDirectoryEntryResult, err error) {
  77. ret = new(LookupDirectoryEntryResult)
  78. if err := call(server, ApiRequest{Command: "lookupDirectoryEntry", Directory: directory, FileName: name}, ret); err == nil {
  79. if ret.Error != "" {
  80. return nil, errors.New(ret.Error)
  81. }
  82. return ret, nil
  83. }
  84. return nil, err
  85. }
  86. func DeleteDirectoryOrFile(server string, path string, isDir bool) error {
  87. destUrl := fmt.Sprintf("http://%s%s", server, path)
  88. if isDir {
  89. destUrl += "/?recursive=true"
  90. }
  91. return util.Delete(destUrl, "")
  92. }
  93. func call(server string, request ApiRequest, ret interface{}) error {
  94. b, err := json.Marshal(request)
  95. if err != nil {
  96. fmt.Println("error:", err)
  97. return nil
  98. }
  99. values := make(url.Values)
  100. values.Add("request", string(b))
  101. jsonBlob, err := util.Post("http://"+server+"/__api__", values)
  102. if err != nil {
  103. return err
  104. }
  105. err = json.Unmarshal(jsonBlob, ret)
  106. if err != nil {
  107. return err
  108. }
  109. return nil
  110. }