chore: Merge pull request #938 from Xhofe/feature/search

Feature/search
pull/948/head
Xhofe 2022-04-13 21:57:59 +08:00 committed by GitHub
commit e24814ee2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 346 additions and 145 deletions

View File

@ -74,9 +74,9 @@ func InitModel() {
log.Infof("auto migrate model...")
if databaseConfig.Type == "mysql" {
err = conf.DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").
AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{})
AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{}, &model.SearchFile{})
} else {
err = conf.DB.AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{})
err = conf.DB.AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{}, &model.SearchFile{})
}
if err != nil {
log.Fatalf("failed to auto migrate: %s", err.Error())

View File

@ -258,6 +258,13 @@ func InitSettings() {
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "enable search",
Value: "false",
Type: "bool",
Access: model.PUBLIC,
Group: model.BACK,
},
}
for i, _ := range settings {
v := settings[i]

View File

@ -85,6 +85,7 @@ var (
"Visitor WebDAV username", "Visitor WebDAV password",
"default page size", "load type",
"ocr api", "favicon",
"enable search",
}
)

View File

@ -12,58 +12,8 @@ import (
log "github.com/sirupsen/logrus"
"path/filepath"
"strconv"
"time"
)
type BaseResp struct {
Code int `json:"code"`
Message string `json:"message"`
}
type Pan123TokenResp struct {
BaseResp
Data struct {
Token string `json:"token"`
} `json:"data"`
}
type Pan123File struct {
FileName string `json:"FileName"`
Size int64 `json:"Size"`
UpdateAt *time.Time `json:"UpdateAt"`
FileId int64 `json:"FileId"`
Type int `json:"Type"`
Etag string `json:"Etag"`
S3KeyFlag string `json:"S3KeyFlag"`
}
type Pan123Files struct {
BaseResp
Data struct {
InfoList []Pan123File `json:"InfoList"`
Next string `json:"Next"`
} `json:"data"`
}
type Pan123DownResp struct {
BaseResp
Data struct {
DownloadUrl string `json:"DownloadUrl"`
} `json:"data"`
}
type UploadResp struct {
BaseResp
Data struct {
AccessKeyId string `json:"AccessKeyId"`
Bucket string `json:"Bucket"`
Key string `json:"Key"`
SecretAccessKey string `json:"SecretAccessKey"`
SessionToken string `json:"SessionToken"`
FileId int64 `json:"FileId"`
} `json:"data"`
}
func (driver Pan123) Login(account *model.Account) error {
url := "https://www.123pan.com/api/user/sign_in"
if account.APIProxyUrl != "" {
@ -98,11 +48,7 @@ func (driver Pan123) FormatFile(file *Pan123File) *model.File {
Driver: driver.Config().Name,
UpdatedAt: file.UpdateAt,
}
if file.Type == 1 {
f.Type = conf.FOLDER
} else {
f.Type = utils.GetFileType(filepath.Ext(file.FileName))
}
f.Type = file.GetType()
return f
}

72
drivers/123/types.go Normal file
View File

@ -0,0 +1,72 @@
package _23
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"path"
"time"
)
type Pan123File struct {
FileName string `json:"FileName"`
Size int64 `json:"Size"`
UpdateAt *time.Time `json:"UpdateAt"`
FileId int64 `json:"FileId"`
Type int `json:"Type"`
Etag string `json:"Etag"`
S3KeyFlag string `json:"S3KeyFlag"`
}
func (f Pan123File) GetSize() uint64 {
return uint64(f.Size)
}
func (f Pan123File) GetName() string {
return f.FileName
}
func (f Pan123File) GetType() int {
if f.Type == 1 {
return conf.FOLDER
}
return utils.GetFileType(path.Ext(f.FileName))
}
type BaseResp struct {
Code int `json:"code"`
Message string `json:"message"`
}
type Pan123TokenResp struct {
BaseResp
Data struct {
Token string `json:"token"`
} `json:"data"`
}
type Pan123Files struct {
BaseResp
Data struct {
InfoList []Pan123File `json:"InfoList"`
Next string `json:"Next"`
} `json:"data"`
}
type Pan123DownResp struct {
BaseResp
Data struct {
DownloadUrl string `json:"DownloadUrl"`
} `json:"data"`
}
type UploadResp struct {
BaseResp
Data struct {
AccessKeyId string `json:"AccessKeyId"`
Bucket string `json:"Bucket"`
Key string `json:"Key"`
SecretAccessKey string `json:"SecretAccessKey"`
SessionToken string `json:"SessionToken"`
FileId int64 `json:"FileId"`
} `json:"data"`
}

