|
| 1 | +package _189 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha1" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 12 | + "github.com/OpenListTeam/OpenList/v4/pkg/torrent" |
| 13 | + "github.com/OpenListTeam/OpenList/v4/pkg/utils" |
| 14 | +) |
| 15 | + |
| 16 | +// GenerateTorrent 根据上传过程中收集的哈希信息生成包含 CAS 扩展的 torrent 文件 |
| 17 | +func GenerateTorrent(fileName string, fileSize int64, fileMD5 string, sliceMD5s []string, sliceSize int64, pieceHashes []byte) ([]byte, error) { |
| 18 | + // 计算 sliceMD5 |
| 19 | + sliceMD5 := fileMD5 |
| 20 | + if len(sliceMD5s) > 1 { |
| 21 | + joined := strings.Join(sliceMD5s, "\n") |
| 22 | + sliceMD5 = strings.ToUpper(torrent.GetMD5Str(joined)) |
| 23 | + } |
| 24 | + |
| 25 | + t := torrent.NewTorrent(fileName, fileSize, fileMD5) |
| 26 | + t.Info.PieceLength = sliceSize |
| 27 | + t.SetPieces(pieceHashes) |
| 28 | + t.SetCASInfo(&torrent.CASInfo{ |
| 29 | + FileMD5: fileMD5, |
| 30 | + SliceMD5: sliceMD5, |
| 31 | + SliceMD5s: sliceMD5s, |
| 32 | + SliceSize: sliceSize, |
| 33 | + Cloud: "189", |
| 34 | + }) |
| 35 | + |
| 36 | + return t.Encode() |
| 37 | +} |
| 38 | + |
| 39 | +// RapidUploadFromTorrent 从 torrent 文件中提取 CAS 信息进行秒传 |
| 40 | +func (d *Cloud189) RapidUploadFromTorrent(ctx context.Context, dstDir model.Obj, torrentData []byte) error { |
| 41 | + // 解析 torrent |
| 42 | + t, err := torrent.Decode(torrentData) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("解析 torrent 失败: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + // 检查是否包含 CAS 扩展信息 |
| 48 | + if !t.HasCASInfo() { |
| 49 | + return fmt.Errorf("torrent 不包含 CAS 扩展信息,无法秒传") |
| 50 | + } |
| 51 | + |
| 52 | + cas := t.CAS |
| 53 | + fileName := t.Info.Name |
| 54 | + fileSize := t.GetTotalSize() |
| 55 | + |
| 56 | + // 获取 sessionKey |
| 57 | + sessionKey, err := d.getSessionKey() |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + d.sessionKey = sessionKey |
| 62 | + |
| 63 | + // 初始化上传 |
| 64 | + res, err := d.uploadRequest("/person/initMultiUpload", map[string]string{ |
| 65 | + "parentFolderId": dstDir.GetID(), |
| 66 | + "fileName": encode(fileName), |
| 67 | + "fileSize": fmt.Sprint(fileSize), |
| 68 | + "sliceSize": fmt.Sprint(cas.SliceSize), |
| 69 | + "lazyCheck": "1", |
| 70 | + }, nil) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("初始化上传失败: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + uploadFileId := utils.Json.Get(res, "data", "uploadFileId").ToString() |
| 76 | + |
| 77 | + // 提交上传(使用 CAS 信息秒传) |
| 78 | + _, err = d.uploadRequest("/person/commitMultiUploadFile", map[string]string{ |
| 79 | + "uploadFileId": uploadFileId, |
| 80 | + "fileMd5": cas.FileMD5, |
| 81 | + "sliceMd5": cas.SliceMD5, |
| 82 | + "lazyCheck": "1", |
| 83 | + "opertype": "3", |
| 84 | + }, nil) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("秒传提交失败: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// ComputeTorrentFromReader 从 io.Reader 计算并生成 torrent 文件 |
| 93 | +func ComputeTorrentFromReader(reader io.Reader, fileName string, fileSize int64, sliceSize int64) ([]byte, error) { |
| 94 | + if sliceSize <= 0 { |
| 95 | + sliceSize = torrent.DefaultPieceSize |
| 96 | + } |
| 97 | + |
| 98 | + hw := torrent.NewHashWriter(sliceSize, sliceSize) |
| 99 | + |
| 100 | + buf := make([]byte, 32*1024) |
| 101 | + for { |
| 102 | + n, err := reader.Read(buf) |
| 103 | + if n > 0 { |
| 104 | + hw.Write(buf[:n]) |
| 105 | + } |
| 106 | + if err == io.EOF { |
| 107 | + break |
| 108 | + } |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + } |
| 113 | + hw.Finish() |
| 114 | + |
| 115 | + fileMD5 := hw.GetFileMD5() |
| 116 | + sliceMD5s := hw.GetSliceMD5s() |
| 117 | + pieceHashes := hw.GetPieceHashes() |
| 118 | + |
| 119 | + return GenerateTorrent(fileName, fileSize, fileMD5, sliceMD5s, sliceSize, pieceHashes) |
| 120 | +} |
| 121 | + |
| 122 | +// ComputePieceSHA1 计算单个分片的 SHA-1 哈希 |
| 123 | +func ComputePieceSHA1(data []byte) []byte { |
| 124 | + h := sha1.Sum(data) |
| 125 | + return h[:] |
| 126 | +} |
| 127 | + |
| 128 | +// ExtractCASFromTorrent 从 torrent 数据中提取 CAS 信息 |
| 129 | +func ExtractCASFromTorrent(torrentData []byte) (*torrent.CASInfo, string, int64, error) { |
| 130 | + t, err := torrent.Decode(torrentData) |
| 131 | + if err != nil { |
| 132 | + return nil, "", 0, fmt.Errorf("解析 torrent 失败: %w", err) |
| 133 | + } |
| 134 | + |
| 135 | + if !t.HasCASInfo() { |
| 136 | + return nil, "", 0, fmt.Errorf("torrent 不包含 CAS 扩展信息") |
| 137 | + } |
| 138 | + |
| 139 | + return t.CAS, t.Info.Name, t.GetTotalSize(), nil |
| 140 | +} |
| 141 | + |
| 142 | +// GetInfoHashHex 获取 torrent 的 info_hash(十六进制字符串) |
| 143 | +func GetInfoHashHex(torrentData []byte) (string, error) { |
| 144 | + t, err := torrent.Decode(torrentData) |
| 145 | + if err != nil { |
| 146 | + return "", err |
| 147 | + } |
| 148 | + return hex.EncodeToString(t.InfoHash), nil |
| 149 | +} |
0 commit comments