github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/internal/pcscommand/share.go (about)

     1  package pcscommand
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/qjfoidnh/BaiduPCS-Go/baidupcs"
    12  	"github.com/qjfoidnh/BaiduPCS-Go/pcstable"
    13  )
    14  
    15  // RunShareSet 执行分享
    16  func RunShareSet(paths []string, option *baidupcs.ShareOption) {
    17  	pcspaths, err := matchPathByShellPattern(paths...)
    18  	if err != nil {
    19  		fmt.Println(err)
    20  		return
    21  	}
    22  
    23  	shared, err := GetBaiduPCS().ShareSet(pcspaths, option)
    24  	if err != nil {
    25  		fmt.Printf("%s失败: %s\n", baidupcs.OperationShareSet, err)
    26  		return
    27  	}
    28  	if option.IsCombined {
    29  		fmt.Printf("shareID: %d, 链接: %s?pwd=%s\n", shared.ShareID, shared.Link, shared.Pwd)
    30  	} else {
    31  		fmt.Printf("shareID: %d, 链接: %s, 密码: %s\n", shared.ShareID, shared.Link, shared.Pwd)
    32  	}
    33  }
    34  
    35  // RunShareCancel 执行取消分享
    36  func RunShareCancel(shareIDs []int64) {
    37  	if len(shareIDs) == 0 {
    38  		fmt.Printf("%s失败, 没有任何 shareid\n", baidupcs.OperationShareCancel)
    39  		return
    40  	}
    41  
    42  	err := GetBaiduPCS().ShareCancel(shareIDs)
    43  	if err != nil {
    44  		fmt.Printf("%s失败: %s\n", baidupcs.OperationShareCancel, err)
    45  		return
    46  	}
    47  
    48  	fmt.Printf("%s成功\n", baidupcs.OperationShareCancel)
    49  }
    50  
    51  // RunShareList 执行列出分享列表
    52  func RunShareList(page int) {
    53  	if page < 1 {
    54  		page = 1
    55  	}
    56  
    57  	pcs := GetBaiduPCS()
    58  	records, err := pcs.ShareList(page)
    59  	if err != nil {
    60  		fmt.Printf("%s失败: %s\n", baidupcs.OperationShareList, err)
    61  		return
    62  	}
    63  
    64  	tb := pcstable.NewTable(os.Stdout)
    65  	tb.SetHeader([]string{"#", "ShareID", "分享链接", "提取密码", "特征目录", "特征路径", "过期时间", "浏览次数"})
    66  	for k, record := range records {
    67  		if record.ExpireType == -1 {
    68  			record.Valid = "已过期" // 已失效分享
    69  		} else {
    70  			if record.ExpireTime == 0 {
    71  				record.Valid = "永久"
    72  			} else {
    73  				tm := time.Unix(time.Now().Unix()+record.ExpireTime, 0)
    74  				record.Valid = tm.Format("2006/01/02 15:04:05")
    75  
    76  			}
    77  
    78  		}
    79  		// 获取Passwd
    80  		if record.Public == 0 && record.ExpireType != -1 {
    81  			// 私密分享
    82  			info, pcsError := pcs.ShareSURLInfo(record.ShareID)
    83  			if pcsError != nil {
    84  				// 获取错误
    85  				fmt.Printf("[%d] 获取分享密码错误: %s\n", k, pcsError)
    86  			} else {
    87  				record.Passwd = strings.TrimSpace(info.Pwd)
    88  			}
    89  		}
    90  
    91  		tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(record.ShareID, 10), record.Shortlink, record.Passwd, path.Clean(path.Dir(record.TypicalPath)), record.TypicalPath, record.Valid, strconv.Itoa(record.ViewCount)})
    92  	}
    93  	tb.Render()
    94  }