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. import (
  4. "code.google.com/p/weed-fs/go/util"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "net/url"
  9. )
  10. type ApiRequest struct {
  11. Command string //"listFiles", "listDirectories"
  12. Directory string
  13. FileName 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) (*ListFilesResult, error) {
  20. var ret 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. } else {
  27. return nil, err
  28. }
  29. }
  30. type ListDirectoriesResult struct {
  31. Directories []DirectoryEntry
  32. Error string `json:"error,omitempty"`
  33. }
  34. func ListDirectories(server string, directory string) (*ListDirectoriesResult, error) {
  35. var ret ListDirectoriesResult
  36. if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, &ret); err == nil {
  37. if ret.Error != "" {
  38. return nil, errors.New(ret.Error)
  39. }
  40. return &ret, nil
  41. } else {
  42. return nil, err
  43. }
  44. }
  45. func call(server string, request ApiRequest, ret interface{}) error {
  46. b, err := json.Marshal(request)
  47. if err != nil {
  48. fmt.Println("error:", err)
  49. return nil
  50. }
  51. values := make(url.Values)
  52. values.Add("request", string(b))
  53. jsonBlob, err := util.Post("http://"+server+"/__api__", values)
  54. if err != nil {
  55. return err
  56. }
  57. err = json.Unmarshal(jsonBlob, ret)
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }