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.

52 lines
1.2 KiB

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