Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
6d19b49a8d | |||
4fa11879f2 | |||
abe9d9237a | |||
e4d206d59c | |||
8636014397 | |||
c0f50ffeff | |||
b677d6ad21 | |||
443067b80f | |||
3138e031f5 | |||
d137ef8759 | |||
389226662c |
@ -1,16 +0,0 @@
|
|||||||
package bootstrap
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/Xhofe/alist/conf"
|
|
||||||
"github.com/patrickmn/go-cache"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// init cache
|
|
||||||
func InitCache() {
|
|
||||||
if conf.Conf.Cache.Enable {
|
|
||||||
log.Infof("初始化缓存...")
|
|
||||||
conf.Cache=cache.New(time.Duration(conf.Conf.Cache.Expiration)*time.Minute,time.Duration(conf.Conf.Cache.CleanupInterval)*time.Minute)
|
|
||||||
}
|
|
||||||
}
|
|
@ -61,7 +61,10 @@ func start() {
|
|||||||
log.Errorf("初始化阿里云盘出现错误,启动失败.")
|
log.Errorf("初始化阿里云盘出现错误,启动失败.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
InitCache()
|
if !InitModel() {
|
||||||
|
log.Errorf("初始化数据库出现错误,启动失败.")
|
||||||
|
return
|
||||||
|
}
|
||||||
InitCron()
|
InitCron()
|
||||||
server()
|
server()
|
||||||
}
|
}
|
||||||
|
46
bootstrap/model.go
Normal file
46
bootstrap/model.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Xhofe/alist/conf"
|
||||||
|
"github.com/Xhofe/alist/server/models"
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitModel() bool {
|
||||||
|
log.Infof("初始化数据库...")
|
||||||
|
switch conf.Conf.Database.Type {
|
||||||
|
case "sqlite3":
|
||||||
|
{
|
||||||
|
if !(strings.HasSuffix(conf.Conf.Database.DBFile, ".db") && len(conf.Conf.Database.DBFile) > 3) {
|
||||||
|
log.Errorf("db名称不正确.")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
needMigrate := !utils.Exists(conf.Conf.Database.DBFile)
|
||||||
|
db, err := gorm.Open(sqlite.Open(conf.Conf.Database.DBFile), &gorm.Config{})
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("连接数据库出现错误:%s", err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
conf.DB = db
|
||||||
|
if needMigrate {
|
||||||
|
log.Infof("迁移数据库...")
|
||||||
|
err = conf.DB.AutoMigrate(&models.File{})
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("数据库迁移失败:%s", err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if err := models.BuildTree(); err != nil {
|
||||||
|
log.Errorf("构建目录树失败:%s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Errorf("不支持的数据库类型:%s", conf.Conf.Database.Type)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
@ -16,7 +16,7 @@ type GithubRelease struct {
|
|||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// check updtae
|
// check update
|
||||||
func CheckUpdate() {
|
func CheckUpdate() {
|
||||||
log.Infof("检查更新...")
|
log.Infof("检查更新...")
|
||||||
url := "https://api.github.com/repos/Xhofe/alist/releases/latest"
|
url := "https://api.github.com/repos/Xhofe/alist/releases/latest"
|
||||||
|
@ -21,16 +21,11 @@ type Config struct {
|
|||||||
} `yaml:"info"`
|
} `yaml:"info"`
|
||||||
Server struct {
|
Server struct {
|
||||||
Port string `yaml:"port"` //端口
|
Port string `yaml:"port"` //端口
|
||||||
Search bool `yaml:"search" json:"search"`//允许搜索
|
Search bool `yaml:"search"` //允许搜索
|
||||||
Static string `yaml:"static"`
|
Static string `yaml:"static"`
|
||||||
SiteUrl string `yaml:"site_url" json:"site_url"`//网站url
|
SiteUrl string `yaml:"site_url"` //网站url
|
||||||
|
Password string `yaml:"password"`
|
||||||
} `yaml:"server"`
|
} `yaml:"server"`
|
||||||
Cache struct{
|
|
||||||
Enable bool `yaml:"enable"`
|
|
||||||
Expiration int `yaml:"expiration"`
|
|
||||||
CleanupInterval int `yaml:"cleanup_interval"`
|
|
||||||
RefreshPassword string `yaml:"refresh_password"`
|
|
||||||
}
|
|
||||||
AliDrive struct {
|
AliDrive struct {
|
||||||
ApiUrl string `yaml:"api_url"` //阿里云盘api
|
ApiUrl string `yaml:"api_url"` //阿里云盘api
|
||||||
RootFolder string `yaml:"root_folder"` //根目录id
|
RootFolder string `yaml:"root_folder"` //根目录id
|
||||||
@ -40,4 +35,14 @@ type Config struct {
|
|||||||
RefreshToken string `yaml:"refresh_token"`
|
RefreshToken string `yaml:"refresh_token"`
|
||||||
MaxFilesCount int `yaml:"max_files_count"`
|
MaxFilesCount int `yaml:"max_files_count"`
|
||||||
} `yaml:"ali_drive"`
|
} `yaml:"ali_drive"`
|
||||||
|
Database struct {
|
||||||
|
Type string `yaml:"type"`
|
||||||
|
User string `yaml:"user"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
|
Host string `yaml:"host"`
|
||||||
|
Port int `yaml:"port"`
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
TablePrefix string `yaml:"tablePrefix"`
|
||||||
|
DBFile string `yaml:"dBFile"`
|
||||||
|
} `yaml:"database"`
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/patrickmn/go-cache"
|
"gorm.io/gorm"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ var(
|
|||||||
Client *http.Client // request client
|
Client *http.Client // request client
|
||||||
Authorization string // authorization string
|
Authorization string // authorization string
|
||||||
|
|
||||||
Cache *cache.Cache // cache
|
DB *gorm.DB
|
||||||
|
|
||||||
Origins []string // allow origins
|
Origins []string // allow origins
|
||||||
)
|
)
|
||||||
@ -23,7 +23,7 @@ var(
|
|||||||
var Conf = new(Config)
|
var Conf = new(Config)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION="v0.1.7"
|
VERSION = "v1.0.0"
|
||||||
|
|
||||||
ImageThumbnailProcess = "image/resize,w_50"
|
ImageThumbnailProcess = "image/resize,w_50"
|
||||||
VideoThumbnailProcess = "video/snapshot,t_0,f_jpg,w_50"
|
VideoThumbnailProcess = "video/snapshot,t_0,f_jpg,w_50"
|
||||||
|
4
go.mod
4
go.mod
@ -9,9 +9,9 @@ require (
|
|||||||
github.com/golang/protobuf v1.4.3 // indirect
|
github.com/golang/protobuf v1.4.3 // indirect
|
||||||
github.com/json-iterator/go v1.1.10 // indirect
|
github.com/json-iterator/go v1.1.10 // indirect
|
||||||
github.com/leodido/go-urn v1.2.1 // indirect
|
github.com/leodido/go-urn v1.2.1 // indirect
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.6 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
|
||||||
github.com/robfig/cron/v3 v3.0.0
|
github.com/robfig/cron/v3 v3.0.0
|
||||||
github.com/sirupsen/logrus v1.7.0
|
github.com/sirupsen/logrus v1.7.0
|
||||||
github.com/ugorji/go v1.2.2 // indirect
|
github.com/ugorji/go v1.2.2 // indirect
|
||||||
@ -19,4 +19,6 @@ require (
|
|||||||
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect
|
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect
|
||||||
google.golang.org/protobuf v1.25.0 // indirect
|
google.golang.org/protobuf v1.25.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
|
gorm.io/driver/sqlite v1.1.4
|
||||||
|
gorm.io/gorm v1.21.1
|
||||||
)
|
)
|
||||||
|
24
go.sum
24
go.sum
@ -3,6 +3,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
|
|||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
|
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
@ -14,6 +15,7 @@ github.com/gin-contrib/static v0.0.0-20200916080430-d45d9a37d28e/go.mod h1:VhW/C
|
|||||||
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
|
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
|
||||||
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
|
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
|
||||||
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
|
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
|
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
|
||||||
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
|
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
|
||||||
@ -43,8 +45,13 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
|
|||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
|
||||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
|
||||||
|
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
|
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
|
||||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
@ -58,6 +65,10 @@ github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ic
|
|||||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
@ -66,9 +77,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
|
|||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/patrickmn/go-cache v1.0.0 h1:3gD5McaYs9CxjyK5AXGcq8gdeCARtd/9gJDUvVeaZ0Y=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
|
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
|
||||||
@ -79,6 +88,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
|||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||||
@ -124,6 +134,7 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
|
|||||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
@ -144,6 +155,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
|
|||||||
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
||||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||||
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
|
||||||
@ -152,6 +164,12 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
|||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
|
||||||
|
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
|
||||||
|
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
||||||
|
gorm.io/gorm v1.21.1 h1:ACwUZ+jzH8eG8zxgqTnMIdgWd+lGfCKZTUxL/uQ1ZQo=
|
||||||
|
gorm.io/gorm v1.21.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
53
server/controllers/down.go
Normal file
53
server/controllers/down.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Xhofe/alist/alidrive"
|
||||||
|
"github.com/Xhofe/alist/server/models"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DownReq struct {
|
||||||
|
Password string `form:"pw"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle download request
|
||||||
|
func Down(c *gin.Context) {
|
||||||
|
filePath := c.Param("path")[1:]
|
||||||
|
var down DownReq
|
||||||
|
if err := c.ShouldBindQuery(&down); err != nil {
|
||||||
|
c.JSON(200, MetaResponse(400, "Bad Request."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("down:%s", filePath)
|
||||||
|
dir, name := filepath.Split(filePath)
|
||||||
|
fileModel, err := models.GetFileByDirAndName(dir, name)
|
||||||
|
if err != nil {
|
||||||
|
if fileModel == nil {
|
||||||
|
c.JSON(200, MetaResponse(404, "Path not found."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if fileModel.Password != "" && fileModel.Password != down.Password {
|
||||||
|
if down.Password == "" {
|
||||||
|
c.JSON(200, MetaResponse(401, "need password."))
|
||||||
|
} else {
|
||||||
|
c.JSON(200, MetaResponse(401, "wrong password."))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if fileModel.Type == "folder" {
|
||||||
|
c.JSON(200, MetaResponse(406, "无法下载目录."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
file, err := alidrive.GetDownLoadUrl(fileModel.FileId)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Redirect(301, file.Url)
|
||||||
|
return
|
||||||
|
}
|
@ -2,74 +2,48 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/alidrive"
|
"github.com/Xhofe/alist/alidrive"
|
||||||
|
"github.com/Xhofe/alist/server/models"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"strings"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handle get request
|
// get request bean
|
||||||
// 因为下载地址有时效,所以去掉了文件请求和直链的缓存
|
type GetReq struct {
|
||||||
func Get(c *gin.Context) {
|
Path string `json:"path" binding:"required"`
|
||||||
var get alidrive.GetReq
|
Password string `json:"password"`
|
||||||
if err := c.ShouldBindJSON(&get); err != nil {
|
|
||||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debugf("get:%+v",get)
|
|
||||||
// cache
|
|
||||||
//cacheKey:=fmt.Sprintf("%s-%s","g",get.FileId)
|
|
||||||
//if conf.Conf.Cache.Enable {
|
|
||||||
// file,exist:=conf.Cache.Get(cacheKey)
|
|
||||||
// if exist {
|
|
||||||
// log.Debugf("使用了缓存:%s",cacheKey)
|
|
||||||
// c.JSON(200,DataResponse(file))
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
file,err:=alidrive.GetFile(get.FileId)
|
|
||||||
if err !=nil {
|
|
||||||
c.JSON(200, MetaResponse(500,err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
paths,err:=alidrive.GetPaths(get.FileId)
|
|
||||||
if err!=nil {
|
|
||||||
c.JSON(200, MetaResponse(500,err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
file.Paths=*paths
|
|
||||||
download,err:=alidrive.GetDownLoadUrl(get.FileId)
|
|
||||||
if err!=nil {
|
|
||||||
c.JSON(200, MetaResponse(500,err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
file.DownloadUrl=download.Url
|
|
||||||
//if conf.Conf.Cache.Enable {
|
|
||||||
// conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
|
|
||||||
//}
|
|
||||||
c.JSON(200, DataResponse(file))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Down(c *gin.Context) {
|
// handle get request
|
||||||
fileIdParam:=c.Param("file_id")
|
func Get(c *gin.Context) {
|
||||||
log.Debugf("down:%s",fileIdParam)
|
var get GetReq
|
||||||
fileId:=strings.Split(fileIdParam,"/")[1]
|
if err := c.ShouldBindJSON(&get); err != nil {
|
||||||
//cacheKey:=fmt.Sprintf("%s-%s","d",fileId)
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
//if conf.Conf.Cache.Enable {
|
return
|
||||||
// downloadUrl,exist:=conf.Cache.Get(cacheKey)
|
}
|
||||||
// if exist {
|
log.Debugf("list:%+v", get)
|
||||||
// log.Debugf("使用了缓存:%s",cacheKey)
|
dir, name := filepath.Split(get.Path)
|
||||||
// c.Redirect(301,downloadUrl.(string))
|
file, err := models.GetFileByDirAndName(dir, name)
|
||||||
// return
|
if err != nil {
|
||||||
// }
|
if file == nil {
|
||||||
//}
|
c.JSON(200, MetaResponse(404, "Path not found."))
|
||||||
file,err:=alidrive.GetDownLoadUrl(fileId)
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if file.Password != "" && file.Password != get.Password {
|
||||||
|
if get.Password == "" {
|
||||||
|
c.JSON(200, MetaResponse(401, "need password."))
|
||||||
|
} else {
|
||||||
|
c.JSON(200, MetaResponse(401, "wrong password."))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
down, err := alidrive.GetDownLoadUrl(file.FileId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if conf.Conf.Cache.Enable {
|
c.JSON(200, DataResponse(down))
|
||||||
// conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
|
|
||||||
//}
|
|
||||||
c.Redirect(301,file.Url)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/Xhofe/alist/alidrive"
|
|
||||||
"github.com/Xhofe/alist/conf"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/patrickmn/go-cache"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// list request bean
|
|
||||||
type ListReq struct {
|
|
||||||
Password string `json:"password"`
|
|
||||||
alidrive.ListReq
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle list request
|
|
||||||
func List(c *gin.Context) {
|
|
||||||
var list ListReq
|
|
||||||
if err := c.ShouldBindJSON(&list);err!=nil {
|
|
||||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debugf("list:%+v",list)
|
|
||||||
// cache
|
|
||||||
cacheKey:=fmt.Sprintf("%s-%s-%s","l",list.ParentFileId,list.Password)
|
|
||||||
if conf.Conf.Cache.Enable {
|
|
||||||
files,exist:=conf.Cache.Get(cacheKey)
|
|
||||||
if exist {
|
|
||||||
log.Debugf("使用了缓存:%s",cacheKey)
|
|
||||||
c.JSON(200, DataResponse(files))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var (
|
|
||||||
files *alidrive.Files
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if list.Limit == 0 {
|
|
||||||
list.Limit=50
|
|
||||||
}
|
|
||||||
if conf.Conf.AliDrive.MaxFilesCount!=0 {
|
|
||||||
list.Limit=conf.Conf.AliDrive.MaxFilesCount
|
|
||||||
}
|
|
||||||
if list.ParentFileId == "root" {
|
|
||||||
files,err=alidrive.GetRoot(list.Limit,list.Marker,list.OrderBy,list.OrderDirection)
|
|
||||||
}else {
|
|
||||||
files,err=alidrive.GetList(list.ParentFileId,list.Limit,list.Marker,list.OrderBy,list.OrderDirection)
|
|
||||||
}
|
|
||||||
if err!=nil {
|
|
||||||
c.JSON(200, MetaResponse(500,err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
password:=alidrive.HasPassword(files)
|
|
||||||
if password!="" && password!=list.Password {
|
|
||||||
if list.Password=="" {
|
|
||||||
c.JSON(200, MetaResponse(401,"need password."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.JSON(200, MetaResponse(401,"wrong password."))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
paths,err:=alidrive.GetPaths(list.ParentFileId)
|
|
||||||
if err!=nil {
|
|
||||||
c.JSON(200, MetaResponse(500,err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
files.Paths=*paths
|
|
||||||
//files.Readme=alidrive.HasReadme(files)
|
|
||||||
if conf.Conf.Cache.Enable {
|
|
||||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
|
||||||
}
|
|
||||||
c.JSON(200, DataResponse(files))
|
|
||||||
}
|
|
@ -6,11 +6,15 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type OfficePreviewReq struct {
|
||||||
|
FileId string `json:"file_id" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
// handle office_preview request
|
// handle office_preview request
|
||||||
func OfficePreview(c *gin.Context) {
|
func OfficePreview(c *gin.Context) {
|
||||||
var req alidrive.OfficePreviewUrlReq
|
var req OfficePreviewReq
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("preview_req:%+v", req)
|
log.Debugf("preview_req:%+v", req)
|
||||||
|
61
server/controllers/path.go
Normal file
61
server/controllers/path.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Xhofe/alist/server/models"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// path request bean
|
||||||
|
type PathReq struct {
|
||||||
|
Path string `json:"path" binding:"required"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle path request
|
||||||
|
func Path(c *gin.Context) {
|
||||||
|
var req PathReq
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("path:%+v", req)
|
||||||
|
// find model
|
||||||
|
dir, name := filepath.Split(req.Path)
|
||||||
|
file, err := models.GetFileByDirAndName(dir, name)
|
||||||
|
if err != nil {
|
||||||
|
// folder model not exist
|
||||||
|
if file == nil {
|
||||||
|
c.JSON(200, MetaResponse(404, "path not found."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// check password
|
||||||
|
if file.Password != "" && file.Password != req.Password {
|
||||||
|
if req.Password == "" {
|
||||||
|
c.JSON(200, MetaResponse(401, "need password."))
|
||||||
|
} else {
|
||||||
|
c.JSON(200, MetaResponse(401, "wrong password."))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// file
|
||||||
|
if file.Type == "file" {
|
||||||
|
c.JSON(200, DataResponse(file))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// folder
|
||||||
|
files, err := models.GetFilesByDir(req.Path + "/")
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// delete password
|
||||||
|
for i, _ := range *files {
|
||||||
|
(*files)[i].Password = ""
|
||||||
|
}
|
||||||
|
c.JSON(200, DataResponse(files))
|
||||||
|
}
|
@ -1,50 +1,35 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"github.com/Xhofe/alist/server/models"
|
||||||
"github.com/Xhofe/alist/alidrive"
|
|
||||||
"github.com/Xhofe/alist/conf"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/patrickmn/go-cache"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handle search request
|
type SearchReq struct {
|
||||||
func Search(c *gin.Context) {
|
Keyword string `json:"keyword" binding:"required"`
|
||||||
if !conf.Conf.Server.Search {
|
Dir string `json:"dir" binding:"required"`
|
||||||
c.JSON(200, MetaResponse(403,"Not allow search."))
|
}
|
||||||
|
|
||||||
|
func LocalSearch(c *gin.Context) {
|
||||||
|
var req SearchReq
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var search alidrive.SearchReq
|
log.Debugf("list:%+v", req)
|
||||||
if err := c.ShouldBindJSON(&search); err != nil {
|
files, err := models.SearchByNameInDir(req.Keyword, req.Dir)
|
||||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debugf("search:%+v",search)
|
|
||||||
// cache
|
|
||||||
cacheKey:=fmt.Sprintf("%s-%s","s",search.Query)
|
|
||||||
if conf.Conf.Cache.Enable {
|
|
||||||
files,exist:=conf.Cache.Get(cacheKey)
|
|
||||||
if exist {
|
|
||||||
log.Debugf("使用了缓存:%s",cacheKey)
|
|
||||||
c.JSON(200, DataResponse(files))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if search.Limit == 0 {
|
|
||||||
search.Limit=50
|
|
||||||
}
|
|
||||||
// Search只支持0-100
|
|
||||||
//if conf.Conf.AliDrive.MaxFilesCount!=0 {
|
|
||||||
// search.Limit=conf.Conf.AliDrive.MaxFilesCount
|
|
||||||
//}
|
|
||||||
files,err:=alidrive.Search(search.Query,search.Limit,search.OrderBy)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if files == nil {
|
||||||
|
c.JSON(200, MetaResponse(404, "Path not found."))
|
||||||
|
return
|
||||||
|
}
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if conf.Conf.Cache.Enable {
|
|
||||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
|
||||||
}
|
|
||||||
c.JSON(200, DataResponse(files))
|
c.JSON(200, DataResponse(files))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GlobalSearch(c *gin.Context) {
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
|
"github.com/Xhofe/alist/server/models"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,18 +11,25 @@ func Info(c *gin.Context) {
|
|||||||
c.JSON(200, DataResponse(conf.Conf.Info))
|
c.JSON(200, DataResponse(conf.Conf.Info))
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle refresh_cache request
|
// rebuild tree
|
||||||
func RefreshCache(c *gin.Context) {
|
func RebuildTree(c *gin.Context) {
|
||||||
password:=c.Param("password")
|
password := c.Param("password")[1:]
|
||||||
if conf.Conf.Cache.Enable {
|
if password != conf.Conf.Server.Password {
|
||||||
if password == conf.Conf.Cache.RefreshPassword {
|
if password == "" {
|
||||||
conf.Cache.Flush()
|
c.JSON(200, MetaResponse(401, "need password."))
|
||||||
c.JSON(200, MetaResponse(200,"flush success."))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, MetaResponse(401, "wrong password."))
|
c.JSON(200, MetaResponse(401, "wrong password."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, MetaResponse(400,"disabled cache."))
|
if err := models.Clear(); err != nil {
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := models.BuildTree(); err != nil {
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, MetaResponse(200, "success."))
|
||||||
return
|
return
|
||||||
}
|
}
|
80
server/models/create.go
Normal file
80
server/models/create.go
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/Xhofe/alist/alidrive"
|
||||||
|
"github.com/Xhofe/alist/conf"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// build tree
|
||||||
|
func BuildTree() error {
|
||||||
|
log.Infof("开始构建目录树...")
|
||||||
|
tx := conf.DB.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if err := tx.Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rootFile := File{
|
||||||
|
Dir: "",
|
||||||
|
FileId: conf.Conf.AliDrive.RootFolder,
|
||||||
|
Name: "root",
|
||||||
|
Type: "folder",
|
||||||
|
}
|
||||||
|
if err := tx.Create(&rootFile).Error; err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := BuildOne(conf.Conf.AliDrive.RootFolder, "root/", tx, ""); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit().Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string) error {
|
||||||
|
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, "", "", "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, file := range files.Items {
|
||||||
|
name := file.Name
|
||||||
|
if strings.HasSuffix(name, ".hide") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
password := parentPassword
|
||||||
|
if strings.Contains(name, ".password-") {
|
||||||
|
index := strings.Index(name, ".password-")
|
||||||
|
name = file.Name[:index]
|
||||||
|
password = file.Name[index+10:]
|
||||||
|
}
|
||||||
|
newFile := File{
|
||||||
|
Dir: path,
|
||||||
|
FileExtension: file.FileExtension,
|
||||||
|
FileId: file.FileId,
|
||||||
|
Name: name,
|
||||||
|
Type: file.Type,
|
||||||
|
UpdatedAt: file.UpdatedAt,
|
||||||
|
Category: file.Category,
|
||||||
|
ContentType: file.ContentType,
|
||||||
|
Size: file.Size,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
log.Debugf("插入file:%+v", newFile)
|
||||||
|
if err := tx.Create(&newFile).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if file.Type == "folder" {
|
||||||
|
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, name), tx, password); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
61
server/models/file.go
Normal file
61
server/models/file.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/Xhofe/alist/conf"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type File struct {
|
||||||
|
Dir string `json:"dir" gorm:"index"`
|
||||||
|
FileExtension string `json:"file_extension"`
|
||||||
|
FileId string `json:"file_id"`
|
||||||
|
Name string `json:"name" gorm:"index"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
UpdatedAt *time.Time `json:"updated_at"`
|
||||||
|
Category string `json:"category"`
|
||||||
|
ContentType string `json:"content_type"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Url string `json:"url" gorm:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (file *File) Create() error {
|
||||||
|
return conf.DB.Create(file).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func Clear() error {
|
||||||
|
return conf.DB.Where("1 = 1").Delete(&File{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFileByDirAndName(dir, name string) (*File, error) {
|
||||||
|
var file File
|
||||||
|
if err := conf.DB.Where("dir = ? AND name = ?", dir, name).First(&file).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &file, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFilesByDir(dir string) (*[]File, error) {
|
||||||
|
var files []File
|
||||||
|
if err := conf.DB.Where("dir = ?", dir).Find(&files).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &files, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SearchByNameGlobal(keyword string) (*[]File, error) {
|
||||||
|
var files []File
|
||||||
|
if err := conf.DB.Where("name LIKE ? AND password = ''", fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &files, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SearchByNameInDir(keyword string, dir string) (*[]File, error) {
|
||||||
|
var files []File
|
||||||
|
if err := conf.DB.Where("dir LIKE ? AND name LIKE ? AND password = ''", fmt.Sprintf("%s%%", dir), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &files, nil
|
||||||
|
}
|
@ -21,14 +21,15 @@ func InitRouter(engine *gin.Engine) {
|
|||||||
|
|
||||||
// init api router
|
// init api router
|
||||||
func InitApiRouter(engine *gin.Engine) {
|
func InitApiRouter(engine *gin.Engine) {
|
||||||
v2:=engine.Group("/api")
|
apiV2 := engine.Group("/api")
|
||||||
{
|
{
|
||||||
v2.GET("/info",controllers.Info)
|
apiV2.GET("/info", controllers.Info)
|
||||||
v2.POST("/get",controllers.Get)
|
apiV2.POST("/get", controllers.Get)
|
||||||
v2.POST("/list",controllers.List)
|
apiV2.POST("/path", controllers.Path)
|
||||||
v2.POST("/search",controllers.Search)
|
apiV2.POST("/office_preview", controllers.OfficePreview)
|
||||||
v2.POST("/office_preview",controllers.OfficePreview)
|
apiV2.POST("/local_search", controllers.LocalSearch)
|
||||||
|
apiV2.POST("/global_search", controllers.GlobalSearch)
|
||||||
|
apiV2.GET("/rebuild/*password", controllers.RebuildTree)
|
||||||
}
|
}
|
||||||
engine.GET("/d/*file_id",controllers.Down)
|
engine.GET("/d/*path", controllers.Down)
|
||||||
engine.GET("/cache/:password",controllers.RefreshCache)
|
|
||||||
}
|
}
|
@ -2,6 +2,7 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -11,3 +12,16 @@ func TestSplit(t *testing.T) {
|
|||||||
strs := strings.Split(drive_id, "/")
|
strs := strings.Split(drive_id, "/")
|
||||||
fmt.Println(strs)
|
fmt.Println(strs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPassword(t *testing.T) {
|
||||||
|
fullName:="hello.password-xhf"
|
||||||
|
index:=strings.Index(fullName,".password-")
|
||||||
|
name:=fullName[:index]
|
||||||
|
password:=fullName[index+10:]
|
||||||
|
fmt.Printf("name:%s, password:%s\n",name,password)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDir(t *testing.T) {
|
||||||
|
dir,file:=filepath.Split("root")
|
||||||
|
fmt.Printf("dir:%s\nfile:%s\n",dir,file)
|
||||||
|
}
|
@ -16,3 +16,4 @@ func TestWriteYml(t *testing.T) {
|
|||||||
alidrive.RefreshToken()
|
alidrive.RefreshToken()
|
||||||
utils.WriteToYml("../conf.yml", conf.Conf)
|
utils.WriteToYml("../conf.yml", conf.Conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
57
utils/common.go
Normal file
57
utils/common.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// copy interface val
|
||||||
|
func SimpleCopyProperties(dst, src interface{}) (err error) {
|
||||||
|
// 防止意外panic
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
err = fmt.Errorf("%v", e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
dstType, dstValue := reflect.TypeOf(dst), reflect.ValueOf(dst)
|
||||||
|
srcType, srcValue := reflect.TypeOf(src), reflect.ValueOf(src)
|
||||||
|
|
||||||
|
// dst必须结构体指针类型
|
||||||
|
if dstType.Kind() != reflect.Ptr || dstType.Elem().Kind() != reflect.Struct {
|
||||||
|
return errors.New("dst type should be a struct pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
// src必须为结构体或者结构体指针,.Elem()类似于*ptr的操作返回指针指向的地址反射类型
|
||||||
|
if srcType.Kind() == reflect.Ptr {
|
||||||
|
srcType, srcValue = srcType.Elem(), srcValue.Elem()
|
||||||
|
}
|
||||||
|
if srcType.Kind() != reflect.Struct {
|
||||||
|
return errors.New("src type should be a struct or a struct pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 取具体内容
|
||||||
|
dstType, dstValue = dstType.Elem(), dstValue.Elem()
|
||||||
|
|
||||||
|
// 属性个数
|
||||||
|
propertyNums := dstType.NumField()
|
||||||
|
|
||||||
|
for i := 0; i < propertyNums; i++ {
|
||||||
|
// 属性
|
||||||
|
property := dstType.Field(i)
|
||||||
|
// 待填充属性值
|
||||||
|
propertyValue := srcValue.FieldByName(property.Name)
|
||||||
|
|
||||||
|
// 无效,说明src没有这个属性 || 属性同名但类型不同
|
||||||
|
if !propertyValue.IsValid() || property.Type != propertyValue.Type() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if dstValue.Field(i).CanSet() {
|
||||||
|
dstValue.Field(i).Set(propertyValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user