Skip to content

Commit

Permalink
The same driver and the same account can only be hooked up once (IceW…
Browse files Browse the repository at this point in the history
…haleTech#878)

Signed-off-by: link <[email protected]>
  • Loading branch information
LinkLeong authored Feb 9, 2023
1 parent fbfcc2c commit 6217009
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
58 changes: 56 additions & 2 deletions route/v1/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"strings"
"time"

"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS/drivers/dropbox"
"github.com/IceWhaleTech/CasaOS/drivers/google_drive"
fileutil "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
"github.com/IceWhaleTech/CasaOS/service"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

func GetRecoverStorage(c *gin.Context) {
Expand Down Expand Up @@ -51,17 +53,42 @@ func GetRecoverStorage(c *gin.Context) {
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
}
dmap := make(map[string]string)
dmap["username"] = username
configs, err := service.MyService.Storage().GetConfig()
if err != nil {
c.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
notify["status"] = "fail"
notify["message"] = "Failed to get user information"
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
}
for _, v := range configs.Remotes {
cf, err := service.MyService.Storage().GetConfigByName(v)
if err != nil {
logger.Error("then get config by name error: ", zap.Error(err), zap.Any("name", v))
continue
}
if cf["type"] == "drive" && cf["username"] == dmap["username"] {
c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
service.MyService.Storage().CheckAndMountByName(cf["username"])
notify["status"] = "warn"
notify["message"] = "The same configuration has been added"
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
}
}
if len(username) > 0 {
a := strings.Split(username, "@")
username = a[0]
}
username = fileutil.NameAccumulation(username, "/mnt")

dataMap, _ := service.MyService.Storage().GetConfigByName(username)
if len(dataMap) > 0 {
service.MyService.Storage().UnmountStorage("/mnt/" + username)
service.MyService.Storage().DeleteConfigByName(username)
}
dmap := make(map[string]string)
dmap["client_id"] = add.ClientID
dmap["client_secret"] = add.ClientSecret
dmap["scope"] = "drive"
Expand Down Expand Up @@ -104,17 +131,44 @@ func GetRecoverStorage(c *gin.Context) {
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
}
dmap := make(map[string]string)
dmap["username"] = username

configs, err := service.MyService.Storage().GetConfig()
if err != nil {
c.String(200, `<p>Failed to get user information:`+err.Error()+`</p><script>window.close()</script>`)
notify["status"] = "fail"
notify["message"] = "Failed to get user information"
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
}
for _, v := range configs.Remotes {
cf, err := service.MyService.Storage().GetConfigByName(v)
if err != nil {
logger.Error("then get config by name error: ", zap.Error(err), zap.Any("name", v))
continue
}
if cf["type"] == "dropbox" && cf["username"] == dmap["username"] {
c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
service.MyService.Storage().CheckAndMountByName(cf["username"])
notify["status"] = "warn"
notify["message"] = "The same configuration has been added"
service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return
}
}
if len(username) > 0 {
a := strings.Split(username, "@")
username = a[0]
}
username = fileutil.NameAccumulation(username, "/mnt")
dataMap, _ := service.MyService.Storage().GetConfigByName(username)
if len(dataMap) > 0 {

service.MyService.Storage().UnmountStorage("/mnt/" + username)
service.MyService.Storage().DeleteConfigByName(username)
}
dmap := make(map[string]string)

dmap["client_id"] = add.AppKey
dmap["client_secret"] = add.AppSecret
dmap["token"] = `{"access_token":"` + dropbox.AccessToken + `","token_type":"bearer","refresh_token":"` + dropbox.Addition.RefreshToken + `","expiry":"` + currentDate + `T` + currentTime.Add(time.Hour*3).Add(time.Minute*50).Format("15:04:05") + `.780385354Z"}`
Expand Down
8 changes: 8 additions & 0 deletions service/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type StorageService interface {
CheckAndMountAll() error
GetConfigByName(name string) (map[string]string, error)
DeleteConfigByName(name string) error
GetConfig() (httper.RemotesResult, error)
}

type storageStruct struct {
Expand Down Expand Up @@ -102,6 +103,13 @@ func (s *storageStruct) GetConfigByName(name string) (map[string]string, error)
func (s *storageStruct) DeleteConfigByName(name string) error {
return httper.DeleteConfigByName(name)
}
func (s *storageStruct) GetConfig() (httper.RemotesResult, error) {
section, err := httper.GetAllConfigName()
if err != nil {
return httper.RemotesResult{}, err
}
return section, nil
}
func NewStorageService() StorageService {
return &storageStruct{}
}

0 comments on commit 6217009

Please sign in to comment.