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.

70 lines
1.5 KiB

  1. package filer
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/chrislusf/weed-fs/go/util"
  7. "net/url"
  8. )
  9. type ApiRequest struct {
  10. Command string //"listFiles", "listDirectories"
  11. Directory string
  12. FileName string
  13. }
  14. type ListFilesResult struct {
  15. Files []FileEntry
  16. Error string `json:"error,omitempty"`
  17. }
  18. func ListFiles(server string, directory string, fileName string) (*ListFilesResult, error) {
  19. var ret ListFilesResult
  20. if err := call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, &ret); err == nil {
  21. if ret.Error != "" {
  22. return nil, errors.New(ret.Error)
  23. }
  24. return &ret, nil
  25. } else {
  26. return nil, err
  27. }
  28. }
  29. type ListDirectoriesResult struct {
  30. Directories []DirectoryEntry
  31. Error string `json:"error,omitempty"`
  32. }
  33. func ListDirectories(server string, directory string) (*ListDirectoriesResult, error) {
  34. var ret ListDirectoriesResult
  35. if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, &ret); err == nil {
  36. if ret.Error != "" {
  37. return nil, errors.New(ret.Error)
  38. }
  39. return &ret, nil
  40. } else {
  41. return nil, err
  42. }
  43. }
  44. func call(server string, request ApiRequest, ret interface{}) error {
  45. b, err := json.Marshal(request)
  46. if err != nil {
  47. fmt.Println("error:", err)
  48. return nil
  49. }
  50. values := make(url.Values)
  51. values.Add("request", string(b))
  52. jsonBlob, err := util.Post("http://"+server+"/__api__", values)
  53. if err != nil {
  54. return err
  55. }
  56. err = json.Unmarshal(jsonBlob, ret)
  57. if err != nil {
  58. return err
  59. }
  60. return nil
  61. }