View File

@ -18,7 +18,6 @@ import (
"math"
"net/http"
"net/http/cookiejar"
"path/filepath"
"regexp"
"strconv"
"strings"
@ -60,11 +59,9 @@ func (driver Cloud189) FormatFile(file *Cloud189File) *model.File {
f.UpdatedAt = &lastOpTime
}
if file.Size == -1 {
f.Type = conf.FOLDER
f.Size = 0
} else {
f.Type = utils.GetFileType(filepath.Ext(file.Name))
}
f.Type = file.GetType()
return f
}

View File

@ -1,5 +1,11 @@
package _89
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"path"
)
type Cloud189Error struct {
ErrorCode string `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
@ -17,6 +23,24 @@ type Cloud189File struct {
Url string `json:"url"`
}
func (f Cloud189File) GetSize() uint64 {
if f.Size == -1 {
return 0
}
return uint64(f.Size)
}
func (f Cloud189File) GetName() string {
return f.Name
}
func (f Cloud189File) GetType() int {
if f.Size == -1 {
return conf.FOLDER
}
return utils.GetFileType(path.Ext(f.Name))
}
type Cloud189Folder struct {
Id int64 `json:"id"`
LastOpTime string `json:"lastOpTime"`

View File

@ -3,7 +3,6 @@ package alidrive
import (
"errors"
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
@ -11,36 +10,10 @@ import (
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"path/filepath"
"time"
)
var aliClient = resty.New()
type AliRespError struct {
Code string `json:"code"`
Message string `json:"message"`
}
type AliFiles struct {
Items []AliFile `json:"items"`
NextMarker string `json:"next_marker"`
}
type AliFile struct {
DriveId string `json:"drive_id"`
CreatedAt *time.Time `json:"created_at"`
FileExtension string `json:"file_extension"`
FileId string `json:"file_id"`
Type string `json:"type"`
Name string `json:"name"`
Category string `json:"category"`
ParentFileId string `json:"parent_file_id"`
UpdatedAt *time.Time `json:"updated_at"`
Size int64 `json:"size"`
Thumbnail string `json:"thumbnail"`
Url string `json:"url"`
}
func (driver AliDrive) FormatFile(file *AliFile) *model.File {
f := &model.File{
Id: file.FileId,
@ -51,17 +24,7 @@ func (driver AliDrive) FormatFile(file *AliFile) *model.File {
Driver: driver.Config().Name,
Url: file.Url,
}
if file.Type == "folder" {
f.Type = conf.FOLDER
} else {
f.Type = utils.GetFileType(file.FileExtension)
}
if file.Category == "video" {
f.Type = conf.VIDEO
}
if file.Category == "image" {
f.Type = conf.IMAGE
}
f.Type = file.GetType()
return f
}

53
drivers/alidrive/types.go Normal file
View File

@ -0,0 +1,53 @@
package alidrive
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"time"
)
type AliRespError struct {
Code string `json:"code"`
Message string `json:"message"`
}
type AliFiles struct {
Items []AliFile `json:"items"`
NextMarker string `json:"next_marker"`
}
type AliFile struct {
DriveId string `json:"drive_id"`
CreatedAt *time.Time `json:"created_at"`
FileExtension string `json:"file_extension"`
FileId string `json:"file_id"`
Type string `json:"type"`
Name string `json:"name"`
Category string `json:"category"`
ParentFileId string `json:"parent_file_id"`
UpdatedAt *time.Time `json:"updated_at"`
Size int64 `json:"size"`
Thumbnail string `json:"thumbnail"`
Url string `json:"url"`
}
func (f AliFile) GetSize() uint64 {
return uint64(f.Size)
}
func (f AliFile) GetName() string {
return f.Name
}
func (f AliFile) GetType() int {
if f.Type == "folder" {
return conf.FOLDER
}
if f.Category == "video" {
return conf.VIDEO
}
if f.Category == "image" {
return conf.IMAGE
}
return utils.GetFileType(f.FileExtension)
}

View File

@ -1,7 +1,6 @@
package base
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
@ -9,12 +8,39 @@ import (
)
func KeyCache(path string, account *model.Account) string {
path = utils.ParsePath(path)
return fmt.Sprintf("%s%s", account.Name, path)
//path = utils.ParsePath(path)
key := utils.ParsePath(utils.Join(account.Name, path))
log.Debugln("cache key: ", key)
return key
}
func SetCache(path string, obj interface{}, account *model.Account) error {
return conf.Cache.Set(conf.Ctx, KeyCache(path, account), obj, nil)
func SaveSearchFiles[T model.ISearchFile](key string, obj []T) {
err := model.DeleteSearchFilesByPath(key)
if err != nil {
log.Errorln("failed create search files", err)
return
}
files := make([]model.SearchFile, len(obj))
for i := 0; i < len(obj); i++ {
files[i] = model.SearchFile{
Path: key,
Name: obj[i].GetName(),
Size: obj[i].GetSize(),
Type: obj[i].GetType(),
}
}
err = model.CreateSearchFiles(files)
if err != nil {
log.Errorln("failed create search files", err)
}
}
func SetCache[T model.ISearchFile](path string, obj []T, account *model.Account) error {
key := KeyCache(path, account)
if conf.GetBool("enable search") {
go SaveSearchFiles(key, obj)
}
return conf.Cache.Set(conf.Ctx, key, obj, nil)
}
func GetCache(path string, account *model.Account) (interface{}, error) {

View File

@ -2,15 +2,10 @@ package google
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"path/filepath"
"strconv"
"time"
)
type TokenError struct {
@ -44,19 +39,6 @@ func (driver GoogleDrive) RefreshToken(account *model.Account) error {
return nil
}
type File struct {
Id string `json:"id"`
Name string `json:"name"`
MimeType string `json:"mimeType"`
ModifiedTime *time.Time `json:"modifiedTime"`
Size string `json:"size"`
ThumbnailLink string `json:"thumbnailLink"`
}
func (driver GoogleDrive) IsDir(mimeType string) bool {
return mimeType == "application/vnd.google-apps.folder" || mimeType == "application/vnd.google-apps.shortcut"
}
func (driver GoogleDrive) FormatFile(file *File, account *model.Account) *model.File {
f := &model.File{
Id: file.Id,
@ -65,13 +47,8 @@ func (driver GoogleDrive) FormatFile(file *File, account *model.Account) *model.
UpdatedAt: file.ModifiedTime,
Url: "",
}
if driver.IsDir(file.MimeType) {
f.Type = conf.FOLDER
} else {
size, _ := strconv.ParseInt(file.Size, 10, 64)
f.Size = size
f.Type = utils.GetFileType(filepath.Ext(file.Name))
}
f.Size = int64(file.GetSize())
f.Type = file.GetType()
if file.ThumbnailLink != "" {
if account.APIProxyUrl != "" {
f.Thumbnail = fmt.Sprintf("%s/%s", account.APIProxyUrl, file.ThumbnailLink)

38
drivers/google/types.go Normal file
View File

@ -0,0 +1,38 @@
package google
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"path"
"strconv"
"time"
)
type File struct {
Id string `json:"id"`
Name string `json:"name"`
MimeType string `json:"mimeType"`
ModifiedTime *time.Time `json:"modifiedTime"`
Size string `json:"size"`
ThumbnailLink string `json:"thumbnailLink"`
}
func (f File) GetSize() uint64 {
if f.GetType() == conf.FOLDER {
return 0
}
size, _ := strconv.ParseUint(f.Size, 10, 64)
return size
}
func (f File) GetName() string {
return f.Name
}
func (f File) GetType() int {
mimeType := f.MimeType
if mimeType == "application/vnd.google-apps.folder" || mimeType == "application/vnd.google-apps.shortcut" {
return conf.FOLDER
}
return utils.GetFileType(path.Ext(f.Name))
}

View File

@ -2,28 +2,15 @@ package lanzou
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"net/url"
"path/filepath"
"regexp"
"strconv"
"time"
)
type LanZouFile struct {
Name string `json:"name"`
NameAll string `json:"name_all"`
Id string `json:"id"`
FolId string `json:"fol_id"`
Size string `json:"size"`
Time string `json:"time"`
Folder bool
}
func (driver *Lanzou) FormatFile(file *LanZouFile) *model.File {
now := time.Now()
f := &model.File{
@ -35,12 +22,11 @@ func (driver *Lanzou) FormatFile(file *LanZouFile) *model.File {
UpdatedAt: &now,
}
if file.Folder {
f.Type = conf.FOLDER
f.Id = file.FolId
} else {
f.Name = file.NameAll
f.Type = utils.GetFileType(filepath.Ext(file.NameAll))
}
f.Type = file.GetType()
return f
}

View File

@ -1,5 +1,39 @@
package lanzou
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"path"
)
type LanZouFile struct {
Name string `json:"name"`
NameAll string `json:"name_all"`
Id string `json:"id"`
FolId string `json:"fol_id"`
Size string `json:"size"`
Time string `json:"time"`
Folder bool
}
func (f LanZouFile) GetSize() uint64 {
return 0
}
func (f LanZouFile) GetName() string {
if f.Folder {
return f.Name
}
return f.NameAll
}
func (f LanZouFile) GetType() int {
if f.Folder {
return conf.FOLDER
}
return utils.GetFileType(path.Ext(f.NameAll))
}
type DownPageResp struct {
Zt int `json:"zt"`
Info struct {

View File

@ -115,6 +115,10 @@ func (driver Native) Files(path string, account *model.Account) ([]model.File, e
}
files = append(files, file)
}
_, err = base.GetCache(path, account)
if len(files) != 0 && err != nil {
_ = base.SetCache(path, files, account)
}
return files, nil
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/Xhofe/alist
go 1.17
go 1.18
require (
github.com/aws/aws-sdk-go v1.27.0

View File

@ -84,3 +84,7 @@ func (f File) ModTime() time.Time {
func (f File) IsDir() bool {
return f.Type == conf.FOLDER
}
func (f File) GetType() int {
return f.Type
}

35
model/search_file.go Normal file
View File

@ -0,0 +1,35 @@
package model
import (
"fmt"
"github.com/Xhofe/alist/conf"
)
type ISearchFile interface {
GetName() string
GetSize() uint64
GetType() int
}
type SearchFile struct {
Path string `json:"path" gorm:"index"`
Name string `json:"name"`
Size uint64 `json:"size"`
Type int `json:"type"`
}
func CreateSearchFiles(files []SearchFile) error {
return conf.DB.Create(files).Error
}
func DeleteSearchFilesByPath(path string) error {
return conf.DB.Where(fmt.Sprintf("%s = ?", columnName("path")), path).Delete(&SearchFile{}).Error
}
func SearchByNameAndPath(path, keyword string) ([]SearchFile, error) {
var files []SearchFile
if err := conf.DB.Where(fmt.Sprintf("%s LIKE ? AND %s LIKE ?", columnName("path"), columnName("name")), fmt.Sprintf("%s%%", path), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
return nil, err
}
return files, nil
}

View File

@ -61,6 +61,7 @@ func Proxy(c *gin.Context) {
return
}
err = common.Proxy(c.Writer, c.Request, link, file)
log.Debugln("web proxy error:", err)
if err != nil {
common.ErrorResp(c, err, 500)
}

View File

@ -0,0 +1,31 @@
package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
type SearchReq struct {
Path string `json:"path"`
Keyword string `json:"keyword"`
}
func Search(c *gin.Context) {
if !conf.GetBool("enable search") {
common.ErrorStrResp(c, "Not allowed search", 403)
return
}
var req SearchReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
files, err := model.SearchByNameAndPath(req.Path, req.Keyword)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, files)
}

View File

@ -25,6 +25,8 @@ func InitApiRouter(r *gin.Engine) {
path.POST("/path", controllers.Path)
path.POST("/preview", controllers.Preview)
public.POST("/search", controllers.Search)
//path.POST("/link",middlewares.Auth, controllers.Link)
public.POST("/upload", file.UploadFiles)