|
| 1 | +package guangyapan |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + stdpath "path" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/alist-org/alist/v3/internal/model" |
| 12 | +) |
| 13 | + |
| 14 | +func (d *GuangYaPan) ResolveOfflineResource(ctx context.Context, fileURL string) (*OfflineResolveData, error) { |
| 15 | + if err := d.ensureAccessToken(ctx); err != nil { |
| 16 | + return nil, err |
| 17 | + } |
| 18 | + fileURL = strings.TrimSpace(fileURL) |
| 19 | + if fileURL == "" { |
| 20 | + return nil, errors.New("offline url is empty") |
| 21 | + } |
| 22 | + |
| 23 | + var resp offlineResolveResp |
| 24 | + if err := d.postAPI(ctx, "/cloudcollection/v1/resolve_res", map[string]any{ |
| 25 | + "url": fileURL, |
| 26 | + }, &resp); err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + if !isSuccessMsg(resp.Msg) { |
| 30 | + return nil, fmt.Errorf("resolve offline resource failed: %s", strings.TrimSpace(resp.Msg)) |
| 31 | + } |
| 32 | + return &resp.Data, nil |
| 33 | +} |
| 34 | + |
| 35 | +func (d *GuangYaPan) OfflineDownload(ctx context.Context, fileURL string, parentDir model.Obj, fileName string) (*OfflineTask, error) { |
| 36 | + resolved, err := d.ResolveOfflineResource(ctx, fileURL) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + parentID := parentDir.GetID() |
| 42 | + if parentID == d.RootFolderID { |
| 43 | + parentID = "" |
| 44 | + } |
| 45 | + |
| 46 | + taskURL := strings.TrimSpace(resolved.URL) |
| 47 | + if taskURL == "" { |
| 48 | + taskURL = strings.TrimSpace(fileURL) |
| 49 | + } |
| 50 | + name := strings.TrimSpace(fileName) |
| 51 | + if name == "" { |
| 52 | + name = resolved.defaultName(taskURL) |
| 53 | + } |
| 54 | + |
| 55 | + body := map[string]any{ |
| 56 | + "url": taskURL, |
| 57 | + "parentId": parentID, |
| 58 | + "newName": name, |
| 59 | + } |
| 60 | + if indexes := resolved.fileIndexes(); len(indexes) > 0 { |
| 61 | + body["fileIndexes"] = indexes |
| 62 | + } |
| 63 | + |
| 64 | + var resp offlineCreateResp |
| 65 | + if err := d.postAPI(ctx, "/cloudcollection/v1/create_task", body, &resp); err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + if !isSuccessMsg(resp.Msg) { |
| 69 | + return nil, fmt.Errorf("create offline task failed: %s", strings.TrimSpace(resp.Msg)) |
| 70 | + } |
| 71 | + taskID := strings.TrimSpace(resp.Data.TaskID) |
| 72 | + if taskID == "" { |
| 73 | + return nil, errors.New("create offline task failed: empty task id") |
| 74 | + } |
| 75 | + return &OfflineTask{ |
| 76 | + TaskID: taskID, |
| 77 | + FileName: name, |
| 78 | + Res: taskURL, |
| 79 | + }, nil |
| 80 | +} |
| 81 | + |
| 82 | +func (d *GuangYaPan) OfflineList(ctx context.Context, taskIDs []string, statuses []int, cursor string, pageSize int) ([]OfflineTask, error) { |
| 83 | + if err := d.ensureAccessToken(ctx); err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + body := map[string]any{} |
| 87 | + if len(taskIDs) > 0 { |
| 88 | + body["taskIds"] = taskIDs |
| 89 | + } |
| 90 | + if len(statuses) > 0 { |
| 91 | + body["status"] = statuses |
| 92 | + } |
| 93 | + if cursor = strings.TrimSpace(cursor); cursor != "" { |
| 94 | + body["cursor"] = cursor |
| 95 | + } |
| 96 | + if pageSize > 0 { |
| 97 | + body["pageSize"] = pageSize |
| 98 | + } |
| 99 | + |
| 100 | + var resp offlineListResp |
| 101 | + if err := d.postAPI(ctx, "/cloudcollection/v1/list_task", body, &resp); err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + if !isSuccessMsg(resp.Msg) { |
| 105 | + return nil, fmt.Errorf("list offline tasks failed: %s", strings.TrimSpace(resp.Msg)) |
| 106 | + } |
| 107 | + return resp.Data.List, nil |
| 108 | +} |
| 109 | + |
| 110 | +func (d *GuangYaPan) DeleteOfflineTasks(ctx context.Context, taskIDs []string, deleteFiles bool) error { |
| 111 | + if err := d.ensureAccessToken(ctx); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + if len(taskIDs) == 0 { |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + var resp offlineDeleteResp |
| 119 | + if err := d.postAPI(ctx, "/cloudcollection/v2/delete_task", map[string]any{ |
| 120 | + "taskIds": taskIDs, |
| 121 | + }, &resp); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + if !isSuccessMsg(resp.Msg) { |
| 125 | + return fmt.Errorf("delete offline tasks failed: %s", strings.TrimSpace(resp.Msg)) |
| 126 | + } |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func (d OfflineResolveData) defaultName(fileURL string) string { |
| 131 | + if d.BTResInfo != nil && strings.TrimSpace(d.BTResInfo.FileName) != "" { |
| 132 | + return strings.TrimSpace(d.BTResInfo.FileName) |
| 133 | + } |
| 134 | + u, err := url.Parse(fileURL) |
| 135 | + if err == nil { |
| 136 | + name := strings.TrimSpace(stdpath.Base(u.Path)) |
| 137 | + if name != "" && name != "." && name != "/" { |
| 138 | + if decoded, err := url.PathUnescape(name); err == nil { |
| 139 | + name = decoded |
| 140 | + } |
| 141 | + return name |
| 142 | + } |
| 143 | + } |
| 144 | + return "offline_download" |
| 145 | +} |
| 146 | + |
| 147 | +func (d OfflineResolveData) fileIndexes() []int { |
| 148 | + if d.BTResInfo == nil || len(d.BTResInfo.Subfiles) == 0 { |
| 149 | + return nil |
| 150 | + } |
| 151 | + indexes := make([]int, 0, len(d.BTResInfo.Subfiles)) |
| 152 | + for i, file := range d.BTResInfo.Subfiles { |
| 153 | + if file.FileIndex != nil { |
| 154 | + indexes = append(indexes, *file.FileIndex) |
| 155 | + continue |
| 156 | + } |
| 157 | + indexes = append(indexes, i) |
| 158 | + } |
| 159 | + return indexes |
| 160 | +} |
| 161 | + |
| 162 | +func isSuccessMsg(msg string) bool { |
| 163 | + msg = strings.TrimSpace(msg) |
| 164 | + return msg == "" || strings.EqualFold(msg, "success") |
| 165 | +} |
0 commit comments