Skip to content

Commit 6591af5

Browse files
committed
feat: store search file index
1 parent 58568d4 commit 6591af5

17 files changed

Lines changed: 304 additions & 145 deletions

File tree

bootstrap/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func InitModel() {
7474
log.Infof("auto migrate model...")
7575
if databaseConfig.Type == "mysql" {
7676
err = conf.DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").
77-
AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{})
77+
AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{}, &model.SearchFile{})
7878
} else {
79-
err = conf.DB.AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{})
79+
err = conf.DB.AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{}, &model.SearchFile{})
8080
}
8181
if err != nil {
8282
log.Fatalf("failed to auto migrate: %s", err.Error())

drivers/123/123.go

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,8 @@ import (
1212
log "github.com/sirupsen/logrus"
1313
"path/filepath"
1414
"strconv"
15-
"time"
1615
)
1716

18-
type BaseResp struct {
19-
Code int `json:"code"`
20-
Message string `json:"message"`
21-
}
22-
23-
type Pan123TokenResp struct {
24-
BaseResp
25-
Data struct {
26-
Token string `json:"token"`
27-
} `json:"data"`
28-
}
29-
30-
type Pan123File struct {
31-
FileName string `json:"FileName"`
32-
Size int64 `json:"Size"`
33-
UpdateAt *time.Time `json:"UpdateAt"`
34-
FileId int64 `json:"FileId"`
35-
Type int `json:"Type"`
36-
Etag string `json:"Etag"`
37-
S3KeyFlag string `json:"S3KeyFlag"`
38-
}
39-
40-
type Pan123Files struct {
41-
BaseResp
42-
Data struct {
43-
InfoList []Pan123File `json:"InfoList"`
44-
Next string `json:"Next"`
45-
} `json:"data"`
46-
}
47-
48-
type Pan123DownResp struct {
49-
BaseResp
50-
Data struct {
51-
DownloadUrl string `json:"DownloadUrl"`
52-
} `json:"data"`
53-
}
54-
55-
type UploadResp struct {
56-
BaseResp
57-
Data struct {
58-
AccessKeyId string `json:"AccessKeyId"`
59-
Bucket string `json:"Bucket"`
60-
Key string `json:"Key"`
61-
SecretAccessKey string `json:"SecretAccessKey"`
62-
SessionToken string `json:"SessionToken"`
63-
FileId int64 `json:"FileId"`
64-
} `json:"data"`
65-
}
66-
6717
func (driver Pan123) Login(account *model.Account) error {
6818
url := "https://www.123pan.com/api/user/sign_in"
6919
if account.APIProxyUrl != "" {
@@ -98,11 +48,7 @@ func (driver Pan123) FormatFile(file *Pan123File) *model.File {
9848
Driver: driver.Config().Name,
9949
UpdatedAt: file.UpdateAt,
10050
}
101-
if file.Type == 1 {
102-
f.Type = conf.FOLDER
103-
} else {
104-
f.Type = utils.GetFileType(filepath.Ext(file.FileName))
105-
}
51+
f.Type = file.GetType()
10652
return f
10753
}
10854

drivers/123/types.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package _23
2+
3+
import (
4+
"github.com/Xhofe/alist/conf"
5+
"github.com/Xhofe/alist/utils"
6+
"path"
7+
"time"
8+
)
9+
10+
type Pan123File struct {
11+
FileName string `json:"FileName"`
12+
Size int64 `json:"Size"`
13+
UpdateAt *time.Time `json:"UpdateAt"`
14+
FileId int64 `json:"FileId"`
15+
Type int `json:"Type"`
16+
Etag string `json:"Etag"`
17+
S3KeyFlag string `json:"S3KeyFlag"`
18+
}
19+
20+
func (f Pan123File) GetSize() uint64 {
21+
return uint64(f.Size)
22+
}
23+
24+
func (f Pan123File) GetName() string {
25+
return f.FileName
26+
}
27+
28+
func (f Pan123File) GetType() int {
29+
if f.Type == 1 {
30+
return conf.FOLDER
31+
}
32+
return utils.GetFileType(path.Ext(f.FileName))
33+
}
34+
35+
type BaseResp struct {
36+
Code int `json:"code"`
37+
Message string `json:"message"`
38+
}
39+
40+
type Pan123TokenResp struct {
41+
BaseResp
42+
Data struct {
43+
Token string `json:"token"`
44+
} `json:"data"`
45+
}
46+
47+
type Pan123Files struct {
48+
BaseResp
49+
Data struct {
50+
InfoList []Pan123File `json:"InfoList"`
51+
Next string `json:"Next"`
52+
} `json:"data"`
53+
}
54+
55+
type Pan123DownResp struct {
56+
BaseResp
57+
Data struct {
58+
DownloadUrl string `json:"DownloadUrl"`
59+
} `json:"data"`
60+
}
61+
62+
type UploadResp struct {
63+
BaseResp
64+
Data struct {
65+
AccessKeyId string `json:"AccessKeyId"`
66+
Bucket string `json:"Bucket"`
67+
Key string `json:"Key"`
68+
SecretAccessKey string `json:"SecretAccessKey"`
69+
SessionToken string `json:"SessionToken"`
70+
FileId int64 `json:"FileId"`
71+
} `json:"data"`
72+
}

drivers/189/189.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"math"
1919
"net/http"
2020
"net/http/cookiejar"
21-
"path/filepath"
2221
"regexp"
2322
"strconv"
2423
"strings"
@@ -60,11 +59,9 @@ func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
6059
f.UpdatedAt = &lastOpTime
6160
}
6261
if file.Size == -1 {
63-
f.Type = conf.FOLDER
6462
f.Size = 0
65-
} else {
66-
f.Type = utils.GetFileType(filepath.Ext(file.Name))
6763
}
64+
f.Type = file.GetType()
6865
return f
6966
}
7067

