|
| 1 | +package teldrive |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/OpenListTeam/OpenList/v4/drivers/base" |
| 12 | + "github.com/OpenListTeam/OpenList/v4/internal/driver" |
| 13 | + "github.com/OpenListTeam/OpenList/v4/internal/errs" |
| 14 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 15 | + "github.com/OpenListTeam/OpenList/v4/internal/op" |
| 16 | + "github.com/OpenListTeam/OpenList/v4/pkg/utils" |
| 17 | + "github.com/go-resty/resty/v2" |
| 18 | + "github.com/google/uuid" |
| 19 | +) |
| 20 | + |
| 21 | +type Teldrive struct { |
| 22 | + model.Storage |
| 23 | + Addition |
| 24 | +} |
| 25 | + |
| 26 | +func (d *Teldrive) Config() driver.Config { |
| 27 | + return config |
| 28 | +} |
| 29 | + |
| 30 | +func (d *Teldrive) GetAddition() driver.Additional { |
| 31 | + return &d.Addition |
| 32 | +} |
| 33 | + |
| 34 | +func (d *Teldrive) Init(ctx context.Context) error { |
| 35 | + d.Address = strings.TrimSuffix(d.Address, "/") |
| 36 | + if d.Cookie == "" || !strings.HasPrefix(d.Cookie, "access_token=") { |
| 37 | + return fmt.Errorf("cookie must start with 'access_token='") |
| 38 | + } |
| 39 | + if d.UploadConcurrency == 0 { |
| 40 | + d.UploadConcurrency = 4 |
| 41 | + } |
| 42 | + if d.ChunkSize == 0 { |
| 43 | + d.ChunkSize = 10 |
| 44 | + } |
| 45 | + |
| 46 | + op.MustSaveDriverStorage(d) |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func (d *Teldrive) Drop(ctx context.Context) error { |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func (d *Teldrive) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { |
| 55 | + var listResp ListResp |
| 56 | + err := d.request(http.MethodGet, "/api/files", func(req *resty.Request) { |
| 57 | + req.SetQueryParams(map[string]string{ |
| 58 | + "path": dir.GetPath(), |
| 59 | + "limit": "1000", // overide default 500, TODO pagination |
| 60 | + }) |
| 61 | + }, &listResp) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return utils.SliceConvert(listResp.Items, func(src Object) (model.Obj, error) { |
| 67 | + return &model.Object{ |
| 68 | + ID: src.ID, |
| 69 | + Name: src.Name, |
| 70 | + Size: func() int64 { |
| 71 | + if src.Type == "folder" { |
| 72 | + return 0 |
| 73 | + } |
| 74 | + return src.Size |
| 75 | + }(), |
| 76 | + IsFolder: src.Type == "folder", |
| 77 | + Modified: src.UpdatedAt, |
| 78 | + }, nil |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +func (d *Teldrive) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { |
| 83 | + if d.UseShareLink { |
| 84 | + shareObj, err := d.getShareFileById(file.GetID()) |
| 85 | + if err != nil || shareObj == nil { |
| 86 | + if err := d.createShareFile(file.GetID()); err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + shareObj, err = d.getShareFileById(file.GetID()) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + } |
| 94 | + return &model.Link{ |
| 95 | + URL: d.Address + "/api/shares/" + url.PathEscape(shareObj.Id) + "/files/" + url.PathEscape(file.GetID()) + "/" + url.PathEscape(file.GetName()), |
| 96 | + }, nil |
| 97 | + } |
| 98 | + return &model.Link{ |
| 99 | + URL: d.Address + "/api/files/" + url.PathEscape(file.GetID()) + "/" + url.PathEscape(file.GetName()), |
| 100 | + Header: http.Header{ |
| 101 | + "Cookie": {d.Cookie}, |
| 102 | + }, |
| 103 | + }, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (d *Teldrive) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error { |
| 107 | + return d.request(http.MethodPost, "/api/files/mkdir", func(req *resty.Request) { |
| 108 | + req.SetBody(map[string]interface{}{ |
| 109 | + "path": parentDir.GetPath() + "/" + dirName, |
| 110 | + }) |
| 111 | + }, nil) |
| 112 | +} |
| 113 | + |
| 114 | +func (d *Teldrive) Move(ctx context.Context, srcObj, dstDir model.Obj) error { |
| 115 | + body := base.Json{ |
| 116 | + "ids": []string{srcObj.GetID()}, |
| 117 | + "destinationParent": dstDir.GetID(), |
| 118 | + } |
| 119 | + return d.request(http.MethodPost, "/api/files/move", func(req *resty.Request) { |
| 120 | + req.SetBody(body) |
| 121 | + }, nil) |
| 122 | +} |
| 123 | + |
| 124 | +func (d *Teldrive) Rename(ctx context.Context, srcObj model.Obj, newName string) error { |
| 125 | + body := base.Json{ |
| 126 | + "name": newName, |
| 127 | + } |
| 128 | + return d.request(http.MethodPatch, "/api/files/{id}", func(req *resty.Request) { |
| 129 | + req.SetPathParam("id", srcObj.GetID()) |
| 130 | + req.SetBody(body) |
| 131 | + }, nil) |
| 132 | +} |
| 133 | + |
| 134 | +func (d *Teldrive) Copy(ctx context.Context, srcObj, dstDir model.Obj) error { |
| 135 | + copyConcurrentLimit := 4 |
| 136 | + copyManager := NewCopyManager(ctx, copyConcurrentLimit, d) |
| 137 | + copyManager.startWorkers() |
| 138 | + copyManager.G.Go(func() error { |
| 139 | + defer close(copyManager.TaskChan) |
| 140 | + return copyManager.generateTasks(ctx, srcObj, dstDir) |
| 141 | + }) |
| 142 | + return copyManager.G.Wait() |
| 143 | +} |
| 144 | + |
| 145 | +func (d *Teldrive) Remove(ctx context.Context, obj model.Obj) error { |
| 146 | + body := base.Json{ |
| 147 | + "ids": []string{obj.GetID()}, |
| 148 | + } |
| 149 | + return d.request(http.MethodPost, "/api/files/delete", func(req *resty.Request) { |
| 150 | + req.SetBody(body) |
| 151 | + }, nil) |
| 152 | +} |
| 153 | + |
| 154 | +func (d *Teldrive) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) error { |
| 155 | + fileId := uuid.New().String() |
| 156 | + chunkSizeInMB := d.ChunkSize |
| 157 | + chunkSize := chunkSizeInMB * 1024 * 1024 // Convert MB to bytes |
| 158 | + totalSize := file.GetSize() |
| 159 | + totalParts := int(math.Ceil(float64(totalSize) / float64(chunkSize))) |
| 160 | + maxRetried := 3 |
| 161 | + |
| 162 | + // delete the upload task when finished or failed |
| 163 | + defer func() { |
| 164 | + _ = d.request(http.MethodDelete, "/api/uploads/{id}", func(req *resty.Request) { |
| 165 | + req.SetPathParam("id", fileId) |
| 166 | + }, nil) |
| 167 | + }() |
| 168 | + |
| 169 | + if obj, err := d.getFile(dstDir.GetPath(), file.GetName(), file.IsDir()); err == nil { |
| 170 | + if err = d.Remove(ctx, obj); err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + } |
| 174 | + // start the upload process |
| 175 | + if err := d.request(http.MethodGet, "/api/uploads/fileId", func(req *resty.Request) { |
| 176 | + req.SetPathParam("id", fileId) |
| 177 | + }, nil); err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + if totalSize == 0 { |
| 181 | + return d.touch(file.GetName(), dstDir.GetPath()) |
| 182 | + } |
| 183 | + |
| 184 | + if totalParts <= 1 { |
| 185 | + return d.doSingleUpload(ctx, dstDir, file, up, totalParts, chunkSize, fileId) |
| 186 | + } |
| 187 | + |
| 188 | + return d.doMultiUpload(ctx, dstDir, file, up, maxRetried, totalParts, chunkSize, fileId) |
| 189 | +} |
| 190 | + |
| 191 | +func (d *Teldrive) GetArchiveMeta(ctx context.Context, obj model.Obj, args model.ArchiveArgs) (model.ArchiveMeta, error) { |
| 192 | + // TODO get archive file meta-info, return errs.NotImplement to use an internal archive tool, optional |
| 193 | + return nil, errs.NotImplement |
| 194 | +} |
| 195 | + |
| 196 | +func (d *Teldrive) ListArchive(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) ([]model.Obj, error) { |
| 197 | + // TODO list args.InnerPath in the archive obj, return errs.NotImplement to use an internal archive tool, optional |
| 198 | + return nil, errs.NotImplement |
| 199 | +} |
| 200 | + |
| 201 | +func (d *Teldrive) Extract(ctx context.Context, obj model.Obj, args model.ArchiveInnerArgs) (*model.Link, error) { |
| 202 | + // TODO return link of file args.InnerPath in the archive obj, return errs.NotImplement to use an internal archive tool, optional |
| 203 | + return nil, errs.NotImplement |
| 204 | +} |
| 205 | + |
| 206 | +func (d *Teldrive) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.Obj, args model.ArchiveDecompressArgs) ([]model.Obj, error) { |
| 207 | + // TODO extract args.InnerPath path in the archive srcObj to the dstDir location, optional |
| 208 | + // a folder with the same name as the archive file needs to be created to store the extracted results if args.PutIntoNewDir |
| 209 | + // return errs.NotImplement to use an internal archive tool |
| 210 | + return nil, errs.NotImplement |
| 211 | +} |
| 212 | + |
| 213 | +//func (d *Teldrive) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) { |
| 214 | +// return nil, errs.NotSupport |
| 215 | +//} |
| 216 | + |
| 217 | +var _ driver.Driver = (*Teldrive)(nil) |
0 commit comments