forked from IceWhaleTech/CasaOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added google drive and dropbox driver
- Loading branch information
Showing
38 changed files
with
2,005 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[Unit] | ||
Description=rclone | ||
|
||
[Service] | ||
ExecStartPre=/usr/bin/rm -f /tmp/rclone.sock | ||
ExecStart=/usr/bin/rclone rcd --rc-addr unix:///tmp/rclone.sock --rc-no-auth | ||
Restart=always | ||
RestartSec=10 | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package dropbox | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/IceWhaleTech/CasaOS-Common/utils/logger" | ||
"github.com/IceWhaleTech/CasaOS/internal/driver" | ||
"github.com/IceWhaleTech/CasaOS/model" | ||
"github.com/IceWhaleTech/CasaOS/pkg/utils" | ||
"github.com/go-resty/resty/v2" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Dropbox struct { | ||
model.Storage | ||
Addition | ||
AccessToken string | ||
} | ||
|
||
func (d *Dropbox) Config() driver.Config { | ||
return config | ||
} | ||
|
||
func (d *Dropbox) GetAddition() driver.Additional { | ||
return &d.Addition | ||
} | ||
|
||
func (d *Dropbox) Init(ctx context.Context) error { | ||
if len(d.RefreshToken) == 0 { | ||
d.getRefreshToken() | ||
} | ||
return d.refreshToken() | ||
} | ||
|
||
func (d *Dropbox) Drop(ctx context.Context) error { | ||
|
||
return nil | ||
} | ||
|
||
func (d *Dropbox) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { | ||
files, err := d.getFiles(dir.GetID()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return utils.SliceConvert(files, func(src File) (model.Obj, error) { | ||
return fileToObj(src), nil | ||
}) | ||
} | ||
|
||
func (d *Dropbox) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { | ||
url := "https://content.dropboxapi.com/2/files/download" | ||
link := model.Link{ | ||
URL: url, | ||
Method: http.MethodPost, | ||
Header: http.Header{ | ||
"Authorization": []string{"Bearer " + d.AccessToken}, | ||
"Dropbox-API-Arg": []string{`{"path": "` + file.GetPath() + `"}`}, | ||
}, | ||
} | ||
return &link, nil | ||
} | ||
func (d *Dropbox) GetUserInfo(ctx context.Context) (string, error) { | ||
url := "https://api.dropboxapi.com/2/users/get_current_account" | ||
user := UserInfo{} | ||
resp, err := d.request(url, http.MethodPost, func(req *resty.Request) { | ||
req.SetHeader("Content-Type", "") | ||
}, &user) | ||
if err != nil { | ||
return "", err | ||
} | ||
logger.Info("resp", zap.Any("resp", string(resp))) | ||
return user.Email, nil | ||
} | ||
func (d *Dropbox) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error { | ||
return nil | ||
} | ||
|
||
func (d *Dropbox) Move(ctx context.Context, srcObj, dstDir model.Obj) error { | ||
return nil | ||
} | ||
|
||
func (d *Dropbox) Rename(ctx context.Context, srcObj model.Obj, newName string) error { | ||
return nil | ||
} | ||
|
||
func (d *Dropbox) Copy(ctx context.Context, srcObj, dstDir model.Obj) error { | ||
return errors.New("not support") | ||
} | ||
|
||
func (d *Dropbox) Remove(ctx context.Context, obj model.Obj) error { | ||
return nil | ||
} | ||
|
||
func (d *Dropbox) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error { | ||
return nil | ||
} | ||
|
||
var _ driver.Driver = (*Dropbox)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dropbox | ||
|
||
import ( | ||
"github.com/IceWhaleTech/CasaOS/internal/driver" | ||
"github.com/IceWhaleTech/CasaOS/internal/op" | ||
) | ||
|
||
const ICONURL = "https://i.pcmag.com/imagery/reviews/02PHW91bUvLOs36qNbBzOiR-12.fit_scale.size_760x427.v1569471162.png" | ||
|
||
type Addition struct { | ||
driver.RootID | ||
RefreshToken string `json:"refresh_token" required:"true" omit:"true"` | ||
AppKey string `json:"app_key" type:"string" default:"onr2ic0c0m97mxr" omit:"true"` | ||
AppSecret string `json:"app_secret" type:"string" default:"nd3cjtikbxyj3pz" omit:"true"` | ||
OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" omit:"true"` | ||
AuthUrl string `json:"auth_url" type:"string" default:"https://www.dropbox.com/oauth2/authorize?client_id=onr2ic0c0m97mxr&redirect_uri=https://test-get.casaos.io&response_type=code&token_access_type=offline&state=${HOST}%2Fv1%2Frecover%2FDropbox"` | ||
Icon string `json:"icon" type:"string" default:"https://i.pcmag.com/imagery/reviews/02PHW91bUvLOs36qNbBzOiR-12.fit_scale.size_760x427.v1569471162.png"` | ||
Code string `json:"code" type:"string" help:"code from auth_url" omit:"true"` | ||
} | ||
|
||
var config = driver.Config{ | ||
Name: "Dropbox", | ||
OnlyProxy: true, | ||
DefaultRoot: "root", | ||
} | ||
|
||
func init() { | ||
op.RegisterDriver(func() driver.Driver { | ||
return &Dropbox{} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package dropbox | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/IceWhaleTech/CasaOS-Common/utils/logger" | ||
"github.com/IceWhaleTech/CasaOS/model" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type UserInfo struct { | ||
AccountID string `json:"account_id"` | ||
Name struct { | ||
GivenName string `json:"given_name"` | ||
Surname string `json:"surname"` | ||
FamiliarName string `json:"familiar_name"` | ||
DisplayName string `json:"display_name"` | ||
AbbreviatedName string `json:"abbreviated_name"` | ||
} `json:"name"` | ||
Email string `json:"email"` | ||
EmailVerified bool `json:"email_verified"` | ||
Disabled bool `json:"disabled"` | ||
Country string `json:"country"` | ||
Locale string `json:"locale"` | ||
ReferralLink string `json:"referral_link"` | ||
IsPaired bool `json:"is_paired"` | ||
AccountType struct { | ||
Tag string `json:".tag"` | ||
} `json:"account_type"` | ||
RootInfo struct { | ||
Tag string `json:".tag"` | ||
RootNamespaceID string `json:"root_namespace_id"` | ||
HomeNamespaceID string `json:"home_namespace_id"` | ||
} `json:"root_info"` | ||
} | ||
type TokenError struct { | ||
Error string `json:"error"` | ||
ErrorDescription string `json:"error_description"` | ||
} | ||
type File struct { | ||
Tag string `json:".tag"` | ||
Name string `json:"name"` | ||
PathLower string `json:"path_lower"` | ||
PathDisplay string `json:"path_display"` | ||
ID string `json:"id"` | ||
ClientModified time.Time `json:"client_modified,omitempty"` | ||
ServerModified time.Time `json:"server_modified,omitempty"` | ||
Rev string `json:"rev,omitempty"` | ||
Size int `json:"size,omitempty"` | ||
IsDownloadable bool `json:"is_downloadable,omitempty"` | ||
ContentHash string `json:"content_hash,omitempty"` | ||
} | ||
|
||
type Files struct { | ||
Files []File `json:"entries"` | ||
Cursor string `json:"cursor"` | ||
HasMore bool `json:"has_more"` | ||
} | ||
|
||
type Error struct { | ||
Error struct { | ||
Errors []struct { | ||
Domain string `json:"domain"` | ||
Reason string `json:"reason"` | ||
Message string `json:"message"` | ||
LocationType string `json:"location_type"` | ||
Location string `json:"location"` | ||
} | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} `json:"error"` | ||
} | ||
|
||
func fileToObj(f File) *model.ObjThumb { | ||
logger.Info("dropbox file", zap.Any("file", f)) | ||
obj := &model.ObjThumb{ | ||
Object: model.Object{ | ||
ID: f.ID, | ||
Name: f.Name, | ||
Size: int64(f.Size), | ||
Modified: f.ClientModified, | ||
IsFolder: f.Tag == "folder", | ||
Path: f.PathDisplay, | ||
}, | ||
Thumbnail: model.Thumbnail{}, | ||
} | ||
return obj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package dropbox | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/IceWhaleTech/CasaOS-Common/utils/logger" | ||
"github.com/IceWhaleTech/CasaOS/drivers/base" | ||
"github.com/go-resty/resty/v2" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (d *Dropbox) getRefreshToken() error { | ||
url := "https://api.dropbox.com/oauth2/token" | ||
var resp base.TokenResp | ||
var e TokenError | ||
|
||
res, err := base.RestyClient.R().SetResult(&resp).SetError(&e). | ||
SetFormData(map[string]string{ | ||
"code": d.Code, | ||
"grant_type": "authorization_code", | ||
"redirect_uri": "https://test-get.casaos.io", | ||
}).SetBasicAuth(d.Addition.AppKey, d.Addition.AppSecret).SetHeader("Content-Type", "application/x-www-form-urlencoded").Post(url) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Info("get refresh token", zap.String("res", res.String())) | ||
if e.Error != "" { | ||
return fmt.Errorf(e.Error) | ||
} | ||
d.RefreshToken = resp.RefreshToken | ||
return nil | ||
|
||
} | ||
func (d *Dropbox) refreshToken() error { | ||
url := "https://api.dropbox.com/oauth2/token" | ||
var resp base.TokenResp | ||
var e TokenError | ||
|
||
res, err := base.RestyClient.R().SetResult(&resp).SetError(&e). | ||
SetFormData(map[string]string{ | ||
"refresh_token": d.RefreshToken, | ||
"grant_type": "refresh_token", | ||
}).SetBasicAuth(d.Addition.AppKey, d.Addition.AppSecret).SetHeader("Content-Type", "application/x-www-form-urlencoded").Post(url) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Info("get refresh token", zap.String("res", res.String())) | ||
if e.Error != "" { | ||
return fmt.Errorf(e.Error) | ||
} | ||
d.AccessToken = resp.AccessToken | ||
return nil | ||
|
||
} | ||
func (d *Dropbox) request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) { | ||
req := base.RestyClient.R() | ||
req.SetHeader("Authorization", "Bearer "+d.AccessToken) | ||
req.SetHeader("Content-Type", "application/json") | ||
if callback != nil { | ||
callback(req) | ||
} | ||
if resp != nil { | ||
req.SetResult(resp) | ||
} | ||
var e Error | ||
req.SetError(&e) | ||
res, err := req.Execute(method, url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if e.Error.Code != 0 { | ||
if e.Error.Code == 401 { | ||
err = d.refreshToken() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return d.request(url, method, callback, resp) | ||
} | ||
return nil, fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors) | ||
} | ||
return res.Body(), nil | ||
} | ||
func (d *Dropbox) getFiles(path string) ([]File, error) { | ||
|
||
res := make([]File, 0) | ||
var resp Files | ||
body := base.Json{ | ||
"limit": 2000, | ||
"path": path, | ||
} | ||
|
||
_, err := d.request("https://api.dropboxapi.com/2/files/list_folder", http.MethodPost, func(req *resty.Request) { | ||
req.SetBody(body) | ||
}, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res = append(res, resp.Files...) | ||
|
||
return res, nil | ||
} |
Oops, something went wrong.