github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/internal/pcsconfig/maniper.go (about) 1 package pcsconfig 2 3 import ( 4 "github.com/fzfile/BaiduPCS-Go/pcsutil/converter" 5 "github.com/fzfile/BaiduPCS-Go/requester" 6 "strings" 7 ) 8 9 const ( 10 opDelete = "delete" 11 opSwitch = "switch" 12 opGet = "get" 13 ) 14 15 func (c *PCSConfig) manipUser(op string, baiduBase *BaiduBase) (*Baidu, error) { 16 // empty baiduBase 17 if baiduBase == nil || (baiduBase.UID == 0 && baiduBase.Name == "") { 18 switch op { 19 case opGet: 20 return &Baidu{}, nil 21 default: 22 return nil, ErrBaiduUserNotFound 23 } 24 } 25 if len(c.BaiduUserList) == 0 { 26 return nil, ErrNoSuchBaiduUser 27 } 28 29 for k, user := range c.BaiduUserList { 30 if user == nil { 31 continue 32 } 33 34 switch { 35 case baiduBase.UID != 0 && baiduBase.Name != "": 36 // 不区分大小写 37 if user.UID == baiduBase.UID && strings.EqualFold(user.Name, baiduBase.Name) { 38 goto handle 39 } 40 continue 41 case baiduBase.UID == 0 && baiduBase.Name != "": 42 // 不区分大小写 43 if strings.EqualFold(user.Name, baiduBase.Name) { 44 goto handle 45 } 46 continue 47 case baiduBase.UID != 0 && baiduBase.Name == "": 48 if user.UID == baiduBase.UID { 49 goto handle 50 } 51 continue 52 default: 53 continue 54 } 55 // unreachable zone 56 57 handle: 58 switch op { 59 case opSwitch: 60 c.setupNewUser(user) 61 case opDelete: 62 c.BaiduUserList = append(c.BaiduUserList[:k], c.BaiduUserList[k+1:]...) 63 64 // 修改 正在使用的 百度帐号 65 // 如果要删除的帐号为当前登录的帐号, 则设置当前登录帐号为列表中第一个帐号 66 if c.BaiduActiveUID == user.UID { 67 if len(c.BaiduUserList) != 0 { 68 c.setupNewUser(c.BaiduUserList[0]) 69 } else { 70 c.BaiduActiveUID = 0 71 } 72 } 73 case opGet: 74 // do nothing 75 default: 76 // do nothing 77 } 78 return user, nil 79 } 80 81 return nil, ErrBaiduUserNotFound 82 } 83 84 //setupNewUser 从已有用户中, 设置新的当前登录用户 85 func (c *PCSConfig) setupNewUser(user *Baidu) { 86 if user == nil { 87 return 88 } 89 c.BaiduActiveUID = user.UID 90 c.activeUser = user 91 c.pcs = user.BaiduPCS() 92 } 93 94 // SwitchUser 切换用户, 返回切换成功的用户 95 func (c *PCSConfig) SwitchUser(baiduBase *BaiduBase) (*Baidu, error) { 96 return c.manipUser(opSwitch, baiduBase) 97 } 98 99 // DeleteUser 删除用户, 返回删除成功的用户 100 func (c *PCSConfig) DeleteUser(baiduBase *BaiduBase) (*Baidu, error) { 101 return c.manipUser(opDelete, baiduBase) 102 } 103 104 // GetBaiduUser 获取百度用户信息 105 func (c *PCSConfig) GetBaiduUser(baidubase *BaiduBase) (*Baidu, error) { 106 return c.manipUser(opGet, baidubase) 107 } 108 109 // CheckBaiduUserExist 检查百度用户是否存在于已登录列表 110 func (c *PCSConfig) CheckBaiduUserExist(baidubase *BaiduBase) bool { 111 _, err := c.manipUser("", baidubase) 112 return err == nil 113 } 114 115 // SetupUserByBDUSS 设置百度 bduss, ptoken, stoken 并保存 116 func (c *PCSConfig) SetupUserByBDUSS(bduss, ptoken, stoken string) (baidu *Baidu, err error) { 117 b, err := NewUserInfoByBDUSS(bduss) 118 if err != nil { 119 return nil, err 120 } 121 122 c.DeleteUser(&BaiduBase{ 123 UID: b.UID, 124 }) // 删除旧的信息 125 126 b.PTOKEN = ptoken 127 b.STOKEN = stoken 128 129 c.BaiduUserList = append(c.BaiduUserList, b) 130 131 // 自动切换用户 132 c.setupNewUser(b) 133 return b, nil 134 } 135 136 // SetAppID 设置app_id 137 func (c *PCSConfig) SetAppID(appID int) { 138 c.AppID = appID 139 if c.pcs != nil { 140 c.pcs.SetAPPID(appID) 141 } 142 } 143 144 // SetCacheSizeByStr 设置cache_size 145 func (c *PCSConfig) SetCacheSizeByStr(sizeStr string) error { 146 size, err := converter.ParseFileSizeStr(sizeStr) 147 if err != nil { 148 return err 149 } 150 c.CacheSize = int(size) 151 return nil 152 } 153 154 // SetMaxDownloadRateByStr 设置 max_download_rate 155 func (c *PCSConfig) SetMaxDownloadRateByStr(sizeStr string) error { 156 size, err := converter.ParseFileSizeStr(stripPerSecond(sizeStr)) 157 if err != nil { 158 return err 159 } 160 c.MaxDownloadRate = size 161 return nil 162 } 163 164 // SetMaxUploadRateByStr 设置 max_upload_rate 165 func (c *PCSConfig) SetMaxUploadRateByStr(sizeStr string) error { 166 size, err := converter.ParseFileSizeStr(stripPerSecond(sizeStr)) 167 if err != nil { 168 return err 169 } 170 c.MaxUploadRate = size 171 return nil 172 } 173 174 // SetUserAgent 设置User-Agent 175 func (c *PCSConfig) SetUserAgent(userAgent string) { 176 c.UserAgent = userAgent 177 requester.UserAgent = userAgent 178 } 179 180 // SetPCSUA 设置 PCS User-Agent 181 func (c *PCSConfig) SetPCSUA(pcsUA string) { 182 c.PCSUA = pcsUA 183 if c.pcs != nil { 184 c.pcs.SetPCSUserAgent(pcsUA) 185 } 186 } 187 188 // SetPanUA 设置 Pan User-Agent 189 func (c *PCSConfig) SetPanUA(panUA string) { 190 c.PanUA = panUA 191 if c.pcs != nil { 192 c.pcs.SetPanUserAgent(panUA) 193 } 194 } 195 196 // SetEnableHTTPS 设置是否启用https 197 func (c *PCSConfig) SetEnableHTTPS(https bool) { 198 c.EnableHTTPS = https 199 if c.pcs != nil { 200 c.pcs.SetHTTPS(https) 201 } 202 } 203 204 // SetProxy 设置代理 205 func (c *PCSConfig) SetProxy(proxy string) { 206 c.Proxy = proxy 207 requester.SetGlobalProxy(proxy) 208 } 209 210 // SetLocalAddrs 设置localAddrs 211 func (c *PCSConfig) SetLocalAddrs(localAddrs string) { 212 c.LocalAddrs = localAddrs 213 requester.SetLocalTCPAddrList(strings.Split(localAddrs, ",")...) 214 }