-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
270 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"github.com/Xhofe/alist/conf" | ||
"github.com/patrickmn/go-cache" | ||
log "github.com/sirupsen/logrus" | ||
"time" | ||
) | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ func start() { | |
log.Errorf("初始化阿里云盘出现错误,启动失败.") | ||
return | ||
} | ||
InitCache() | ||
InitCron() | ||
server() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
info: | ||
title: AList | ||
site_url: http://localhost | ||
logo: | ||
title: AList #标题 | ||
site_url: http://localhost:5277 #前端地址 | ||
backend_url: http://localhost:5244 #后端地址 | ||
logo: "" #网站logo 如果填写,则会替换掉默认的 | ||
footer_text: Xhofe's Blog #网页底部文字 | ||
footer_url: https://www.nn.ci #网页底部文字链接 | ||
music_img: https://img.oez.cc/2020/12/19/0f8b57866bdb5.gif #预览音乐文件时的图片 | ||
server: | ||
port: "5244" | ||
search: true | ||
static: dist | ||
port: "5244" #程序监听端口 | ||
search: true #是否开启搜索接口,开启搜索之后密码和根目录都会失效,所以前端暂时不做搜索 | ||
static: dist #前端文件目录 | ||
cache: | ||
cache: true #是否开启缓存 | ||
expiration: 5 #缓存失效时间(单位:分钟) | ||
cleanup_interval: 10 #清理失效缓存间隔 | ||
refresh_password: password #手动清理缓存密码 | ||
ali_drive: | ||
api_url: https://api.aliyundrive.com/v2 | ||
root_folder: root | ||
token: | ||
access_token: | ||
api_url: https://api.aliyundrive.com/v2 #阿里云盘api,无需修改 | ||
root_folder: root #根目录 | ||
refresh_token: need | ||
max_files_count: 3000 | ||
max_files_count: 3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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" | ||
"strings" | ||
) | ||
|
||
func Get(c *gin.Context) { | ||
var get alidrive.GetReq | ||
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 | ||
if conf.Conf.Cache.Enable { | ||
conf.Cache.Set(cacheKey,file,cache.DefaultExpiration) | ||
} | ||
c.JSON(200,dataResponse(file)) | ||
} | ||
|
||
func Down(c *gin.Context) { | ||
fileIdParam:=c.Param("file_id") | ||
log.Debugf("down:%s",fileIdParam) | ||
fileId:=strings.Split(fileIdParam,"/")[1] | ||
cacheKey:=fmt.Sprintf("%s-%s","d",fileId) | ||
if conf.Conf.Cache.Enable { | ||
downloadUrl,exist:=conf.Cache.Get(cacheKey) | ||
if exist { | ||
log.Debugf("使用了缓存:%s",cacheKey) | ||
c.Redirect(301,downloadUrl.(string)) | ||
return | ||
} | ||
} | ||
file,err:=alidrive.GetFile(fileId) | ||
if err != nil { | ||
c.JSON(200, metaResponse(500,err.Error())) | ||
return | ||
} | ||
if conf.Conf.Cache.Enable { | ||
conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration) | ||
} | ||
c.Redirect(301,file.DownloadUrl) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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" | ||
) | ||
|
||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package server | ||
package controllers | ||
|
||
import "github.com/Xhofe/alist/alidrive" | ||
|
||
|
Oops, something went wrong.