92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
package baidu
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"time"
|
|
)
|
|
|
|
type APIError struct {
|
|
Errno int `json:"errno"`
|
|
ErrorCode int `json:"error_code"`
|
|
ErrorMsg string `json:"error_msg"`
|
|
Errmsg string `json:"errmsg"`
|
|
RequestID any `json:"request_id"`
|
|
}
|
|
|
|
func (e *APIError) Code() int {
|
|
if e.Errno != 0 {
|
|
return e.Errno
|
|
}
|
|
return e.ErrorCode
|
|
}
|
|
|
|
func (e *APIError) Error() string {
|
|
msg := e.ErrorMsg
|
|
if msg == "" {
|
|
msg = e.Errmsg
|
|
}
|
|
if msg == "" {
|
|
msg = "Baidu API request failed"
|
|
}
|
|
return fmt.Sprintf("%s (code=%d, request_id=%v)", msg, e.Code(), e.RequestID)
|
|
}
|
|
|
|
type File struct {
|
|
FSID int64 `json:"fs_id"`
|
|
Category int `json:"category"`
|
|
Size int64 `json:"size"`
|
|
Path string `json:"path"`
|
|
ServerFilename string `json:"server_filename"`
|
|
MD5 string `json:"md5"`
|
|
IsDir int `json:"isdir"`
|
|
ServerCTime int64 `json:"server_ctime"`
|
|
ServerMTime int64 `json:"server_mtime"`
|
|
LocalCTime int64 `json:"local_ctime"`
|
|
LocalMTime int64 `json:"local_mtime"`
|
|
CTime int64 `json:"ctime"`
|
|
MTime int64 `json:"mtime"`
|
|
DLink string `json:"dlink,omitempty"`
|
|
}
|
|
|
|
func (f File) Name() string {
|
|
if f.ServerFilename != "" {
|
|
return f.ServerFilename
|
|
}
|
|
return path.Base(f.Path)
|
|
}
|
|
|
|
func (f File) IsDirectory() bool { return f.IsDir == 1 }
|
|
|
|
func (f File) ModTime() time.Time {
|
|
stamp := f.LocalMTime
|
|
if stamp == 0 {
|
|
stamp = f.ServerMTime
|
|
}
|
|
if stamp == 0 {
|
|
stamp = f.MTime
|
|
}
|
|
return time.Unix(stamp, 0)
|
|
}
|
|
|
|
type Quota struct {
|
|
Total int64 `json:"total"`
|
|
Used int64 `json:"used"`
|
|
}
|
|
|
|
type Token struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
Scope string `json:"scope"`
|
|
}
|
|
|
|
type OAuthError struct {
|
|
ErrorCode string `json:"error"`
|
|
ErrorDescription string `json:"error_description"`
|
|
}
|
|
|
|
func (e *OAuthError) Error() string {
|
|
return fmt.Sprintf("OAuth error %s: %s", e.ErrorCode, e.ErrorDescription)
|
|
}
|