|
@ -4,16 +4,16 @@ import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"errors" |
|
|
"errors" |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"net/url" |
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util" |
|
|
"github.com/chrislusf/seaweedfs/weed/util" |
|
|
|
|
|
|
|
|
"net/url" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type ApiRequest struct { |
|
|
type ApiRequest struct { |
|
|
Command string //"listFiles", "listDirectories"
|
|
|
|
|
|
|
|
|
Command string //"listFiles", "listDirectories", "getFileSize"
|
|
|
Directory string |
|
|
Directory string |
|
|
FileName string |
|
|
FileName string |
|
|
|
|
|
FileId string |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type ListFilesResult struct { |
|
|
type ListFilesResult struct { |
|
@ -21,16 +21,47 @@ type ListFilesResult struct { |
|
|
Error string `json:"error,omitempty"` |
|
|
Error string `json:"error,omitempty"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func ListFiles(server string, directory string, fileName string) (*ListFilesResult, error) { |
|
|
|
|
|
var ret ListFilesResult |
|
|
|
|
|
if err := call(server, ApiRequest{Command: "listFiles", Directory: directory, FileName: fileName}, &ret); err == nil { |
|
|
|
|
|
|
|
|
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 { |
|
|
if ret.Error != "" { |
|
|
if ret.Error != "" { |
|
|
return nil, errors.New(ret.Error) |
|
|
return nil, errors.New(ret.Error) |
|
|
} |
|
|
} |
|
|
return &ret, nil |
|
|
|
|
|
} else { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
|
return ret, nil |
|
|
} |
|
|
} |
|
|
|
|
|
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 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
type ListDirectoriesResult struct { |
|
|
type ListDirectoriesResult struct { |
|
@ -38,16 +69,23 @@ type ListDirectoriesResult struct { |
|
|
Error string `json:"error,omitempty"` |
|
|
Error string `json:"error,omitempty"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func ListDirectories(server string, directory string) (*ListDirectoriesResult, error) { |
|
|
|
|
|
var ret ListDirectoriesResult |
|
|
|
|
|
if err := call(server, ApiRequest{Command: "listDirectories", Directory: directory}, &ret); err == nil { |
|
|
|
|
|
|
|
|
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 { |
|
|
if ret.Error != "" { |
|
|
if ret.Error != "" { |
|
|
return nil, errors.New(ret.Error) |
|
|
return nil, errors.New(ret.Error) |
|
|
} |
|
|
} |
|
|
return &ret, nil |
|
|
|
|
|
} else { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
|
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" |
|
|
} |
|
|
} |
|
|
|
|
|
return util.Delete(destUrl, "") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func call(server string, request ApiRequest, ret interface{}) error { |
|
|
func call(server string, request ApiRequest, ret interface{}) error { |
|
|