2014-05-13 18:25:48 +00:00
|
|
|
package filer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-03-17 07:02:27 +00:00
|
|
|
"net/url"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2014-05-13 18:25:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ApiRequest struct {
|
2017-03-17 07:02:27 +00:00
|
|
|
Command string //"listFiles", "listDirectories", "getFileSize"
|
2014-05-15 06:44:19 +00:00
|
|
|
Directory string
|
|
|
|
FileName string
|
2017-03-17 07:02:27 +00:00
|
|
|
FileId string
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListFilesResult struct {
|
|
|
|
Files []FileEntry
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:02:27 +00:00
|
|
|
func ListFiles(server string, directory string, fileName string) (ret *ListFilesResult, err error) {
|
|
|
|
ret = new(ListFilesResult)
|
|
|
|
if err = call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, ret); err == nil {
|
|
|
|
if ret.Error != "" {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetFileSizeResult struct {
|
|
|
|
Size uint64
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFileSize(server string, fileId string) (ret *GetFileSizeResult, err error) {
|
|
|
|
ret = new(GetFileSizeResult)
|
|
|
|
if err = call(server, ApiRequest{Command: "getFileSize", FileId: fileId}, ret); err == nil {
|
2014-05-13 18:25:48 +00:00
|
|
|
if ret.Error != "" {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
2017-03-17 07:02:27 +00:00
|
|
|
return ret, nil
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
2017-03-17 07:02:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetFileContentResult struct {
|
|
|
|
Content []byte
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFileContent(server string, fileId string) (ret *GetFileContentResult, err error) {
|
|
|
|
ret = new(GetFileContentResult)
|
|
|
|
if err = call(server, ApiRequest{Command: "getFileContent", FileId: fileId}, ret); err == nil {
|
|
|
|
if ret.Error != "" {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListDirectoriesResult struct {
|
|
|
|
Directories []DirectoryEntry
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-03-17 07:02:27 +00:00
|
|
|
func ListDirectories(server string, directory string) (ret *ListDirectoriesResult, err error) {
|
|
|
|
ret = new(ListDirectoriesResult)
|
|
|
|
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)
|
|
|
|
}
|
2017-03-17 07:02:27 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteDirectoryOrFile(server string, path string, isDir bool) error {
|
|
|
|
destUrl := fmt.Sprintf("http://%s%s", server, path)
|
|
|
|
if isDir {
|
|
|
|
destUrl += "/?recursive=true"
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
2017-03-17 07:02:27 +00:00
|
|
|
return util.Delete(destUrl, "")
|
2014-05-13 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|