1 Star 0 Fork 1

freely随意/BaiduPCS-Go

forked from ttpc2008/BaiduPCS-Go 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
share.go 2.91 KB
一键复制 编辑 原始数据 按行查看 历史
konica 提交于 2018-08-04 22:27 . 更新错误处理, 增加搜索文件
package baidupcs
import (
"errors"
"github.com/iikira/BaiduPCS-Go/baidupcs/pcserror"
)
type (
// ShareOption 分享可选项
ShareOption struct {
Password string // 密码
Period int // 有效期
}
// Shared 分享信息
Shared struct {
Link string `json:"link"`
ShareID int64 `json:"shareid"`
}
// ShareRecordInfo 分享信息
ShareRecordInfo struct {
ShareID int64 `json:"shareId"`
FsIds []string `json:"fsIds"`
Passwd string `json:"passwd"`
Shortlink string `json:"shortlink"`
TypicalPath string `json:"typicalPath"`
}
sharePSetJSON struct {
*Shared
*pcserror.PanErrorInfo
}
shareListJSON struct {
List ShareRecordInfoList `json:"list"`
*pcserror.PanErrorInfo
}
)
// Clean 清理
func (sri *ShareRecordInfo) Clean() {
if sri.Passwd == "0" {
sri.Passwd = ""
}
}
// HasPasswd 是否需要提取码
func (sri *ShareRecordInfo) HasPasswd() bool {
return sri.Passwd != "" && sri.Passwd != "0"
}
// ShareRecordInfoList 分享信息列表
type ShareRecordInfoList []*ShareRecordInfo
// Clean 清理
func (sril *ShareRecordInfoList) Clean() {
for _, sri := range *sril {
if sri == nil {
continue
}
sri.Clean()
}
}
// ShareSet 分享文件
func (pcs *BaiduPCS) ShareSet(paths []string, option *ShareOption) (s *Shared, pcsError pcserror.Error) {
if option == nil {
option = &ShareOption{}
}
dataReadCloser, pcsError := pcs.PrepareSharePSet(paths, option.Period)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPanErrorInfo(OperationShareSet)
jsonData := sharePSetJSON{
Shared: &Shared{},
PanErrorInfo: errInfo,
}
pcsError = handleJSONParse(OperationShareSet, dataReadCloser, &jsonData)
if pcsError != nil {
return
}
if jsonData.Link == "" {
errInfo.ErrType = pcserror.ErrTypeOthers
errInfo.Err = errors.New("未找到分享链接")
return nil, errInfo
}
return jsonData.Shared, nil
}
// ShareCancel 取消分享
func (pcs *BaiduPCS) ShareCancel(shareIDs []int64) (pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareShareCancel(shareIDs)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
pcsError = pcserror.DecodePanJSONError(OperationShareCancel, dataReadCloser)
return
}
// ShareList 列出分享列表
func (pcs *BaiduPCS) ShareList(page int) (records ShareRecordInfoList, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareShareList(page)
if pcsError != nil {
return
}
defer dataReadCloser.Close()
errInfo := pcserror.NewPanErrorInfo(OperationShareList)
jsonData := shareListJSON{
List: records,
PanErrorInfo: errInfo,
}
pcsError = handleJSONParse(OperationShareList, dataReadCloser, &jsonData)
if pcsError != nil {
return
}
if jsonData.List == nil {
errInfo.ErrType = pcserror.ErrTypeOthers
errInfo.Err = errors.New("shared list is nil")
return nil, errInfo
}
jsonData.List.Clean()
return jsonData.List, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/freelysuiyi/BaiduPCS-Go.git
git@gitee.com:freelysuiyi/BaiduPCS-Go.git
freelysuiyi
BaiduPCS-Go
BaiduPCS-Go
v3.5.4

搜索帮助

0d507c66 1850385 C8b1a773 1850385