The same driver and the same account can only be hooked up once (#878)

Signed-off-by: link <a624669980@163.com>
This commit is contained in:
link 2023-02-09 17:23:33 +08:00 committed by GitHub
parent fbfcc2c43a
commit 6217009caf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View file

@ -4,11 +4,13 @@ import (
"strings" "strings"
"time" "time"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS/drivers/dropbox" "github.com/IceWhaleTech/CasaOS/drivers/dropbox"
"github.com/IceWhaleTech/CasaOS/drivers/google_drive" "github.com/IceWhaleTech/CasaOS/drivers/google_drive"
fileutil "github.com/IceWhaleTech/CasaOS/pkg/utils/file" fileutil "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
"github.com/IceWhaleTech/CasaOS/service" "github.com/IceWhaleTech/CasaOS/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap"
) )
func GetRecoverStorage(c *gin.Context) { func GetRecoverStorage(c *gin.Context) {
@ -51,17 +53,42 @@ func GetRecoverStorage(c *gin.Context) {
service.MyService.Notify().SendNotify("casaos:file:recover", notify) service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return 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 { if len(username) > 0 {
a := strings.Split(username, "@") a := strings.Split(username, "@")
username = a[0] username = a[0]
} }
username = fileutil.NameAccumulation(username, "/mnt") username = fileutil.NameAccumulation(username, "/mnt")
dataMap, _ := service.MyService.Storage().GetConfigByName(username) dataMap, _ := service.MyService.Storage().GetConfigByName(username)
if len(dataMap) > 0 { if len(dataMap) > 0 {
service.MyService.Storage().UnmountStorage("/mnt/" + username) service.MyService.Storage().UnmountStorage("/mnt/" + username)
service.MyService.Storage().DeleteConfigByName(username) service.MyService.Storage().DeleteConfigByName(username)
} }
dmap := make(map[string]string)
dmap["client_id"] = add.ClientID dmap["client_id"] = add.ClientID
dmap["client_secret"] = add.ClientSecret dmap["client_secret"] = add.ClientSecret
dmap["scope"] = "drive" dmap["scope"] = "drive"
@ -104,6 +131,32 @@ func GetRecoverStorage(c *gin.Context) {
service.MyService.Notify().SendNotify("casaos:file:recover", notify) service.MyService.Notify().SendNotify("casaos:file:recover", notify)
return 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 { if len(username) > 0 {
a := strings.Split(username, "@") a := strings.Split(username, "@")
username = a[0] username = a[0]
@ -111,10 +164,11 @@ func GetRecoverStorage(c *gin.Context) {
username = fileutil.NameAccumulation(username, "/mnt") username = fileutil.NameAccumulation(username, "/mnt")
dataMap, _ := service.MyService.Storage().GetConfigByName(username) dataMap, _ := service.MyService.Storage().GetConfigByName(username)
if len(dataMap) > 0 { if len(dataMap) > 0 {
service.MyService.Storage().UnmountStorage("/mnt/" + username) service.MyService.Storage().UnmountStorage("/mnt/" + username)
service.MyService.Storage().DeleteConfigByName(username) service.MyService.Storage().DeleteConfigByName(username)
} }
dmap := make(map[string]string)
dmap["client_id"] = add.AppKey dmap["client_id"] = add.AppKey
dmap["client_secret"] = add.AppSecret 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"}` 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"}`

View file

@ -18,6 +18,7 @@ type StorageService interface {
CheckAndMountAll() error CheckAndMountAll() error
GetConfigByName(name string) (map[string]string, error) GetConfigByName(name string) (map[string]string, error)
DeleteConfigByName(name string) error DeleteConfigByName(name string) error
GetConfig() (httper.RemotesResult, error)
} }
type storageStruct struct { type storageStruct struct {
@ -102,6 +103,13 @@ func (s *storageStruct) GetConfigByName(name string) (map[string]string, error)
func (s *storageStruct) DeleteConfigByName(name string) error { func (s *storageStruct) DeleteConfigByName(name string) error {
return httper.DeleteConfigByName(name) 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 { func NewStorageService() StorageService {
return &storageStruct{} return &storageStruct{}
} }