github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/baidupcs/share.go (about) 1 package baidupcs 2 3 import ( 4 "errors" 5 "github.com/fzfile/BaiduPCS-Go/baidupcs/pcserror" 6 "strings" 7 ) 8 9 type ( 10 // ShareOption 分享可选项 11 ShareOption struct { 12 Password string // 密码 13 Period int // 有效期 14 } 15 16 // Shared 分享信息 17 Shared struct { 18 Link string `json:"link"` 19 ShareID int64 `json:"shareid"` 20 } 21 22 // ShareRecordInfo 分享信息 23 ShareRecordInfo struct { 24 ShareID int64 `json:"shareId"` 25 FsIds []int64 `json:"fsIds"` 26 Passwd string `json:"-"` // 这个字段已经没有用了, 需要从ShareSURLInfo中获取 27 Shortlink string `json:"shortlink"` 28 Status int `json:"status"` // 状态 29 Public int `json:"public"` // 是否为公开分享 30 TypicalCategory int `json:"typicalCategory"` // 文件类型 31 TypicalPath string `json:"typicalPath"` 32 } 33 34 shareSURLInfo struct { 35 *pcserror.PanErrorInfo 36 *ShareSURLInfo 37 } 38 39 // ShareSURLInfo 分享的子信息 40 ShareSURLInfo struct { 41 Pwd string `json:"pwd"` // 新密码 42 ShortURL string `json:"shorturl"` 43 } 44 45 // ShareRecordInfoList 分享信息列表 46 ShareRecordInfoList []*ShareRecordInfo 47 48 sharePSetJSON struct { 49 *Shared 50 *pcserror.PanErrorInfo 51 } 52 53 shareListJSON struct { 54 List ShareRecordInfoList `json:"list"` 55 *pcserror.PanErrorInfo 56 } 57 ) 58 59 var ( 60 // ErrShareLinkNotFound 未找到分享链接 61 ErrShareLinkNotFound = errors.New("未找到分享链接") 62 ) 63 64 // ShareSet 分享文件 65 func (pcs *BaiduPCS) ShareSet(paths []string, option *ShareOption) (s *Shared, pcsError pcserror.Error) { 66 if option == nil { 67 option = &ShareOption{} 68 } 69 70 dataReadCloser, pcsError := pcs.PrepareSharePSet(paths, option.Period) 71 if pcsError != nil { 72 return 73 } 74 75 defer dataReadCloser.Close() 76 77 errInfo := pcserror.NewPanErrorInfo(OperationShareSet) 78 jsonData := sharePSetJSON{ 79 Shared: &Shared{}, 80 PanErrorInfo: errInfo, 81 } 82 83 pcsError = pcserror.HandleJSONParse(OperationShareSet, dataReadCloser, &jsonData) 84 if pcsError != nil { 85 return 86 } 87 88 if jsonData.Link == "" { 89 errInfo.ErrType = pcserror.ErrTypeOthers 90 errInfo.Err = ErrShareLinkNotFound 91 return nil, errInfo 92 } 93 94 return jsonData.Shared, nil 95 } 96 97 // ShareCancel 取消分享 98 func (pcs *BaiduPCS) ShareCancel(shareIDs []int64) (pcsError pcserror.Error) { 99 dataReadCloser, pcsError := pcs.PrepareShareCancel(shareIDs) 100 if pcsError != nil { 101 return 102 } 103 104 defer dataReadCloser.Close() 105 106 pcsError = pcserror.DecodePanJSONError(OperationShareCancel, dataReadCloser) 107 return 108 } 109 110 // ShareList 列出分享列表 111 func (pcs *BaiduPCS) ShareList(page int) (records ShareRecordInfoList, pcsError pcserror.Error) { 112 dataReadCloser, pcsError := pcs.PrepareShareList(page) 113 if pcsError != nil { 114 return 115 } 116 117 defer dataReadCloser.Close() 118 119 errInfo := pcserror.NewPanErrorInfo(OperationShareList) 120 jsonData := shareListJSON{ 121 List: records, 122 PanErrorInfo: errInfo, 123 } 124 125 pcsError = pcserror.HandleJSONParse(OperationShareList, dataReadCloser, &jsonData) 126 if pcsError != nil { 127 // json解析错误 128 if pcsError.GetErrType() == pcserror.ErrTypeJSONParseError { 129 // 服务器更改, List为空时变成{}, 导致解析错误 130 if strings.Contains(pcsError.GetError().Error(), `"list":{}`) { 131 // 返回空列表 132 return jsonData.List, nil 133 } 134 } 135 return 136 } 137 138 if jsonData.List == nil { 139 errInfo.ErrType = pcserror.ErrTypeOthers 140 errInfo.Err = errors.New("shared list is nil") 141 return nil, errInfo 142 } 143 144 return jsonData.List, nil 145 } 146 147 //ShareSURLInfo 获取分享的详细信息, 包含密码 148 func (pcs *BaiduPCS) ShareSURLInfo(shareID int64) (info *ShareSURLInfo, pcsError pcserror.Error) { 149 dataReadCloser, pcsError := pcs.PrepareShareSURLInfo(shareID) 150 if pcsError != nil { 151 return 152 } 153 154 defer dataReadCloser.Close() 155 156 errInfo := pcserror.NewPanErrorInfo(OperationShareSURLInfo) 157 158 jsonData := shareSURLInfo{ 159 PanErrorInfo: errInfo, 160 } 161 162 pcsError = pcserror.HandleJSONParse(OperationShareList, dataReadCloser, &jsonData) 163 if pcsError != nil { 164 // json解析错误 165 return 166 } 167 168 // 去掉0 169 if jsonData.Pwd == "0" { 170 jsonData.Pwd = "" 171 } 172 173 return jsonData.ShareSURLInfo, nil 174 }