Skip to content

Commit da1c7a4

Browse files
author
sheltonzhu
authored
feat: add 115_share driver (#5481 close #5384)
This update introduces the ability to mount 115 share links. Currently, only listing and downloading are supported. Note that login and share link are required for this feature to work. Close #5384
1 parent 769281b commit da1c7a4

File tree

6 files changed

+261
-96
lines changed

6 files changed

+261
-96
lines changed

drivers/115_share/driver.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package _115_share
2+
3+
import (
4+
"context"
5+
6+
driver115 "github.com/SheltonZhu/115driver/pkg/driver"
7+
"github.com/alist-org/alist/v3/internal/driver"
8+
"github.com/alist-org/alist/v3/internal/errs"
9+
"github.com/alist-org/alist/v3/internal/model"
10+
"github.com/alist-org/alist/v3/pkg/utils"
11+
"golang.org/x/time/rate"
12+
)
13+
14+
type Pan115Share struct {
15+
model.Storage
16+
Addition
17+
client *driver115.Pan115Client
18+
limiter *rate.Limiter
19+
}
20+
21+
func (d *Pan115Share) Config() driver.Config {
22+
return config
23+
}
24+
25+
func (d *Pan115Share) GetAddition() driver.Additional {
26+
return &d.Addition
27+
}
28+
29+
func (d *Pan115Share) Init(ctx context.Context) error {
30+
if d.LimitRate > 0 {
31+
d.limiter = rate.NewLimiter(rate.Limit(d.LimitRate), 1)
32+
}
33+
34+
return d.login()
35+
}
36+
37+
func (d *Pan115Share) WaitLimit(ctx context.Context) error {
38+
if d.limiter != nil {
39+
return d.limiter.Wait(ctx)
40+
}
41+
return nil
42+
}
43+
44+
func (d *Pan115Share) Drop(ctx context.Context) error {
45+
return nil
46+
}
47+
48+
func (d *Pan115Share) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
49+
if err := d.WaitLimit(ctx); err != nil {
50+
return nil, err
51+
}
52+
53+
files := make([]driver115.ShareFile, 0)
54+
fileResp, err := d.client.GetShareSnap(d.ShareCode, d.ReceiveCode, dir.GetID(), driver115.QueryLimit(int(d.PageSize)))
55+
if err != nil {
56+
return nil, err
57+
}
58+
files = append(files, fileResp.Data.List...)
59+
total := fileResp.Data.Count
60+
count := len(fileResp.Data.List)
61+
for total > count {
62+
fileResp, err := d.client.GetShareSnap(
63+
d.ShareCode, d.ReceiveCode, dir.GetID(),
64+
driver115.QueryLimit(int(d.PageSize)), driver115.QueryOffset(count),
65+
)
66+
if err != nil {
67+
return nil, err
68+
}
69+
files = append(files, fileResp.Data.List...)
70+
count += len(fileResp.Data.List)
71+
}
72+
73+
return utils.SliceConvert(files, transFunc)
74+
}
75+
76+
func (d *Pan115Share) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
77+
if err := d.WaitLimit(ctx); err != nil {
78+
return nil, err
79+
}
80+
downloadInfo, err := d.client.DownloadByShareCode(d.ShareCode, d.ReceiveCode, file.GetID())
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
return &model.Link{URL: downloadInfo.URL.URL}, nil
86+
}
87+
88+
func (d *Pan115Share) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
89+
return errs.NotSupport
90+
}
91+
92+
func (d *Pan115Share) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
93+
return errs.NotSupport
94+
}
95+
96+
func (d *Pan115Share) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
97+
return errs.NotSupport
98+
}
99+
100+
func (d *Pan115Share) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
101+
return errs.NotSupport
102+
}
103+
104+
func (d *Pan115Share) Remove(ctx context.Context, obj model.Obj) error {
105+
return errs.NotSupport
106+
}
107+
108+
func (d *Pan115Share) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
109+
return errs.NotSupport
110+
}
111+
112+
var _ driver.Driver = (*Pan115Share)(nil)

drivers/115_share/meta.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package _115_share
2+
3+
import (
4+
"github.com/alist-org/alist/v3/internal/driver"
5+
"github.com/alist-org/alist/v3/internal/op"
6+
)
7+
8+
type Addition struct {
9+
Cookie string `json:"cookie" type:"text" help:"one of QR code token and cookie required"`
10+
QRCodeToken string `json:"qrcode_token" type:"text" help:"one of QR code token and cookie required"`
11+
PageSize int64 `json:"page_size" type:"number" default:"20" help:"list api per page size of 115 driver"`
12+
LimitRate float64 `json:"limit_rate" type:"number" default:"2" help:"limit all api request rate (1r/[limit_rate]s)"`
13+
ShareCode string `json:"share_code" type:"text" required:"true" help:"share code of 115 share link"`
14+
ReceiveCode string `json:"receive_code" type:"text" required:"true" help:"receive code of 115 share link"`
15+
driver.RootID
16+
}
17+
18+
var config = driver.Config{
19+
Name: "115 Share",
20+
DefaultRoot: "",
21+
// OnlyProxy: true,
22+
// OnlyLocal: true,
23+
CheckStatus: false,
24+
Alert: "",
25+
NoOverwriteUpload: true,
26+
NoUpload: true,
27+
}
28+
29+
func init() {
30+
op.RegisterDriver(func() driver.Driver {
31+
return &Pan115Share{}
32+
})
33+
}

