2014-05-13 18:25:48 +00:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2014-05-13 18:25:48 +00:00
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ApiRequest struct {
|
2014-05-15 06:44:19 +00:00
|
|
|
Command string //"listFiles", "listDirectories"
|
|
|
|
Directory string
|
|
|
|
FileName string
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListFilesResult struct {
|
|
|
|
Files []FileEntry
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:44:19 +00:00
|
|
|
func ListFiles(server string, directory string, fileName string) (*ListFilesResult, error) {
|
2014-05-13 18:25:48 +00:00
|
|
|
var ret ListFilesResult
|
2014-05-15 06:44:19 +00:00
|
|
|
if err := call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, &ret); err == nil {
|
2014-05-13 18:25:48 +00:00
|
|
|
if ret.Error != "" {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListDirectoriesResult struct {
|
|
|
|
Directories []DirectoryEntry
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:44:19 +00:00
|
|
|
func ListDirectories(server string, directory string) (*ListDirectoriesResult, error) {
|
2014-05-13 18:25:48 +00:00
|
|
|
var ret ListDirectoriesResult
|
2014-05-15 06:44:19 +00:00
|
|
|
if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, &ret); err == nil {
|
2014-05-13 18:25:48 +00:00
|
|
|
if ret.Error != "" {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func call(server string, request ApiRequest, ret interface{}) error {
|
|
|
|
b, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Add("request", string(b))
|
|
|
|
jsonBlob, err := util.Post("http://"+server+"/__api__", values)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(jsonBlob, ret)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|