parent
fa3b51096a
commit
807aa5ac18
@ -0,0 +1,59 @@
|
|||||||
|
package onedrive
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
model "github.com/HFO4/cloudreve/models"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/request"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrAuthEndpoint 无法解析授权端点地址
|
||||||
|
ErrAuthEndpoint = errors.New("无法解析授权端点地址")
|
||||||
|
ErrInvalidRefreshToken = errors.New("上传策略无有效的RefreshToken")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client OneDrive客户端
|
||||||
|
type Client struct {
|
||||||
|
Endpoints *Endpoints
|
||||||
|
Policy *model.Policy
|
||||||
|
Credential *Credential
|
||||||
|
|
||||||
|
ClientID string
|
||||||
|
ClientSecret string
|
||||||
|
Redirect string
|
||||||
|
|
||||||
|
Request request.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoints OneDrive客户端相关设置
|
||||||
|
type Endpoints struct {
|
||||||
|
OAuthURL string // OAuth认证的基URL
|
||||||
|
OAuthEndpoints *oauthEndpoint
|
||||||
|
EndpointURL string // 接口请求的基URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient 根据存储策略获取新的client
|
||||||
|
func NewClient(policy *model.Policy) (*Client, error) {
|
||||||
|
client := &Client{
|
||||||
|
Endpoints: &Endpoints{
|
||||||
|
OAuthURL: policy.BaseURL,
|
||||||
|
EndpointURL: policy.Server,
|
||||||
|
},
|
||||||
|
Credential: &Credential{
|
||||||
|
RefreshToken: policy.AccessKey,
|
||||||
|
},
|
||||||
|
Policy: policy,
|
||||||
|
ClientID: policy.BucketName,
|
||||||
|
ClientSecret: policy.SecretKey,
|
||||||
|
Redirect: policy.OptionsSerialized.OdRedirect,
|
||||||
|
Request: request.HTTPClient{},
|
||||||
|
}
|
||||||
|
|
||||||
|
oauthBase := client.getOAuthEndpoint()
|
||||||
|
if oauthBase == nil {
|
||||||
|
return nil, ErrAuthEndpoint
|
||||||
|
}
|
||||||
|
client.Endpoints.OAuthEndpoints = oauthBase
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package onedrive
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
model "github.com/HFO4/cloudreve/models"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/filesystem/response"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||||
|
"io"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Driver OneDrive 适配器
|
||||||
|
type Driver struct {
|
||||||
|
Policy *model.Policy
|
||||||
|
Client *Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get 获取文件
|
||||||
|
func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser, error) {
|
||||||
|
return nil, errors.New("未实现")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put 将文件流保存到指定目录
|
||||||
|
func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
|
||||||
|
return errors.New("未实现")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete 删除一个或多个文件,
|
||||||
|
// 返回未删除的文件,及遇到的最后一个错误
|
||||||
|
func (handler Driver) Delete(ctx context.Context, files []string) ([]string, error) {
|
||||||
|
return []string{}, errors.New("未实现")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Thumb 获取文件缩略图
|
||||||
|
func (handler Driver) Thumb(ctx context.Context, path string) (*response.ContentResponse, error) {
|
||||||
|
return nil, errors.New("未实现")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Source 获取外链URL
|
||||||
|
func (handler Driver) Source(
|
||||||
|
ctx context.Context,
|
||||||
|
path string,
|
||||||
|
baseURL url.URL,
|
||||||
|
ttl int64,
|
||||||
|
isDownload bool,
|
||||||
|
speed int,
|
||||||
|
) (string, error) {
|
||||||
|
return "", errors.New("未实现")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token 获取上传策略和认证Token
|
||||||
|
func (handler Driver) Token(ctx context.Context, TTL int64, key string) (serializer.UploadCredential, error) {
|
||||||
|
err := handler.Client.UpdateCredential(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return serializer.UploadCredential{}, err
|
||||||
|
}
|
||||||
|
return serializer.UploadCredential{
|
||||||
|
Policy: handler.Client.Credential.AccessToken,
|
||||||
|
}, nil
|
||||||
|
//res,err := handler.Client.ObtainToken(ctx,WithCode("M2e92c4a9-de12-cdda-9cf4-e01f67272831"))
|
||||||
|
//if err != nil{
|
||||||
|
// return serializer.UploadCredential{},err
|
||||||
|
//}
|
||||||
|
//return serializer.UploadCredential{
|
||||||
|
// Policy:res.RefreshToken,
|
||||||
|
//}, nil
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package onedrive
|
||||||
|
|
||||||
|
// Option 发送请求的额外设置
|
||||||
|
type Option interface {
|
||||||
|
apply(*options)
|
||||||
|
}
|
||||||
|
|
||||||
|
type options struct {
|
||||||
|
redirect string
|
||||||
|
code string
|
||||||
|
refreshToken string
|
||||||
|
}
|
||||||
|
|
||||||
|
type optionFunc func(*options)
|
||||||
|
|
||||||
|
// WithCode 设置接口Code
|
||||||
|
func WithCode(t string) Option {
|
||||||
|
return optionFunc(func(o *options) {
|
||||||
|
o.code = t
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRefreshToken 设置接口RefreshToken
|
||||||
|
func WithRefreshToken(t string) Option {
|
||||||
|
return optionFunc(func(o *options) {
|
||||||
|
o.refreshToken = t
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f optionFunc) apply(o *options) {
|
||||||
|
f(o)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDefaultOption() *options {
|
||||||
|
return &options{}
|
||||||
|
}
|
Loading…
Reference in new issue