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.

53 lines
1.2 KiB

12 years ago
12 years ago
  1. package operation
  2. import (
  3. "code.google.com/p/weed-fs/go/util"
  4. "encoding/json"
  5. "errors"
  6. _ "fmt"
  7. "math/rand"
  8. "net/url"
  9. "strings"
  10. )
  11. type Location struct {
  12. Url string `json:"url"`
  13. PublicUrl string `json:"publicUrl"`
  14. }
  15. type LookupResult struct {
  16. Locations []Location `json:"locations"`
  17. Error string `json:"error"`
  18. }
  19. func Lookup(server string, vid string) (*LookupResult, error) {
  20. values := make(url.Values)
  21. values.Add("volumeId", vid)
  22. jsonBlob, err := util.Post("http://"+server+"/dir/lookup", values)
  23. if err != nil {
  24. return nil, err
  25. }
  26. var ret LookupResult
  27. err = json.Unmarshal(jsonBlob, &ret)
  28. if err != nil {
  29. return nil, err
  30. }
  31. if ret.Error != "" {
  32. return nil, errors.New(ret.Error)
  33. }
  34. return &ret, nil
  35. }
  36. func LookupFileId(server string, fileId string) (fullUrl string, err error) {
  37. parts := strings.Split(fileId, ",")
  38. if len(parts) != 2 {
  39. return "", errors.New("Invalid fileId " + fileId)
  40. }
  41. lookup, lookupError := Lookup(server, parts[0])
  42. if lookupError != nil {
  43. return "", lookupError
  44. }
  45. if len(lookup.Locations) == 0 {
  46. return "", errors.New("File Not Found")
  47. }
  48. return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl + "/" + fileId, nil
  49. }