diff --git a/README.md b/README.md
index 187d1767..bcb1c0b3 100644
--- a/README.md
+++ b/README.md
@@ -2,12 +2,12 @@
-
-
-
-
+
+
+
+
-
+
@@ -23,7 +23,9 @@
### 演示地址
-- https://alist.nn.ci
+- https://alist.nn.ci (稳定版本)
+- https://alist.now.sh (开发版本)
+- https://alist-plyr.now.sh (plyr分支版本)
### 预览
diff --git a/alidrive/auth.go b/alidrive/auth.go
index 46e0514e..3f0e18f4 100644
--- a/alidrive/auth.go
+++ b/alidrive/auth.go
@@ -8,6 +8,7 @@ import (
log "github.com/sirupsen/logrus"
)
+// use token login
func TokenLogin() (*TokenLoginResp, error) {
log.Infof("尝试使用token登录...")
url:="https://auth.aliyundrive.com/v2/oauth/token_login"
@@ -29,6 +30,7 @@ func TokenLogin() (*TokenLoginResp, error) {
return nil,fmt.Errorf("登录token失效,请更换:%s",tokenLogin.Message)
}
+// get access token
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
log.Infof("获取API token...")
url:="https://websv.aliyundrive.com/token/get"
@@ -51,6 +53,7 @@ func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
return &token,nil
}
+// refresh access_token token by refresh_token
func RefreshToken() bool {
log.Infof("刷新token...")
url:="https://websv.aliyundrive.com/token/refresh"
diff --git a/alidrive/req_bean.go b/alidrive/req_bean.go
index 2028d3bd..847aaf25 100644
--- a/alidrive/req_bean.go
+++ b/alidrive/req_bean.go
@@ -1,5 +1,6 @@
package alidrive
+// list request bean
type ListReq struct {
DriveId string `json:"drive_id"`
Fields string `json:"fields"`
@@ -13,6 +14,7 @@ type ListReq struct {
VideoThumbnailProcess string `json:"video_thumbnail_process"`
}
+// get request bean
type GetReq struct {
DriveId string `json:"drive_id"`
FileId string `json:"file_id"`
@@ -20,6 +22,7 @@ type GetReq struct {
VideoThumbnailProcess string `json:"video_thumbnail_process"`
}
+// search request bean
type SearchReq struct {
DriveId string `json:"drive_id"`
ImageThumbnailProcess string `json:"image_thumbnail_process"`
@@ -33,18 +36,22 @@ type SearchReq struct {
VideoThumbnailProcess string `json:"video_thumbnail_process"`
}
+// token_login request bean
type TokenLoginReq struct {
Token string `json:"token"`
}
+// get_token request bean
type GetTokenReq struct {
Code string `json:"code"`
}
+// refresh_token request bean
type RefreshTokenReq struct {
RefreshToken string `json:"refresh_token"`
}
+// office_preview_url request bean
type OfficePreviewUrlReq struct {
AccessToken string `json:"access_token"`
DriveId string `json:"drive_id"`
diff --git a/alidrive/request.go b/alidrive/request.go
index bcc55e91..3a90db0e 100644
--- a/alidrive/request.go
+++ b/alidrive/request.go
@@ -12,6 +12,7 @@ import (
"time"
)
+// get file
func GetFile(fileId string) (*File, error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/get"
req:=GetReq{
@@ -27,6 +28,7 @@ func GetFile(fileId string) (*File, error) {
return &resp,nil
}
+// search by keyword
func Search(key string,limit int, marker string) (*Files, error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/search"
req:=SearchReq{
@@ -46,10 +48,12 @@ func Search(key string,limit int, marker string) (*Files, error) {
return &resp,nil
}
+// get root folder
func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
return GetList(conf.Conf.AliDrive.RootFolder,limit,marker,orderBy,orderDirection)
}
+// get folder list by file_id
func GetList(parent string,limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/list"
req:=ListReq{
@@ -71,6 +75,7 @@ func GetList(parent string,limit int,marker string,orderBy string,orderDirection
return &resp,nil
}
+// get user info
func GetUserInfo() (*UserInfo,error) {
url:=conf.Conf.AliDrive.ApiUrl+"/user/get"
var resp UserInfo
@@ -80,6 +85,7 @@ func GetUserInfo() (*UserInfo,error) {
return &resp,nil
}
+// get office preview url and token
func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp,error) {
url:=conf.Conf.AliDrive.ApiUrl+"/file/get_office_preview_url"
req:=OfficePreviewUrlReq{
@@ -94,6 +100,7 @@ func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp,error) {
return &resp,nil
}
+// convert body to json
func BodyToJson(url string, req interface{}, resp RespHandle,auth bool) error {
if body,err := DoPost(url,req,auth);err!=nil {
log.Errorf("doPost出错:%s",err.Error())
@@ -116,6 +123,7 @@ func BodyToJson(url string, req interface{}, resp RespHandle,auth bool) error {
return fmt.Errorf(resp.GetMessage())
}
+// do post request
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
var(
resp *http.Response
diff --git a/alidrive/resp_bean.go b/alidrive/resp_bean.go
index 00b6ab5c..5f787fd3 100644
--- a/alidrive/resp_bean.go
+++ b/alidrive/resp_bean.go
@@ -8,13 +8,15 @@ import (
"time"
)
+// response bean methods
type RespHandle interface {
- IsAvailable() bool
- GetCode() string
- GetMessage() string
- SetCode(code string)
+ IsAvailable() bool // check available
+ GetCode() string // get err code
+ GetMessage() string // get err message
+ SetCode(code string) // set err code
}
+// common response bean
type RespError struct {
Code string `json:"code"`
Message string `json:"message"`
@@ -36,6 +38,7 @@ func (resp *RespError)SetCode(code string) {
resp.Code=code
}
+// user_info response bean
type UserInfo struct {
RespError
DomainId string `json:"domain_id"`
@@ -54,19 +57,22 @@ type UserInfo struct {
UserData map[string]interface{} `json:"user_data"`
}
+// folder files response bean
type Files struct {
RespError
Items []File `json:"items"`
NextMarker string `json:"next_marker"`
- Readme string `json:"readme"`
+ Readme string `json:"readme"` // Deprecated
Paths []Path `json:"paths"`
}
+// path bean
type Path struct {
Name string `json:"name"`
FileId string `json:"file_id"`
}
+// file response bean
type File struct {
RespError
DriveId string `json:"drive_id"`
@@ -98,11 +104,13 @@ type File struct {
Paths []Path `json:"paths"`
}
+// token_login response bean
type TokenLoginResp struct {
RespError
Goto string `json:"goto"`
}
+// token response bean
type TokenResp struct {
RespError
AccessToken string `json:"access_token"`
@@ -123,12 +131,14 @@ type TokenResp struct {
DeviceId string `json:"device_id"`
}
+// office_preview_url response bean
type OfficePreviewUrlResp struct {
RespError
PreviewUrl string `json:"preview_url"`
AccessToken string `json:"access_token"`
}
+// check password
func HasPassword(files *Files) string {
fileList := files.Items
for i, file := range fileList {
@@ -140,6 +150,7 @@ func HasPassword(files *Files) string {
return ""
}
+// Deprecated: check readme, implemented by the front end now
func HasReadme(files *Files) string {
fileList := files.Items
for _, file := range fileList {
diff --git a/alist.go b/alist.go
index 07572821..50ece8f6 100644
--- a/alist.go
+++ b/alist.go
@@ -2,6 +2,7 @@ package main
import "github.com/Xhofe/alist/bootstrap"
+// main function
func main() {
bootstrap.Run()
}
\ No newline at end of file
diff --git a/bootstrap/alidrive.go b/bootstrap/alidrive.go
index dde49eff..be9728d0 100644
--- a/bootstrap/alidrive.go
+++ b/bootstrap/alidrive.go
@@ -6,6 +6,7 @@ import (
log "github.com/sirupsen/logrus"
)
+// init aliyun drive
func InitAliDrive() bool {
log.Infof("初始化阿里云盘...")
//首先token_login
diff --git a/bootstrap/cache.go b/bootstrap/cache.go
index 94a02f6b..1546e55f 100644
--- a/bootstrap/cache.go
+++ b/bootstrap/cache.go
@@ -7,6 +7,7 @@ import (
"time"
)
+// init cache
func InitCache() {
if conf.Conf.Cache.Enable {
log.Infof("初始化缓存...")
diff --git a/bootstrap/client.go b/bootstrap/client.go
index 4cf536bb..fd76e1b0 100644
--- a/bootstrap/client.go
+++ b/bootstrap/client.go
@@ -6,6 +6,7 @@ import (
"net/http"
)
+// init request client
func InitClient() {
log.Infof("初始化client...")
conf.Client=&http.Client{}
diff --git a/bootstrap/cmd.go b/bootstrap/cmd.go
index a0d4360a..8a82621a 100644
--- a/bootstrap/cmd.go
+++ b/bootstrap/cmd.go
@@ -16,6 +16,7 @@ func init() {
flag.StringVar(&conf.Con,"conf","conf.yml","config file")
}
+// bootstrap run
func Run() {
flag.Parse()
if conf.Help {
@@ -29,6 +30,7 @@ func Run() {
start()
}
+// print asc
func printASC() {
log.Info(`
________ ___ ___ ________ _________
@@ -42,6 +44,7 @@ func printASC() {
`)
}
+// start server
func start() {
InitLog()
printASC()
@@ -60,6 +63,7 @@ func start() {
server()
}
+// start http server
func server() {
baseServer:="0.0.0.0:"+conf.Conf.Server.Port
r:=gin.Default()
diff --git a/bootstrap/config.go b/bootstrap/config.go
index 355afbe6..c14aa627 100644
--- a/bootstrap/config.go
+++ b/bootstrap/config.go
@@ -9,6 +9,7 @@ import (
"strings"
)
+// read config file
func ReadConf(config string) bool {
log.Infof("读取配置文件...")
if !utils.Exists(config) {
diff --git a/bootstrap/cron.go b/bootstrap/cron.go
index e503ba99..a2ae1e45 100644
--- a/bootstrap/cron.go
+++ b/bootstrap/cron.go
@@ -8,10 +8,12 @@ import (
var Cron *cron.Cron
+// refresh token func for cron
func refreshToken() {
alidrive.RefreshToken()
}
+// init cron jobs
func InitCron() {
log.Infof("初始化定时任务:刷新token")
Cron=cron.New()
diff --git a/bootstrap/log.go b/bootstrap/log.go
index 20885dd0..f0c350b8 100644
--- a/bootstrap/log.go
+++ b/bootstrap/log.go
@@ -6,6 +6,7 @@ import (
log "github.com/sirupsen/logrus"
)
+// init logrus
func InitLog() {
if conf.Debug {
log.SetLevel(log.DebugLevel)
diff --git a/bootstrap/update.go b/bootstrap/update.go
index 33ee07b7..6905c797 100644
--- a/bootstrap/update.go
+++ b/bootstrap/update.go
@@ -9,12 +9,14 @@ import (
"net/http"
)
+// github release response bean
type GithubRelease struct {
TagName string `json:"tag_name"`
HtmlUrl string `json:"html_url"`
Body string `json:"body"`
}
+// check updtae
func CheckUpdate() {
log.Infof("检查更新...")
url:="https://api.github.com/repos/Xhofe/alist/releases/latest"
diff --git a/conf/config.go b/conf/config.go
index e7feb462..70a2d65e 100644
--- a/conf/config.go
+++ b/conf/config.go
@@ -1,5 +1,6 @@
package conf
+// config struct
type Config struct {
Info struct{
Title string `yaml:"title" json:"title"`
diff --git a/conf/const.go b/conf/const.go
index 837239d1..1547602c 100644
--- a/conf/const.go
+++ b/conf/const.go
@@ -6,22 +6,22 @@ import (
)
var(
- Debug bool
- Help bool
- Version bool
- Con string
- Client *http.Client
- Authorization string
+ Debug bool // is debug command
+ Help bool // is help command
+ Version bool // is print version command
+ Con string // config file
+ Client *http.Client // request client
+ Authorization string // authorization string
- Cache *cache.Cache
+ Cache *cache.Cache // cache
- Origins []string
+ Origins []string // allow origins
)
var Conf = new(Config)
const (
- VERSION="v0.1.6"
+ VERSION="v0.1.7"
ImageThumbnailProcess="image/resize,w_50"
VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50"
diff --git a/server/controllers/common.go b/server/controllers/common.go
index 37eac38a..8ce50beb 100644
--- a/server/controllers/common.go
+++ b/server/controllers/common.go
@@ -2,6 +2,7 @@ package controllers
import "github.com/gin-gonic/gin"
+// common meta response
func MetaResponse(code int, msg string) gin.H {
return gin.H{
"meta":gin.H{
@@ -11,6 +12,7 @@ func MetaResponse(code int, msg string) gin.H {
}
}
+// common data response
func DataResponse(data interface{}) gin.H {
return gin.H{
"meta":gin.H{
diff --git a/server/controllers/get.go b/server/controllers/get.go
index d9744794..34cd2810 100644
--- a/server/controllers/get.go
+++ b/server/controllers/get.go
@@ -7,8 +7,8 @@ import (
"strings"
)
+// handle get request
// 因为下载地址有时效,所以去掉了文件请求和直链的缓存
-
func Get(c *gin.Context) {
var get alidrive.GetReq
if err := c.ShouldBindJSON(&get); err != nil {
diff --git a/server/controllers/list.go b/server/controllers/list.go
index 2099841c..0282d8a7 100644
--- a/server/controllers/list.go
+++ b/server/controllers/list.go
@@ -9,11 +9,13 @@ import (
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 {
@@ -65,7 +67,7 @@ func List(c *gin.Context) {
return
}
files.Paths=*paths
- files.Readme=alidrive.HasReadme(files)
+ //files.Readme=alidrive.HasReadme(files)
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
}
diff --git a/server/controllers/offie_preview.go b/server/controllers/offie_preview.go
index 1afe7290..303aad65 100644
--- a/server/controllers/offie_preview.go
+++ b/server/controllers/offie_preview.go
@@ -6,6 +6,7 @@ import (
log "github.com/sirupsen/logrus"
)
+// handle office_preview request
func OfficePreview(c *gin.Context) {
var req alidrive.OfficePreviewUrlReq
if err := c.ShouldBindJSON(&req); err != nil {
diff --git a/server/controllers/search.go b/server/controllers/search.go
index f343a1b8..c9999cd0 100644
--- a/server/controllers/search.go
+++ b/server/controllers/search.go
@@ -9,6 +9,7 @@ import (
log "github.com/sirupsen/logrus"
)
+// handle search request
func Search(c *gin.Context) {
if !conf.Conf.Server.Search {
c.JSON(200, MetaResponse(403,"Not allow search."))
diff --git a/server/controllers/utils.go b/server/controllers/utils.go
index 78246b1c..59ae1784 100644
--- a/server/controllers/utils.go
+++ b/server/controllers/utils.go
@@ -5,10 +5,12 @@ import (
"github.com/gin-gonic/gin"
)
+// handle info request
func Info(c *gin.Context) {
c.JSON(200, DataResponse(conf.Conf.Info))
}
+// handle refresh_cache request
func RefreshCache(c *gin.Context) {
password:=c.Param("password")
if conf.Conf.Cache.Enable {
diff --git a/server/middlewares.go b/server/middlewares.go
index c3399594..612fd828 100644
--- a/server/middlewares.go
+++ b/server/middlewares.go
@@ -7,7 +7,8 @@ import (
"github.com/gin-gonic/gin"
)
-func CrosHandler() gin.HandlerFunc {
+// handle cors request
+func CorsHandler() gin.HandlerFunc {
return func(context *gin.Context) {
origin:=context.GetHeader("Origin")
// 同源
diff --git a/server/router.go b/server/router.go
index 25898e36..57261c2b 100644
--- a/server/router.go
+++ b/server/router.go
@@ -8,17 +8,19 @@ import (
log "github.com/sirupsen/logrus"
)
+// init router
func InitRouter(engine *gin.Engine) {
log.Infof("初始化路由...")
- engine.Use(CrosHandler())
- InitApiRouter(engine)
-}
-
-func InitApiRouter(engine *gin.Engine) {
+ engine.Use(CorsHandler())
engine.Use(static.Serve("/",static.LocalFile(conf.Conf.Server.Static,false)))
engine.NoRoute(func(c *gin.Context) {
c.File(conf.Conf.Server.Static+"/index.html")
})
+ InitApiRouter(engine)
+}
+
+// init api router
+func InitApiRouter(engine *gin.Engine) {
v2:=engine.Group("/api")
{
v2.GET("/info",controllers.Info)
diff --git a/utils/check.go b/utils/check.go
index f676501e..6e8fcd9b 100644
--- a/utils/check.go
+++ b/utils/check.go
@@ -7,6 +7,7 @@ import (
"strings"
)
+// get code from url
func GetCode(rawUrl string) string {
u,err:=url.Parse(rawUrl)
if err!=nil {
@@ -17,6 +18,7 @@ func GetCode(rawUrl string) string {
return code
}
+// determine whether to include
func ContainsString(array []string, val string) (index int) {
index = -1
for i := 0; i < len(array); i++ {
@@ -28,6 +30,7 @@ func ContainsString(array []string, val string) (index int) {
return
}
+// compare version
func VersionCompare(version1, version2 string) int {
a := strings.Split(version1, ".")
b := strings.Split(version2, ".")
diff --git a/utils/file.go b/utils/file.go
index 846a7f90..5d19e493 100644
--- a/utils/file.go
+++ b/utils/file.go
@@ -8,6 +8,7 @@ import (
"path/filepath"
)
+// determine whether the file exists
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
@@ -17,6 +18,7 @@ func Exists(name string) bool {
return true
}
+// 嵌套创建文件
func CreatNestedFile(path string) (*os.File, error) {
basePath := filepath.Dir(path)
if !Exists(basePath) {
@@ -29,6 +31,7 @@ func CreatNestedFile(path string) (*os.File, error) {
return os.Create(path)
}
+// write struct to yaml file
func WriteToYml(src string,conf interface{}){
data,err := yaml.Marshal(conf)
if err!=nil {