2012-09-20 09:47:32 +00:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2013-02-27 06:54:22 +00:00
|
|
|
"code.google.com/p/weed-fs/go/util"
|
2013-01-17 08:56:56 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
_ "fmt"
|
|
|
|
"net/url"
|
2014-02-15 01:10:49 +00:00
|
|
|
"strings"
|
2012-09-20 09:47:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Location struct {
|
2013-02-10 17:44:44 +00:00
|
|
|
Url string `json:"url"`
|
|
|
|
PublicUrl string `json:"publicUrl"`
|
2012-09-20 09:47:32 +00:00
|
|
|
}
|
|
|
|
type LookupResult struct {
|
2013-02-10 17:44:44 +00:00
|
|
|
Locations []Location `json:"locations"`
|
|
|
|
Error string `json:"error"`
|
2012-09-20 09:47:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 01:10:49 +00:00
|
|
|
func Lookup(server string, vid string) (*LookupResult, error) {
|
2013-01-17 08:56:56 +00:00
|
|
|
values := make(url.Values)
|
2014-02-15 01:10:49 +00:00
|
|
|
values.Add("volumeId", vid)
|
2013-01-17 08:56:56 +00: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
|
2012-09-20 09:47:32 +00:00
|
|
|
}
|
2013-11-19 07:03:59 +00:00
|
|
|
|
|
|
|
func LookupFileId(server string, fileId string) (fullUrl string, err error) {
|
2014-02-15 01:10:49 +00:00
|
|
|
a := strings.Split(fileId, ",")
|
|
|
|
if len(a) != 2 {
|
|
|
|
return "", errors.New("Invalid fileId " + fileId)
|
2013-11-19 07:03:59 +00:00
|
|
|
}
|
2014-02-15 01:10:49 +00:00
|
|
|
lookup, lookupError := Lookup(server, a[0])
|
2013-11-19 07:03:59 +00:00
|
|
|
if lookupError != nil {
|
|
|
|
return "", lookupError
|
|
|
|
}
|
|
|
|
if len(lookup.Locations) == 0 {
|
|
|
|
return "", errors.New("File Not Found")
|
|
|
|
}
|
|
|
|
return "http://" + lookup.Locations[0].PublicUrl + "/" + fileId, nil
|
|
|
|
}
|