drivers/189/types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package _89
22

3+
import (
4+
"github.com/Xhofe/alist/conf"
5+
"github.com/Xhofe/alist/utils"
6+
"path"
7+
)
8+
39
type Cloud189Error struct {
410
ErrorCode string `json:"errorCode"`
511
ErrorMsg string `json:"errorMsg"`
@@ -17,6 +23,24 @@ type Cloud189File struct {
1723
Url string `json:"url"`
1824
}
1925

26+
func (f Cloud189File) GetSize() uint64 {
27+
if f.Size == -1 {
28+
return 0
29+
}
30+
return uint64(f.Size)
31+
}
32+
33+
func (f Cloud189File) GetName() string {
34+
return f.Name
35+
}
36+
37+
func (f Cloud189File) GetType() int {
38+
if f.Size == -1 {
39+
return conf.FOLDER
40+
}
41+
return utils.GetFileType(path.Ext(f.Name))
42+
}
43+
2044
type Cloud189Folder struct {
2145
Id int64 `json:"id"`
2246
LastOpTime string `json:"lastOpTime"`

drivers/alidrive/alidrive.go

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,17 @@ package alidrive
33
import (
44
"errors"
55
"fmt"
6-
"github.com/Xhofe/alist/conf"
76
"github.com/Xhofe/alist/drivers/base"
87
"github.com/Xhofe/alist/model"
98
"github.com/Xhofe/alist/utils"
109
"github.com/go-resty/resty/v2"
1110
jsoniter "github.com/json-iterator/go"
1211
log "github.com/sirupsen/logrus"
1312
"path/filepath"
14-
"time"
1513
)
1614

1715
var aliClient = resty.New()
1816

19-
type AliRespError struct {
20-
Code string `json:"code"`
21-
Message string `json:"message"`
22-
}
23-
24-
type AliFiles struct {
25-
Items []AliFile `json:"items"`
26-
NextMarker string `json:"next_marker"`
27-
}
28-
29-
type AliFile struct {
30-
DriveId string `json:"drive_id"`
31-
CreatedAt *time.Time `json:"created_at"`
32-
FileExtension string `json:"file_extension"`
33-
FileId string `json:"file_id"`
34-
Type string `json:"type"`
35-
Name string `json:"name"`
36-
Category string `json:"category"`
37-
ParentFileId string `json:"parent_file_id"`
38-
UpdatedAt *time.Time `json:"updated_at"`
39-
Size int64 `json:"size"`
40-
Thumbnail string `json:"thumbnail"`
41-
Url string `json:"url"`
42-
}
43-
4417
func (driver AliDrive) FormatFile(file *AliFile) *model.File {
4518
f := &model.File{
4619
Id: file.FileId,
@@ -51,17 +24,7 @@ func (driver AliDrive) FormatFile(file *AliFile) *model.File {
5124
Driver: driver.Config().Name,
5225
Url: file.Url,
5326
}
54-
if file.Type == "folder" {
55-
f.Type = conf.FOLDER
56-
} else {
57-
f.Type = utils.GetFileType(file.FileExtension)
58-
}
59-
if file.Category == "video" {
60-
f.Type = conf.VIDEO
61-
}
62-
if file.Category == "image" {
63-
f.Type = conf.IMAGE
64-
}
27+
f.Type = file.GetType()
6528
return f
6629
}
6730

drivers/alidrive/types.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package alidrive
2+
3+
import (
4+
"github.com/Xhofe/alist/conf"
5+
"github.com/Xhofe/alist/utils"
6+
"time"
7+
)
8+
9+
type AliRespError struct {
10+
Code string `json:"code"`
11+
Message string `json:"message"`
12+
}
13+
14+
type AliFiles struct {
15+
Items []AliFile `json:"items"`
16+
NextMarker string `json:"next_marker"`
17+
}
18+
19+
type AliFile struct {
20+
DriveId string `json:"drive_id"`
21+
CreatedAt *time.Time `json:"created_at"`
22+
FileExtension string `json:"file_extension"`
23+
FileId string `json:"file_id"`
24+
Type string `json:"type"`
25+
Name string `json:"name"`
26+
Category string `json:"category"`
27+
ParentFileId string `json:"parent_file_id"`
28+
UpdatedAt *time.Time `json:"updated_at"`
29+
Size int64 `json:"size"`
30+
Thumbnail string `json:"thumbnail"`
31+
Url string `json:"url"`
32+
}
33+
34+
func (f AliFile) GetSize() uint64 {
35+
return uint64(f.Size)
36+
}
37+
38+
func (f AliFile) GetName() string {
39+
return f.Name
40+
}
41+
42+
func (f AliFile) GetType() int {
43+
if f.Type == "folder" {
44+
return conf.FOLDER
45+
}
46+
if f.Category == "video" {
47+
return conf.VIDEO
48+
}
49+
if f.Category == "image" {
50+
return conf.IMAGE
51+
}
52+
return utils.GetFileType(f.FileExtension)
53+
}

drivers/base/cache.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
package base
22

33
import (
4-
"fmt"
54
"github.com/Xhofe/alist/conf"
65
"github.com/Xhofe/alist/model"
76
"github.com/Xhofe/alist/utils"
87
log "github.com/sirupsen/logrus"
98
)
109

1110
func KeyCache(path string, account *model.Account) string {
12-
path = utils.ParsePath(path)
13-
return fmt.Sprintf("%s%s", account.Name, path)
11+
//path = utils.ParsePath(path)
12+
key := utils.ParsePath(utils.Join(account.Name, path))
13+
log.Debugln("cache key: ", key)
14+
return key
1415
}
1516

16-
func SetCache(path string, obj interface{}, account *model.Account) error {
17-
return conf.Cache.Set(conf.Ctx, KeyCache(path, account), obj, nil)
17+
func SaveSearchFiles[T model.ISearchFile](key string, obj []T) {
18+
err := model.DeleteSearchFilesByPath(key)
19+
if err != nil {
20+
log.Errorln("failed create search files", err)
21+
return
22+
}
23+
files := make([]model.SearchFile, len(obj))
24+
for i := 0; i < len(obj); i++ {
25+
files[i] = model.SearchFile{
26+
Path: key,
27+
Name: obj[i].GetName(),
28+
Size: obj[i].GetSize(),
29+
Type: obj[i].GetType(),
30+
}
31+
}
32+
err = model.CreateSearchFiles(files)
33+
if err != nil {
34+
log.Errorln("failed create search files", err)
35+
}
36+
}
37+
38+
func SetCache[T model.ISearchFile](path string, obj []T, account *model.Account) error {
39+
key := KeyCache(path, account)
40+
go SaveSearchFiles(key, obj)
41+
return conf.Cache.Set(conf.Ctx, key, obj, nil)
1842
}
1943

2044
func GetCache(path string, account *model.Account) (interface{}, error) {

0 commit comments

Comments
 (0)