Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/123_share/driver.go
1986 views
1
package _123Share
2
3
import (
4
"context"
5
"encoding/base64"
6
"fmt"
7
"net/http"
8
"net/url"
9
"sync"
10
"time"
11
12
"golang.org/x/time/rate"
13
14
_123 "github.com/alist-org/alist/v3/drivers/123"
15
"github.com/alist-org/alist/v3/drivers/base"
16
"github.com/alist-org/alist/v3/internal/driver"
17
"github.com/alist-org/alist/v3/internal/errs"
18
"github.com/alist-org/alist/v3/internal/model"
19
"github.com/alist-org/alist/v3/pkg/utils"
20
"github.com/go-resty/resty/v2"
21
log "github.com/sirupsen/logrus"
22
)
23
24
type Pan123Share struct {
25
model.Storage
26
Addition
27
apiRateLimit sync.Map
28
ref *_123.Pan123
29
}
30
31
func (d *Pan123Share) Config() driver.Config {
32
return config
33
}
34
35
func (d *Pan123Share) GetAddition() driver.Additional {
36
return &d.Addition
37
}
38
39
func (d *Pan123Share) Init(ctx context.Context) error {
40
// TODO login / refresh token
41
//op.MustSaveDriverStorage(d)
42
return nil
43
}
44
45
func (d *Pan123Share) InitReference(storage driver.Driver) error {
46
refStorage, ok := storage.(*_123.Pan123)
47
if ok {
48
d.ref = refStorage
49
return nil
50
}
51
return fmt.Errorf("ref: storage is not 123Pan")
52
}
53
54
func (d *Pan123Share) Drop(ctx context.Context) error {
55
d.ref = nil
56
return nil
57
}
58
59
func (d *Pan123Share) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
60
// TODO return the files list, required
61
files, err := d.getFiles(ctx, dir.GetID())
62
if err != nil {
63
return nil, err
64
}
65
return utils.SliceConvert(files, func(src File) (model.Obj, error) {
66
return src, nil
67
})
68
}
69
70
func (d *Pan123Share) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
71
// TODO return link of file, required
72
if f, ok := file.(File); ok {
73
//var resp DownResp
74
var headers map[string]string
75
if !utils.IsLocalIPAddr(args.IP) {
76
headers = map[string]string{
77
//"X-Real-IP": "1.1.1.1",
78
"X-Forwarded-For": args.IP,
79
}
80
}
81
data := base.Json{
82
"shareKey": d.ShareKey,
83
"SharePwd": d.SharePwd,
84
"etag": f.Etag,
85
"fileId": f.FileId,
86
"s3keyFlag": f.S3KeyFlag,
87
"size": f.Size,
88
}
89
resp, err := d.request(DownloadInfo, http.MethodPost, func(req *resty.Request) {
90
req.SetBody(data).SetHeaders(headers)
91
}, nil)
92
if err != nil {
93
return nil, err
94
}
95
downloadUrl := utils.Json.Get(resp, "data", "DownloadURL").ToString()
96
u, err := url.Parse(downloadUrl)
97
if err != nil {
98
return nil, err
99
}
100
nu := u.Query().Get("params")
101
if nu != "" {
102
du, _ := base64.StdEncoding.DecodeString(nu)
103
u, err = url.Parse(string(du))
104
if err != nil {
105
return nil, err
106
}
107
}
108
u_ := u.String()
109
log.Debug("download url: ", u_)
110
res, err := base.NoRedirectClient.R().SetHeader("Referer", "https://www.123pan.com/").Get(u_)
111
if err != nil {
112
return nil, err
113
}
114
log.Debug(res.String())
115
link := model.Link{
116
URL: u_,
117
}
118
log.Debugln("res code: ", res.StatusCode())
119
if res.StatusCode() == 302 {
120
link.URL = res.Header().Get("location")
121
} else if res.StatusCode() < 300 {
122
link.URL = utils.Json.Get(res.Body(), "data", "redirect_url").ToString()
123
}
124
link.Header = http.Header{
125
"Referer": []string{"https://www.123pan.com/"},
126
}
127
return &link, nil
128
}
129
return nil, fmt.Errorf("can't convert obj")
130
}
131
132
func (d *Pan123Share) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
133
// TODO create folder, optional
134
return errs.NotSupport
135
}
136
137
func (d *Pan123Share) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
138
// TODO move obj, optional
139
return errs.NotSupport
140
}
141
142
func (d *Pan123Share) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
143
// TODO rename obj, optional
144
return errs.NotSupport
145
}
146
147
func (d *Pan123Share) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
148
// TODO copy obj, optional
149
return errs.NotSupport
150
}
151
152
func (d *Pan123Share) Remove(ctx context.Context, obj model.Obj) error {
153
// TODO remove obj, optional
154
return errs.NotSupport
155
}
156
157
func (d *Pan123Share) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
158
// TODO upload file, optional
159
return errs.NotSupport
160
}
161
162
//func (d *Pan123Share) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
163
// return nil, errs.NotSupport
164
//}
165
166
func (d *Pan123Share) APIRateLimit(ctx context.Context, api string) error {
167
value, _ := d.apiRateLimit.LoadOrStore(api,
168
rate.NewLimiter(rate.Every(700*time.Millisecond), 1))
169
limiter := value.(*rate.Limiter)
170
171
return limiter.Wait(ctx)
172
}
173
174
var _ driver.Driver = (*Pan123Share)(nil)
175
176