github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/internal/pcscommand/share.go (about)

     1  package pcscommand
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/fzfile/BaiduPCS-Go/baidupcs"
     6  	"github.com/fzfile/BaiduPCS-Go/pcstable"
     7  	"os"
     8  	"path"
     9  	"strconv"
    10  )
    11  
    12  // RunShareSet 执行分享
    13  func RunShareSet(paths []string, option *baidupcs.ShareOption) {
    14  	pcspaths, err := matchPathByShellPattern(paths...)
    15  	if err != nil {
    16  		fmt.Println(err)
    17  		return
    18  	}
    19  
    20  	shared, err := GetBaiduPCS().ShareSet(pcspaths, option)
    21  	if err != nil {
    22  		fmt.Printf("%s失败: %s\n", baidupcs.OperationShareSet, err)
    23  		return
    24  	}
    25  
    26  	fmt.Printf("shareID: %d, 链接: %s\n", shared.ShareID, shared.Link)
    27  }
    28  
    29  // RunShareCancel 执行取消分享
    30  func RunShareCancel(shareIDs []int64) {
    31  	if len(shareIDs) == 0 {
    32  		fmt.Printf("%s失败, 没有任何 shareid\n", baidupcs.OperationShareCancel)
    33  		return
    34  	}
    35  
    36  	err := GetBaiduPCS().ShareCancel(shareIDs)
    37  	if err != nil {
    38  		fmt.Printf("%s失败: %s\n", baidupcs.OperationShareCancel, err)
    39  		return
    40  	}
    41  
    42  	fmt.Printf("%s成功\n", baidupcs.OperationShareCancel)
    43  }
    44  
    45  // RunShareList 执行列出分享列表
    46  func RunShareList(page int) {
    47  	if page < 1 {
    48  		page = 1
    49  	}
    50  
    51  	pcs := GetBaiduPCS()
    52  	records, err := pcs.ShareList(page)
    53  	if err != nil {
    54  		fmt.Printf("%s失败: %s\n", baidupcs.OperationShareList, err)
    55  		return
    56  	}
    57  
    58  	tb := pcstable.NewTable(os.Stdout)
    59  	tb.SetHeader([]string{"#", "ShareID", "分享链接", "提取密码", "特征目录", "特征路径"})
    60  	for k, record := range records {
    61  		// 获取Passwd
    62  		if record.Public == 0 {
    63  			// 私密分享
    64  			info, pcsError := pcs.ShareSURLInfo(record.ShareID)
    65  			if pcsError != nil {
    66  				// 获取错误
    67  				fmt.Printf("[%d] 获取分享密码错误: %s\n", k, pcsError)
    68  			} else {
    69  				record.Passwd = info.Pwd
    70  			}
    71  		}
    72  
    73  		tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(record.ShareID, 10), record.Shortlink, record.Passwd, path.Clean(path.Dir(record.TypicalPath)), record.TypicalPath})
    74  	}
    75  	tb.Render()
    76  }