|
| 1 | +package terbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/md5" |
| 7 | + "encoding/hex" |
| 8 | + "fmt" |
| 9 | + "github.com/alist-org/alist/v3/drivers/base" |
| 10 | + "github.com/alist-org/alist/v3/pkg/utils" |
| 11 | + log "github.com/sirupsen/logrus" |
| 12 | + "io" |
| 13 | + "math" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + stdpath "path" |
| 17 | + "strconv" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/alist-org/alist/v3/internal/driver" |
| 21 | + "github.com/alist-org/alist/v3/internal/model" |
| 22 | +) |
| 23 | + |
| 24 | +type Terabox struct { |
| 25 | + model.Storage |
| 26 | + Addition |
| 27 | +} |
| 28 | + |
| 29 | +func (d *Terabox) Config() driver.Config { |
| 30 | + return config |
| 31 | +} |
| 32 | + |
| 33 | +func (d *Terabox) GetAddition() driver.Additional { |
| 34 | + return &d.Addition |
| 35 | +} |
| 36 | + |
| 37 | +func (d *Terabox) Init(ctx context.Context) error { |
| 38 | + _, err := d.request("https://www.terabox.com/api/check/#", http.MethodGet, nil, nil) |
| 39 | + return err |
| 40 | +} |
| 41 | + |
| 42 | +func (d *Terabox) Drop(ctx context.Context) error { |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func (d *Terabox) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { |
| 47 | + files, err := d.getFiles(dir.GetPath()) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return utils.SliceConvert(files, func(src File) (model.Obj, error) { |
| 52 | + return fileToObj(src), nil |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func (d *Terabox) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { |
| 57 | + if d.DownloadAPI == "crack" { |
| 58 | + return d.linkCrack(file, args) |
| 59 | + } |
| 60 | + return d.linkOfficial(file, args) |
| 61 | +} |
| 62 | + |
| 63 | +func (d *Terabox) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error { |
| 64 | + _, err := d.create(stdpath.Join(parentDir.GetPath(), dirName), 0, 1, "", "") |
| 65 | + return err |
| 66 | +} |
| 67 | + |
| 68 | +func (d *Terabox) Move(ctx context.Context, srcObj, dstDir model.Obj) error { |
| 69 | + data := []base.Json{ |
| 70 | + { |
| 71 | + "path": srcObj.GetPath(), |
| 72 | + "dest": dstDir.GetPath(), |
| 73 | + "newname": srcObj.GetName(), |
| 74 | + }, |
| 75 | + } |
| 76 | + _, err := d.manage("move", data) |
| 77 | + return err |
| 78 | +} |
| 79 | + |
| 80 | +func (d *Terabox) Rename(ctx context.Context, srcObj model.Obj, newName string) error { |
| 81 | + data := []base.Json{ |
| 82 | + { |
| 83 | + "path": srcObj.GetPath(), |
| 84 | + "newname": newName, |
| 85 | + }, |
| 86 | + } |
| 87 | + _, err := d.manage("rename", data) |
| 88 | + return err |
| 89 | +} |
| 90 | + |
| 91 | +func (d *Terabox) Copy(ctx context.Context, srcObj, dstDir model.Obj) error { |
| 92 | + data := []base.Json{ |
| 93 | + { |
| 94 | + "path": srcObj.GetPath(), |
| 95 | + "dest": dstDir.GetPath(), |
| 96 | + "newname": srcObj.GetName(), |
| 97 | + }, |
| 98 | + } |
| 99 | + _, err := d.manage("copy", data) |
| 100 | + return err |
| 101 | +} |
| 102 | + |
| 103 | +func (d *Terabox) Remove(ctx context.Context, obj model.Obj) error { |
| 104 | + data := []string{obj.GetPath()} |
| 105 | + _, err := d.manage("delete", data) |
| 106 | + return err |
| 107 | +} |
| 108 | + |
| 109 | +func (d *Terabox) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error { |
| 110 | + tempFile, err := utils.CreateTempFile(stream.GetReadCloser()) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + defer func() { |
| 115 | + _ = tempFile.Close() |
| 116 | + _ = os.Remove(tempFile.Name()) |
| 117 | + }() |
| 118 | + var Default int64 = 4 * 1024 * 1024 |
| 119 | + defaultByteData := make([]byte, Default) |
| 120 | + count := int(math.Ceil(float64(stream.GetSize()) / float64(Default))) |
| 121 | + // cal md5 |
| 122 | + h1 := md5.New() |
| 123 | + h2 := md5.New() |
| 124 | + block_list := make([]string, 0) |
| 125 | + left := stream.GetSize() |
| 126 | + for i := 0; i < count; i++ { |
| 127 | + byteSize := Default |
| 128 | + var byteData []byte |
| 129 | + if left < Default { |
| 130 | + byteSize = left |
| 131 | + byteData = make([]byte, byteSize) |
| 132 | + } else { |
| 133 | + byteData = defaultByteData |
| 134 | + } |
| 135 | + left -= byteSize |
| 136 | + _, err = io.ReadFull(tempFile, byteData) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + h1.Write(byteData) |
| 141 | + h2.Write(byteData) |
| 142 | + block_list = append(block_list, fmt.Sprintf("\"%s\"", hex.EncodeToString(h2.Sum(nil)))) |
| 143 | + h2.Reset() |
| 144 | + } |
| 145 | + |
| 146 | + _, err = tempFile.Seek(0, io.SeekStart) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + rawPath := stdpath.Join(dstDir.GetPath(), stream.GetName()) |
| 152 | + path := encodeURIComponent(rawPath) |
| 153 | + block_list_str := fmt.Sprintf("[%s]", strings.Join(block_list, ",")) |
| 154 | + data := fmt.Sprintf("path=%s&size=%d&isdir=0&autoinit=1&block_list=%s", |
| 155 | + path, stream.GetSize(), |
| 156 | + block_list_str) |
| 157 | + params := map[string]string{} |
| 158 | + var precreateResp PrecreateResp |
| 159 | + _, err = d.post("/api/precreate", params, data, &precreateResp) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + log.Debugf("%+v", precreateResp) |
| 164 | + if precreateResp.ReturnType == 2 { |
| 165 | + return nil |
| 166 | + } |
| 167 | + params = map[string]string{ |
| 168 | + "method": "upload", |
| 169 | + "path": path, |
| 170 | + "uploadid": precreateResp.Uploadid, |
| 171 | + "app_id": "250528", |
| 172 | + "web": "1", |
| 173 | + "channel": "dubox", |
| 174 | + "clienttype": "0", |
| 175 | + } |
| 176 | + left = stream.GetSize() |
| 177 | + for i, partseq := range precreateResp.BlockList { |
| 178 | + if utils.IsCanceled(ctx) { |
| 179 | + return ctx.Err() |
| 180 | + } |
| 181 | + byteSize := Default |
| 182 | + var byteData []byte |
| 183 | + if left < Default { |
| 184 | + byteSize = left |
| 185 | + byteData = make([]byte, byteSize) |
| 186 | + } else { |
| 187 | + byteData = defaultByteData |
| 188 | + } |
| 189 | + left -= byteSize |
| 190 | + _, err = io.ReadFull(tempFile, byteData) |
| 191 | + if err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + u := "https://c-jp.terabox.com/rest/2.0/pcs/superfile2" |
| 195 | + params["partseq"] = strconv.Itoa(partseq) |
| 196 | + res, err := base.RestyClient.R(). |
| 197 | + SetContext(ctx). |
| 198 | + SetQueryParams(params). |
| 199 | + SetFileReader("file", stream.GetName(), bytes.NewReader(byteData)). |
| 200 | + SetHeader("Cookie", d.Cookie). |
| 201 | + Post(u) |
| 202 | + if err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + log.Debugln(res.String()) |
| 206 | + if len(precreateResp.BlockList) > 0 { |
| 207 | + up(i * 100 / len(precreateResp.BlockList)) |
| 208 | + } |
| 209 | + } |
| 210 | + _, err = d.create(rawPath, stream.GetSize(), 0, precreateResp.Uploadid, block_list_str) |
| 211 | + return err |
| 212 | +} |
| 213 | + |
| 214 | +var _ driver.Driver = (*Terabox)(nil) |
0 commit comments