Files
seaweedfs/go/operation/lookup_volume_id.go

54 lines
1.2 KiB
Go
Raw Normal View History

package operation
import (
2013-02-26 22:54:22 -08:00
"code.google.com/p/weed-fs/go/util"
2013-01-17 00:56:56 -08:00
"encoding/json"
"errors"
_ "fmt"
2014-03-30 11:28:04 -07:00
"math/rand"
2013-01-17 00:56:56 -08:00
"net/url"
"strings"
)
type Location struct {
2013-02-10 09:44:44 -08:00
Url string `json:"url"`
PublicUrl string `json:"publicUrl"`
}
type LookupResult struct {
2013-02-10 09:44:44 -08:00
Locations []Location `json:"locations"`
Error string `json:"error"`
}
func Lookup(server string, vid string) (*LookupResult, error) {
2013-01-17 00:56:56 -08:00
values := make(url.Values)
values.Add("volumeId", vid)
2013-01-17 00:56:56 -08:00
jsonBlob, err := util.Post("http://"+server+"/dir/lookup", values)
if err != nil {
return nil, err
}
var ret LookupResult
err = json.Unmarshal(jsonBlob, &ret)
if err != nil {
return nil, err
}
if ret.Error != "" {
return nil, errors.New(ret.Error)
}
return &ret, nil
}
2013-11-18 23:03:59 -08:00
func LookupFileId(server string, fileId string) (fullUrl string, err error) {
2014-03-30 11:28:04 -07:00
parts := strings.Split(fileId, ",")
if len(parts) != 2 {
return "", errors.New("Invalid fileId " + fileId)
2013-11-18 23:03:59 -08:00
}
2014-03-30 11:28:04 -07:00
lookup, lookupError := Lookup(server, parts[0])
2013-11-18 23:03:59 -08:00
if lookupError != nil {
return "", lookupError
}
if len(lookup.Locations) == 0 {
return "", errors.New("File Not Found")
}
2014-03-30 11:28:04 -07:00
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl + "/" + fileId, nil
2013-11-18 23:03:59 -08:00
}