drivers/115_share/utils.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package _115_share
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"time"
7+
8+
driver115 "github.com/SheltonZhu/115driver/pkg/driver"
9+
"github.com/alist-org/alist/v3/internal/model"
10+
"github.com/alist-org/alist/v3/pkg/utils"
11+
"github.com/pkg/errors"
12+
)
13+
14+
var _ model.Obj = (*FileObj)(nil)
15+
16+
type FileObj struct {
17+
Size int64
18+
Sha1 string
19+
Utm time.Time
20+
FileName string
21+
isDir bool
22+
FileID string
23+
}
24+
25+
func (f *FileObj) CreateTime() time.Time {
26+
return f.Utm
27+
}
28+
29+
func (f *FileObj) GetHash() utils.HashInfo {
30+
return utils.NewHashInfo(utils.SHA1, f.Sha1)
31+
}
32+
33+
func (f *FileObj) GetSize() int64 {
34+
return f.Size
35+
}
36+
37+
func (f *FileObj) GetName() string {
38+
return f.FileName
39+
}
40+
41+
func (f *FileObj) ModTime() time.Time {
42+
return f.Utm
43+
}
44+
45+
func (f *FileObj) IsDir() bool {
46+
return f.isDir
47+
}
48+
49+
func (f *FileObj) GetID() string {
50+
return f.FileID
51+
}
52+
53+
func (f *FileObj) GetPath() string {
54+
return ""
55+
}
56+
57+
func transFunc(sf driver115.ShareFile) (model.Obj, error) {
58+
timeInt, err := strconv.ParseInt(sf.UpdateTime, 10, 64)
59+
if err != nil {
60+
return nil, err
61+
}
62+
var (
63+
utm = time.Unix(timeInt, 0)
64+
isDir = (sf.IsFile == 0)
65+
fileID = string(sf.FileID)
66+
)
67+
if isDir {
68+
fileID = string(sf.CategoryID)
69+
}
70+
return &FileObj{
71+
Size: int64(sf.Size),
72+
Sha1: sf.Sha1,
73+
Utm: utm,
74+
FileName: string(sf.FileName),
75+
isDir: isDir,
76+
FileID: fileID,
77+
}, nil
78+
}
79+
80+
var UserAgent = driver115.UA115Browser
81+
82+
func (d *Pan115Share) login() error {
83+
var err error
84+
opts := []driver115.Option{
85+
driver115.UA(UserAgent),
86+
}
87+
d.client = driver115.New(opts...)
88+
if _, err := d.client.GetShareSnap(d.ShareCode, d.ReceiveCode, ""); err != nil {
89+
return errors.Wrap(err, "failed to get share snap")
90+
}
91+
cr := &driver115.Credential{}
92+
if d.QRCodeToken != "" {
93+
s := &driver115.QRCodeSession{
94+
UID: d.QRCodeToken,
95+
}
96+
if cr, err = d.client.QRCodeLogin(s); err != nil {
97+
return errors.Wrap(err, "failed to login by qrcode")
98+
}
99+
d.Cookie = fmt.Sprintf("UID=%s;CID=%s;SEID=%s", cr.UID, cr.CID, cr.SEID)
100+
d.QRCodeToken = ""
101+
} else if d.Cookie != "" {
102+
if err = cr.FromCookie(d.Cookie); err != nil {
103+
return errors.Wrap(err, "failed to login by cookies")
104+
}
105+
d.client.ImportCredential(cr)
106+
} else {
107+
return errors.New("missing cookie or qrcode account")
108+
}
109+
110+
return d.client.LoginCheck()
111+
}

drivers/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package drivers
22

33
import (
44
_ "github.com/alist-org/alist/v3/drivers/115"
5+
_ "github.com/alist-org/alist/v3/drivers/115_share"
56
_ "github.com/alist-org/alist/v3/drivers/123"
67
_ "github.com/alist-org/alist/v3/drivers/123_link"
78
_ "github.com/alist-org/alist/v3/drivers/123_share"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/alist-org/alist/v3
33
go 1.20
44

55
require (
6-
github.com/SheltonZhu/115driver v1.0.16
6+
github.com/SheltonZhu/115driver v1.0.21
77
github.com/Xhofe/go-cache v0.0.0-20220723083548-714439c8af9a
88
github.com/Xhofe/rateg v0.0.0-20230728072201-251a4e1adad4
99
github.com/Xhofe/wopan-sdk-go v0.1.2

0 commit comments

Comments
 (0)