Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
9cb548d4f7 | |||
0e7083a713 | |||
46f09836f3 | |||
e146054679 | |||
3b2a729dc6 | |||
d18a752732 | |||
b9676182c9 | |||
434eb25408 | |||
858291876b | |||
dcebf5257f |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: Build
|
name: build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: Release
|
name: release
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
|
14
README.md
14
README.md
@ -2,12 +2,12 @@
|
|||||||
<img src="https://img.oez.cc/2020/12/24/1fb16bc25a4f6.png" alt="AList Logo" width=200/>
|
<img src="https://img.oez.cc/2020/12/24/1fb16bc25a4f6.png" alt="AList Logo" width=200/>
|
||||||
</p>
|
</p>
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/release/Xhofe/alist" alt="Release version"></a>
|
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/release/Xhofe/alist?style=flat-square" alt="Release version"></a>
|
||||||
<a href="https://github.com/Xhofe/alist/actions?query=workflow%3ABuild"><img src="https://github.com/Xhofe/alist/workflows/Build/badge.svg" alt="Build status"></a>
|
<a href="https://github.com/Xhofe/alist/actions?query=workflow%3ABuild"><img src="https://img.shields.io/github/workflow/status/Xhofe/alist/build?style=flat-square" alt="Build status"></a>
|
||||||
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/downloads/Xhofe/alist/latest/total" alt="Downloads"></a>
|
<a href="https://github.com/Xhofe/alist/releases"><img src="https://img.shields.io/github/downloads/Xhofe/alist/total?style=flat-square" alt="Downloads"></a>
|
||||||
<a href="https://github.com/Xhofe/alist/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Xhofe/alist" alt="License"></a>
|
<a href="https://github.com/Xhofe/alist/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Xhofe/alist?style=flat-square" alt="License"></a>
|
||||||
<a href="https://pay.xhofe.top">
|
<a href="https://pay.xhofe.top">
|
||||||
<img src="https://img.shields.io/badge/%24-donate-ff69b4.svg" alt="donate">
|
<img src="https://img.shields.io/badge/%24-donate-ff69b4.svg?style=flat-square" alt="donate">
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
@ -23,7 +23,9 @@
|
|||||||
|
|
||||||
### 演示地址
|
### 演示地址
|
||||||
|
|
||||||
- https://alist.nn.ci
|
- https://alist.nn.ci (稳定版本)
|
||||||
|
- https://alist.now.sh (开发版本)
|
||||||
|
- https://alist-plyr.now.sh (plyr分支版本)
|
||||||
|
|
||||||
### 预览
|
### 预览
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// use token login
|
||||||
func TokenLogin() (*TokenLoginResp, error) {
|
func TokenLogin() (*TokenLoginResp, error) {
|
||||||
log.Infof("尝试使用token登录...")
|
log.Infof("尝试使用token登录...")
|
||||||
url:="https://auth.aliyundrive.com/v2/oauth/token_login"
|
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)
|
return nil,fmt.Errorf("登录token失效,请更换:%s",tokenLogin.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get access token
|
||||||
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
|
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
|
||||||
log.Infof("获取API token...")
|
log.Infof("获取API token...")
|
||||||
url:="https://websv.aliyundrive.com/token/get"
|
url:="https://websv.aliyundrive.com/token/get"
|
||||||
@ -51,6 +53,7 @@ func GetToken(tokenLogin *TokenLoginResp) (*TokenResp,error) {
|
|||||||
return &token,nil
|
return &token,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// refresh access_token token by refresh_token
|
||||||
func RefreshToken() bool {
|
func RefreshToken() bool {
|
||||||
log.Infof("刷新token...")
|
log.Infof("刷新token...")
|
||||||
url:="https://websv.aliyundrive.com/token/refresh"
|
url:="https://websv.aliyundrive.com/token/refresh"
|
||||||
|
@ -1,46 +1,67 @@
|
|||||||
package alidrive
|
package alidrive
|
||||||
|
|
||||||
|
// list request bean
|
||||||
type ListReq struct {
|
type ListReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
Fields string `json:"fields"`
|
Fields string `json:"fields"`
|
||||||
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
||||||
ImageUrlProcess string `json:"image_url_process"`
|
ImageUrlProcess string `json:"image_url_process"`
|
||||||
Limit int `json:"limit"`
|
Limit int `json:"limit"`
|
||||||
Marker string `json:"marker"`
|
Marker string `json:"marker"`
|
||||||
OrderBy string `json:"order_by"`
|
OrderBy string `json:"order_by"`
|
||||||
OrderDirection string `json:"order_direction"`
|
OrderDirection string `json:"order_direction"`
|
||||||
ParentFileId string `json:"parent_file_id"`
|
ParentFileId string `json:"parent_file_id"`
|
||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get request bean
|
||||||
type GetReq struct {
|
type GetReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// download request bean
|
||||||
|
type DownloadReq struct {
|
||||||
|
DriveId string `json:"drive_id"`
|
||||||
|
FileId string `json:"file_id"`
|
||||||
|
ExpireSec int `json:"expire_sec"`
|
||||||
|
FileName string `json:"file_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// search request bean
|
||||||
type SearchReq struct {
|
type SearchReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
ImageThumbnailProcess string `json:"image_thumbnail_process"`
|
||||||
ImageUrlProcess string `json:"image_url_process"`
|
ImageUrlProcess string `json:"image_url_process"`
|
||||||
Limit int `json:"limit"`
|
Limit int `json:"limit"`
|
||||||
Marker string `json:"marker"`
|
Marker string `json:"marker"`
|
||||||
OrderBy string `json:"order_by"`//"type ASC,updated_at DESC"
|
OrderBy string `json:"order_by"` //"type ASC,updated_at DESC"
|
||||||
|
|
||||||
Query string `json:"query"`// "name match '测试文件'"
|
Query string `json:"query"` // "name match '测试文件'"
|
||||||
|
|
||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// token_login request bean
|
||||||
type TokenLoginReq struct {
|
type TokenLoginReq struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get_token request bean
|
||||||
type GetTokenReq struct {
|
type GetTokenReq struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// refresh_token request bean
|
||||||
type RefreshTokenReq struct {
|
type RefreshTokenReq struct {
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// office_preview_url request bean
|
||||||
|
type OfficePreviewUrlReq struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
DriveId string `json:"drive_id"`
|
||||||
|
FileId string `json:"file_id"`
|
||||||
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// get file
|
||||||
func GetFile(fileId string) (*File, error) {
|
func GetFile(fileId string) (*File, error) {
|
||||||
url:=conf.Conf.AliDrive.ApiUrl+"/file/get"
|
url:=conf.Conf.AliDrive.ApiUrl+"/file/get"
|
||||||
req:=GetReq{
|
req:=GetReq{
|
||||||
@ -20,27 +21,29 @@ func GetFile(fileId string) (*File, error) {
|
|||||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var file File
|
var resp File
|
||||||
if body, err := DoPost(url, req,true); err != nil {
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
log.Errorf("doPost出错:%s",err.Error())
|
|
||||||
return nil,err
|
return nil,err
|
||||||
}else {
|
|
||||||
if err = json.Unmarshal(body,&file);err !=nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
|
||||||
return nil,err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if file.IsAvailable() {
|
return &resp,nil
|
||||||
return &file,nil
|
|
||||||
}
|
|
||||||
if file.Code==conf.AccessTokenInvalid {
|
|
||||||
if RefreshToken() {
|
|
||||||
return GetFile(fileId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil,fmt.Errorf(file.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get download_url
|
||||||
|
func GetDownLoadUrl(fileId string) (*DownloadResp, error) {
|
||||||
|
url:=conf.Conf.AliDrive.ApiUrl+"/file/get_download_url"
|
||||||
|
req:=DownloadReq{
|
||||||
|
DriveId: User.DefaultDriveId,
|
||||||
|
FileId: fileId,
|
||||||
|
ExpireSec: 14400,
|
||||||
|
}
|
||||||
|
var resp DownloadResp
|
||||||
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
|
return nil,err
|
||||||
|
}
|
||||||
|
return &resp,nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// search by keyword
|
||||||
func Search(key string,limit int, marker string) (*Files, error) {
|
func Search(key string,limit int, marker string) (*Files, error) {
|
||||||
url:=conf.Conf.AliDrive.ApiUrl+"/file/search"
|
url:=conf.Conf.AliDrive.ApiUrl+"/file/search"
|
||||||
req:=SearchReq{
|
req:=SearchReq{
|
||||||
@ -53,31 +56,19 @@ func Search(key string,limit int, marker string) (*Files, error) {
|
|||||||
Query: fmt.Sprintf("name match '%s'",key),
|
Query: fmt.Sprintf("name match '%s'",key),
|
||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var files Files
|
var resp Files
|
||||||
if body, err := DoPost(url, req,true); err != nil {
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
log.Errorf("doPost出错:%s",err.Error())
|
|
||||||
return nil,err
|
return nil,err
|
||||||
}else {
|
|
||||||
if err = json.Unmarshal(body,&files);err !=nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
|
||||||
return nil,err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if files.IsAvailable() {
|
return &resp,nil
|
||||||
return &files,nil
|
|
||||||
}
|
|
||||||
if files.Code==conf.AccessTokenInvalid {
|
|
||||||
if RefreshToken() {
|
|
||||||
return Search(key,limit,marker)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil,fmt.Errorf(files.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get root folder
|
||||||
func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
|
func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
|
||||||
return GetList(conf.Conf.AliDrive.RootFolder,limit,marker,orderBy,orderDirection)
|
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) {
|
func GetList(parent string,limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
|
||||||
url:=conf.Conf.AliDrive.ApiUrl+"/file/list"
|
url:=conf.Conf.AliDrive.ApiUrl+"/file/list"
|
||||||
req:=ListReq{
|
req:=ListReq{
|
||||||
@ -92,50 +83,62 @@ func GetList(parent string,limit int,marker string,orderBy string,orderDirection
|
|||||||
ParentFileId: parent,
|
ParentFileId: parent,
|
||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var files Files
|
var resp Files
|
||||||
if body, err := DoPost(url, req,true); err != nil {
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
log.Errorf("doPost出错:%s",err.Error())
|
|
||||||
return nil,err
|
return nil,err
|
||||||
}else {
|
|
||||||
if err = json.Unmarshal(body,&files);err !=nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
|
||||||
return nil,err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if files.IsAvailable() {
|
return &resp,nil
|
||||||
return &files,nil
|
|
||||||
}
|
|
||||||
if files.Code==conf.AccessTokenInvalid {
|
|
||||||
if RefreshToken() {
|
|
||||||
return GetRoot(limit,marker,orderBy,orderDirection)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil,fmt.Errorf(files.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get user info
|
||||||
func GetUserInfo() (*UserInfo,error) {
|
func GetUserInfo() (*UserInfo,error) {
|
||||||
url:=conf.Conf.AliDrive.ApiUrl+"/user/get"
|
url:=conf.Conf.AliDrive.ApiUrl+"/user/get"
|
||||||
var user UserInfo
|
var resp UserInfo
|
||||||
if body, err := DoPost(url, map[string]interface{}{},true); err != nil {
|
if err := BodyToJson(url, map[string]interface{}{}, &resp, true); err!=nil {
|
||||||
log.Errorf("doPost出错:%s",err.Error())
|
|
||||||
return nil,err
|
return nil,err
|
||||||
}else {
|
|
||||||
if err = json.Unmarshal(body,&user);err !=nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
|
||||||
return nil,err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if user.IsAvailable() {
|
return &resp,nil
|
||||||
return &user,nil
|
|
||||||
}
|
|
||||||
if user.Code==conf.AccessTokenInvalid {
|
|
||||||
if RefreshToken() {
|
|
||||||
return GetUserInfo()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil,fmt.Errorf(user.Message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get office preview url and token
|
||||||
|
func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp,error) {
|
||||||
|
url:=conf.Conf.AliDrive.ApiUrl+"/file/get_office_preview_url"
|
||||||
|
req:=OfficePreviewUrlReq{
|
||||||
|
AccessToken: conf.Conf.AliDrive.AccessToken,
|
||||||
|
DriveId: User.DefaultDriveId,
|
||||||
|
FileId: fileId,
|
||||||
|
}
|
||||||
|
var resp OfficePreviewUrlResp
|
||||||
|
if err := BodyToJson(url, req, &resp, true); err!=nil {
|
||||||
|
return nil,err
|
||||||
|
}
|
||||||
|
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())
|
||||||
|
return err
|
||||||
|
}else {
|
||||||
|
if err = json.Unmarshal(body,&resp);err!=nil {
|
||||||
|
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if resp.IsAvailable() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if resp.GetCode() == conf.AccessTokenInvalid {
|
||||||
|
resp.SetCode("")
|
||||||
|
if RefreshToken() {
|
||||||
|
return BodyToJson(url,req,resp,auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf(resp.GetMessage())
|
||||||
|
}
|
||||||
|
|
||||||
|
// do post request
|
||||||
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
|
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
|
||||||
var(
|
var(
|
||||||
resp *http.Response
|
resp *http.Response
|
||||||
@ -198,4 +201,4 @@ func GetPaths(fileId string) (*[]Path,error) {
|
|||||||
FileId: "root",
|
FileId: "root",
|
||||||
})
|
})
|
||||||
return &paths,nil
|
return &paths,nil
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,37 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// response bean methods
|
||||||
|
type RespHandle interface {
|
||||||
|
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 {
|
type RespError struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (resp *RespError) IsAvailable() bool {
|
||||||
|
return resp.Code == ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (resp *RespError) GetCode() string {
|
||||||
|
return resp.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
func (resp *RespError) GetMessage() string {
|
||||||
|
return resp.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
func (resp *RespError) SetCode(code string) {
|
||||||
|
resp.Code = code
|
||||||
|
}
|
||||||
|
|
||||||
|
// user_info response bean
|
||||||
type UserInfo struct {
|
type UserInfo struct {
|
||||||
RespError
|
RespError
|
||||||
DomainId string `json:"domain_id"`
|
DomainId string `json:"domain_id"`
|
||||||
@ -31,19 +57,22 @@ type UserInfo struct {
|
|||||||
UserData map[string]interface{} `json:"user_data"`
|
UserData map[string]interface{} `json:"user_data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// folder files response bean
|
||||||
type Files struct {
|
type Files struct {
|
||||||
RespError
|
RespError
|
||||||
Items []File `json:"items"`
|
Items []File `json:"items"`
|
||||||
NextMarker string `json:"next_marker"`
|
NextMarker string `json:"next_marker"`
|
||||||
Readme string `json:"readme"`
|
Readme string `json:"readme"` // Deprecated
|
||||||
Paths []Path `json:"paths"`
|
Paths []Path `json:"paths"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// path bean
|
||||||
type Path struct {
|
type Path struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// file response bean
|
||||||
type File struct {
|
type File struct {
|
||||||
RespError
|
RespError
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
@ -72,18 +101,28 @@ type File struct {
|
|||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
ImageMediaMetadata map[string]interface{} `json:"image_media_metadata"`
|
ImageMediaMetadata map[string]interface{} `json:"image_media_metadata"`
|
||||||
|
|
||||||
Paths []Path `json:"paths"`
|
Paths []Path `json:"paths"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (resp *RespError) IsAvailable() bool {
|
type DownloadResp struct {
|
||||||
return resp.Code == ""
|
RespError
|
||||||
|
Expiration string `json:"expiration"`
|
||||||
|
Method string `json:"method"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
//RateLimit struct{
|
||||||
|
// PartSize int `json:"part_size"`
|
||||||
|
// PartSpeed int `json:"part_speed"`
|
||||||
|
//} `json:"rate_limit"`//rate limit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// token_login response bean
|
||||||
type TokenLoginResp struct {
|
type TokenLoginResp struct {
|
||||||
RespError
|
RespError
|
||||||
Goto string `json:"goto"`
|
Goto string `json:"goto"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// token response bean
|
||||||
type TokenResp struct {
|
type TokenResp struct {
|
||||||
RespError
|
RespError
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
@ -104,17 +143,26 @@ type TokenResp struct {
|
|||||||
DeviceId string `json:"device_id"`
|
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 {
|
func HasPassword(files *Files) string {
|
||||||
fileList := files.Items
|
fileList := files.Items
|
||||||
for i, file := range fileList {
|
for i, file := range fileList {
|
||||||
if strings.HasPrefix(file.Name, ".password-") {
|
if strings.HasPrefix(file.Name, ".password-") {
|
||||||
files.Items=fileList[:i+copy(fileList[i:],fileList[i+1:])]
|
files.Items = fileList[:i+copy(fileList[i:], fileList[i+1:])]
|
||||||
return file.Name[10:]
|
return file.Name[10:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: check readme, implemented by the front end now
|
||||||
func HasReadme(files *Files) string {
|
func HasReadme(files *Files) string {
|
||||||
fileList := files.Items
|
fileList := files.Items
|
||||||
for _, file := range fileList {
|
for _, file := range fileList {
|
||||||
|
1
alist.go
1
alist.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import "github.com/Xhofe/alist/bootstrap"
|
import "github.com/Xhofe/alist/bootstrap"
|
||||||
|
|
||||||
|
// main function
|
||||||
func main() {
|
func main() {
|
||||||
bootstrap.Run()
|
bootstrap.Run()
|
||||||
}
|
}
|
@ -6,6 +6,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// init aliyun drive
|
||||||
func InitAliDrive() bool {
|
func InitAliDrive() bool {
|
||||||
log.Infof("初始化阿里云盘...")
|
log.Infof("初始化阿里云盘...")
|
||||||
//首先token_login
|
//首先token_login
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// init cache
|
||||||
func InitCache() {
|
func InitCache() {
|
||||||
if conf.Conf.Cache.Enable {
|
if conf.Conf.Cache.Enable {
|
||||||
log.Infof("初始化缓存...")
|
log.Infof("初始化缓存...")
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// init request client
|
||||||
func InitClient() {
|
func InitClient() {
|
||||||
log.Infof("初始化client...")
|
log.Infof("初始化client...")
|
||||||
conf.Client=&http.Client{}
|
conf.Client=&http.Client{}
|
||||||
|
@ -2,6 +2,7 @@ package bootstrap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
serv "github.com/Xhofe/alist/server"
|
serv "github.com/Xhofe/alist/server"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -11,18 +12,26 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
flag.BoolVar(&conf.Debug,"debug",false,"use debug mode")
|
flag.BoolVar(&conf.Debug,"debug",false,"use debug mode")
|
||||||
flag.BoolVar(&conf.Help,"help",false,"show usage help")
|
flag.BoolVar(&conf.Help,"help",false,"show usage help")
|
||||||
|
flag.BoolVar(&conf.Version,"version",false,"show version info")
|
||||||
flag.StringVar(&conf.Con,"conf","conf.yml","config file")
|
flag.StringVar(&conf.Con,"conf","conf.yml","config file")
|
||||||
|
flag.BoolVar(&conf.SkipUpdate,"skip-update",false,"skip update")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bootstrap run
|
||||||
func Run() {
|
func Run() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if conf.Help {
|
if conf.Help {
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if conf.Version {
|
||||||
|
fmt.Println("Current version:"+conf.VERSION)
|
||||||
|
return
|
||||||
|
}
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// print asc
|
||||||
func printASC() {
|
func printASC() {
|
||||||
log.Info(`
|
log.Info(`
|
||||||
________ ___ ___ ________ _________
|
________ ___ ___ ________ _________
|
||||||
@ -36,10 +45,13 @@ func printASC() {
|
|||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start server
|
||||||
func start() {
|
func start() {
|
||||||
InitLog()
|
InitLog()
|
||||||
printASC()
|
printASC()
|
||||||
CheckUpdate()
|
if !conf.SkipUpdate {
|
||||||
|
CheckUpdate()
|
||||||
|
}
|
||||||
if !ReadConf(conf.Con) {
|
if !ReadConf(conf.Con) {
|
||||||
log.Errorf("读取配置文件时出现错误,启动失败.")
|
log.Errorf("读取配置文件时出现错误,启动失败.")
|
||||||
return
|
return
|
||||||
@ -54,6 +66,7 @@ func start() {
|
|||||||
server()
|
server()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start http server
|
||||||
func server() {
|
func server() {
|
||||||
baseServer:="0.0.0.0:"+conf.Conf.Server.Port
|
baseServer:="0.0.0.0:"+conf.Conf.Server.Port
|
||||||
r:=gin.Default()
|
r:=gin.Default()
|
||||||
|
@ -6,8 +6,10 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// read config file
|
||||||
func ReadConf(config string) bool {
|
func ReadConf(config string) bool {
|
||||||
log.Infof("读取配置文件...")
|
log.Infof("读取配置文件...")
|
||||||
if !utils.Exists(config) {
|
if !utils.Exists(config) {
|
||||||
@ -25,5 +27,6 @@ func ReadConf(config string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
log.Debugf("config:%+v",conf.Conf)
|
log.Debugf("config:%+v",conf.Conf)
|
||||||
|
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl,",")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
@ -8,10 +8,12 @@ import (
|
|||||||
|
|
||||||
var Cron *cron.Cron
|
var Cron *cron.Cron
|
||||||
|
|
||||||
|
// refresh token func for cron
|
||||||
func refreshToken() {
|
func refreshToken() {
|
||||||
alidrive.RefreshToken()
|
alidrive.RefreshToken()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// init cron jobs
|
||||||
func InitCron() {
|
func InitCron() {
|
||||||
log.Infof("初始化定时任务:刷新token")
|
log.Infof("初始化定时任务:刷新token")
|
||||||
Cron=cron.New()
|
Cron=cron.New()
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// init logrus
|
||||||
func InitLog() {
|
func InitLog() {
|
||||||
if conf.Debug {
|
if conf.Debug {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
|
@ -3,18 +3,22 @@ package bootstrap
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// github release response bean
|
||||||
type GithubRelease struct {
|
type GithubRelease struct {
|
||||||
TagName string `json:"tag_name"`
|
TagName string `json:"tag_name"`
|
||||||
HtmlUrl string `json:"html_url"`
|
HtmlUrl string `json:"html_url"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check updtae
|
||||||
func CheckUpdate() {
|
func CheckUpdate() {
|
||||||
|
log.Infof("检查更新...")
|
||||||
url:="https://api.github.com/repos/Xhofe/alist/releases/latest"
|
url:="https://api.github.com/repos/Xhofe/alist/releases/latest"
|
||||||
resp,err:=http.Get(url)
|
resp,err:=http.Get(url)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
@ -32,8 +36,10 @@ func CheckUpdate() {
|
|||||||
log.Warnf("解析更新失败:%s",err.Error())
|
log.Warnf("解析更新失败:%s",err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if conf.VERSION == release.TagName {
|
lasted:=release.TagName[1:]
|
||||||
log.Infof("当前已是最新版本:%s",release.TagName)
|
now:=conf.VERSION[1:]
|
||||||
|
if utils.VersionCompare(lasted,now) != 1 {
|
||||||
|
log.Infof("当前已是最新版本:%s",conf.VERSION)
|
||||||
}else {
|
}else {
|
||||||
log.Infof("发现新版本:%s",release.TagName)
|
log.Infof("发现新版本:%s",release.TagName)
|
||||||
log.Infof("请至'%s'获取更新.",release.HtmlUrl)
|
log.Infof("请至'%s'获取更新.",release.HtmlUrl)
|
||||||
|
@ -1,17 +1,23 @@
|
|||||||
info:
|
info:
|
||||||
title: AList #标题
|
title: AList #标题
|
||||||
site_url: http://localhost:5277 #前端地址
|
|
||||||
backend_url: http://localhost:5244 #后端地址
|
|
||||||
logo: "" #网站logo 如果填写,则会替换掉默认的
|
logo: "" #网站logo 如果填写,则会替换掉默认的
|
||||||
footer_text: Xhofe's Blog #网页底部文字
|
footer_text: Xhofe's Blog #网页底部文字
|
||||||
footer_url: https://www.nn.ci #网页底部文字链接
|
footer_url: https://www.nn.ci #网页底部文字链接
|
||||||
music_img: https://img.oez.cc/2020/12/19/0f8b57866bdb5.gif #预览音乐文件时的图片
|
music_img: https://img.oez.cc/2020/12/19/0f8b57866bdb5.gif #预览音乐文件时的图片
|
||||||
check_update: true #前端是否显示更新
|
check_update: true #前端是否显示更新
|
||||||
script: #自定义脚本,可以是脚本的链接,也可以直接是脚本内容,如document.querySelector('body').style="background-image:url('https://api.mtyqx.cn/api/random.php');background-attachment:fixed"
|
script: #自定义脚本,可以是脚本的链接,也可以直接是脚本内容,如document.querySelector('body').style="background-image:url('https://api.mtyqx.cn/api/random.php');background-attachment:fixed"
|
||||||
|
autoplay: true #视频是否自动播放
|
||||||
|
preview:
|
||||||
|
url: https://view.alist.nn.ci/onlinePreview?url= #extensions中包含的后缀名预览的地址,默认使用了kkFileView,可以自行搭建
|
||||||
|
pre_process: [base64,encodeURIComponent] #对地址的处理,支持base64,encodeURIComponent,encodeURI
|
||||||
|
extensions: [zip,rar,jar,tar,gzip] #使用上面的url预览的文件后缀,这是只是示例
|
||||||
|
text: [txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp] #要预览的文本文件的后缀,可以自行添加
|
||||||
|
max_size: 5242880
|
||||||
server:
|
server:
|
||||||
port: "5244" #程序监听端口
|
port: "5244" #程序监听端口
|
||||||
search: true #是否开启搜索接口,开启搜索之后密码和根目录都会失效,所以前端暂时不做搜索
|
search: true #是否开启搜索接口,开启搜索之后密码和根目录都会失效,所以前端暂时不做搜索
|
||||||
static: dist #前端文件目录
|
static: dist #前端文件目录
|
||||||
|
site_url: '*' #建议直接填*,若有信任域名要求,可填写其他,逗号分割
|
||||||
cache:
|
cache:
|
||||||
enable: true #是否开启缓存
|
enable: true #是否开启缓存
|
||||||
expiration: 60 #缓存失效时间(单位:分钟)
|
expiration: 60 #缓存失效时间(单位:分钟)
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
|
// config struct
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Info struct{
|
Info struct{
|
||||||
Title string `yaml:"title" json:"title"`
|
Title string `yaml:"title" json:"title"`
|
||||||
SiteUrl string `yaml:"site_url" json:"site_url"`//网站url
|
|
||||||
BackendUrl string `yaml:"backend_url" json:"backend_url"`//后端地址
|
|
||||||
Logo string `yaml:"logo" json:"logo"`
|
Logo string `yaml:"logo" json:"logo"`
|
||||||
FooterText string `yaml:"footer_text" json:"footer_text"`
|
FooterText string `yaml:"footer_text" json:"footer_text"`
|
||||||
FooterUrl string `yaml:"footer_url" json:"footer_url"`
|
FooterUrl string `yaml:"footer_url" json:"footer_url"`
|
||||||
@ -24,6 +23,7 @@ type Config struct {
|
|||||||
Port string `yaml:"port"`//端口
|
Port string `yaml:"port"`//端口
|
||||||
Search bool `yaml:"search" json:"search"`//允许搜索
|
Search bool `yaml:"search" json:"search"`//允许搜索
|
||||||
Static string `yaml:"static"`
|
Static string `yaml:"static"`
|
||||||
|
SiteUrl string `yaml:"site_url" json:"site_url"`//网站url
|
||||||
} `yaml:"server"`
|
} `yaml:"server"`
|
||||||
Cache struct{
|
Cache struct{
|
||||||
Enable bool `yaml:"enable"`
|
Enable bool `yaml:"enable"`
|
||||||
|
@ -6,19 +6,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var(
|
var(
|
||||||
Debug bool
|
Debug bool // is debug command
|
||||||
Help bool
|
Help bool // is help command
|
||||||
Con string
|
Version bool // is print version command
|
||||||
Client *http.Client
|
Con string // config file
|
||||||
Authorization string
|
SkipUpdate bool // skip update
|
||||||
|
|
||||||
Cache *cache.Cache
|
Client *http.Client // request client
|
||||||
|
Authorization string // authorization string
|
||||||
|
|
||||||
|
Cache *cache.Cache // cache
|
||||||
|
|
||||||
|
Origins []string // allow origins
|
||||||
)
|
)
|
||||||
|
|
||||||
var Conf = new(Config)
|
var Conf = new(Config)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION="v0.1.4"
|
VERSION="v0.1.7"
|
||||||
|
|
||||||
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"
|
||||||
|
@ -2,7 +2,8 @@ package controllers
|
|||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
func metaResponse(code int, msg string) gin.H {
|
// common meta response
|
||||||
|
func MetaResponse(code int, msg string) gin.H {
|
||||||
return gin.H{
|
return gin.H{
|
||||||
"meta":gin.H{
|
"meta":gin.H{
|
||||||
"code":code,
|
"code":code,
|
||||||
@ -11,7 +12,8 @@ func metaResponse(code int, msg string) gin.H {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dataResponse(data interface{}) gin.H {
|
// common data response
|
||||||
|
func DataResponse(data interface{}) gin.H {
|
||||||
return gin.H{
|
return gin.H{
|
||||||
"meta":gin.H{
|
"meta":gin.H{
|
||||||
"code":200,
|
"code":200,
|
||||||
|
@ -7,12 +7,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// handle get request
|
||||||
// 因为下载地址有时效,所以去掉了文件请求和直链的缓存
|
// 因为下载地址有时效,所以去掉了文件请求和直链的缓存
|
||||||
|
|
||||||
func Get(c *gin.Context) {
|
func Get(c *gin.Context) {
|
||||||
var get alidrive.GetReq
|
var get alidrive.GetReq
|
||||||
if err := c.ShouldBindJSON(&get); err != nil {
|
if err := c.ShouldBindJSON(&get); err != nil {
|
||||||
c.JSON(200,metaResponse(400,"Bad Request"))
|
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("get:%+v",get)
|
log.Debugf("get:%+v",get)
|
||||||
@ -22,25 +22,31 @@ func Get(c *gin.Context) {
|
|||||||
// file,exist:=conf.Cache.Get(cacheKey)
|
// file,exist:=conf.Cache.Get(cacheKey)
|
||||||
// if exist {
|
// if exist {
|
||||||
// log.Debugf("使用了缓存:%s",cacheKey)
|
// log.Debugf("使用了缓存:%s",cacheKey)
|
||||||
// c.JSON(200,dataResponse(file))
|
// c.JSON(200,DataResponse(file))
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
file,err:=alidrive.GetFile(get.FileId)
|
file,err:=alidrive.GetFile(get.FileId)
|
||||||
if err !=nil {
|
if err !=nil {
|
||||||
c.JSON(200,metaResponse(500,err.Error()))
|
c.JSON(200, MetaResponse(500,err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
paths,err:=alidrive.GetPaths(get.FileId)
|
paths,err:=alidrive.GetPaths(get.FileId)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
c.JSON(200,metaResponse(500,err.Error()))
|
c.JSON(200, MetaResponse(500,err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file.Paths=*paths
|
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 {
|
//if conf.Conf.Cache.Enable {
|
||||||
// conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
|
// conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
|
||||||
//}
|
//}
|
||||||
c.JSON(200,dataResponse(file))
|
c.JSON(200, DataResponse(file))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Down(c *gin.Context) {
|
func Down(c *gin.Context) {
|
||||||
@ -56,14 +62,14 @@ func Down(c *gin.Context) {
|
|||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
file,err:=alidrive.GetFile(fileId)
|
file,err:=alidrive.GetDownLoadUrl(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 {
|
//if conf.Conf.Cache.Enable {
|
||||||
// conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
|
// conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
|
||||||
//}
|
//}
|
||||||
c.Redirect(301,file.DownloadUrl)
|
c.Redirect(301,file.Url)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -9,15 +9,17 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// list request bean
|
||||||
type ListReq struct {
|
type ListReq struct {
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
alidrive.ListReq
|
alidrive.ListReq
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle list request
|
||||||
func List(c *gin.Context) {
|
func List(c *gin.Context) {
|
||||||
var list ListReq
|
var list ListReq
|
||||||
if err := c.ShouldBindJSON(&list);err!=nil {
|
if err := c.ShouldBindJSON(&list);err!=nil {
|
||||||
c.JSON(200, metaResponse(400,"Bad Request"))
|
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("list:%+v",list)
|
log.Debugf("list:%+v",list)
|
||||||
@ -27,7 +29,7 @@ func List(c *gin.Context) {
|
|||||||
files,exist:=conf.Cache.Get(cacheKey)
|
files,exist:=conf.Cache.Get(cacheKey)
|
||||||
if exist {
|
if exist {
|
||||||
log.Debugf("使用了缓存:%s",cacheKey)
|
log.Debugf("使用了缓存:%s",cacheKey)
|
||||||
c.JSON(200, dataResponse(files))
|
c.JSON(200, DataResponse(files))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,27 +49,27 @@ func List(c *gin.Context) {
|
|||||||
files,err=alidrive.GetList(list.ParentFileId,list.Limit,list.Marker,list.OrderBy,list.OrderDirection)
|
files,err=alidrive.GetList(list.ParentFileId,list.Limit,list.Marker,list.OrderBy,list.OrderDirection)
|
||||||
}
|
}
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
c.JSON(200, metaResponse(500,err.Error()))
|
c.JSON(200, MetaResponse(500,err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
password:=alidrive.HasPassword(files)
|
password:=alidrive.HasPassword(files)
|
||||||
if password!="" && password!=list.Password {
|
if password!="" && password!=list.Password {
|
||||||
if list.Password=="" {
|
if list.Password=="" {
|
||||||
c.JSON(200, metaResponse(401,"need password."))
|
c.JSON(200, MetaResponse(401,"need password."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, metaResponse(401,"wrong password."))
|
c.JSON(200, MetaResponse(401,"wrong password."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
paths,err:=alidrive.GetPaths(list.ParentFileId)
|
paths,err:=alidrive.GetPaths(list.ParentFileId)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
c.JSON(200, metaResponse(500,err.Error()))
|
c.JSON(200, MetaResponse(500,err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
files.Paths=*paths
|
files.Paths=*paths
|
||||||
files.Readme=alidrive.HasReadme(files)
|
//files.Readme=alidrive.HasReadme(files)
|
||||||
if conf.Conf.Cache.Enable {
|
if conf.Conf.Cache.Enable {
|
||||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
||||||
}
|
}
|
||||||
c.JSON(200, dataResponse(files))
|
c.JSON(200, DataResponse(files))
|
||||||
}
|
}
|
23
server/controllers/offie_preview.go
Normal file
23
server/controllers/offie_preview.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Xhofe/alist/alidrive"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
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 {
|
||||||
|
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("preview_req:%+v",req)
|
||||||
|
preview,err:=alidrive.GetOfficePreviewUrl(req.FileId)
|
||||||
|
if err!=nil {
|
||||||
|
c.JSON(200, MetaResponse(500,err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, DataResponse(preview))
|
||||||
|
}
|
@ -9,14 +9,15 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// handle search request
|
||||||
func Search(c *gin.Context) {
|
func Search(c *gin.Context) {
|
||||||
if !conf.Conf.Server.Search {
|
if !conf.Conf.Server.Search {
|
||||||
c.JSON(200, metaResponse(403,"Not allow search."))
|
c.JSON(200, MetaResponse(403,"Not allow search."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var search alidrive.SearchReq
|
var search alidrive.SearchReq
|
||||||
if err := c.ShouldBindJSON(&search); err != nil {
|
if err := c.ShouldBindJSON(&search); err != nil {
|
||||||
c.JSON(200, metaResponse(400,"Bad Request"))
|
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("search:%+v",search)
|
log.Debugf("search:%+v",search)
|
||||||
@ -26,7 +27,7 @@ func Search(c *gin.Context) {
|
|||||||
files,exist:=conf.Cache.Get(cacheKey)
|
files,exist:=conf.Cache.Get(cacheKey)
|
||||||
if exist {
|
if exist {
|
||||||
log.Debugf("使用了缓存:%s",cacheKey)
|
log.Debugf("使用了缓存:%s",cacheKey)
|
||||||
c.JSON(200, dataResponse(files))
|
c.JSON(200, DataResponse(files))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,11 +40,11 @@ func Search(c *gin.Context) {
|
|||||||
//}
|
//}
|
||||||
files,err:=alidrive.Search(search.Query,search.Limit,search.OrderBy)
|
files,err:=alidrive.Search(search.Query,search.Limit,search.OrderBy)
|
||||||
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 {
|
if conf.Conf.Cache.Enable {
|
||||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
||||||
}
|
}
|
||||||
c.JSON(200, dataResponse(files))
|
c.JSON(200, DataResponse(files))
|
||||||
}
|
}
|
@ -5,21 +5,23 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// handle info request
|
||||||
func Info(c *gin.Context) {
|
func Info(c *gin.Context) {
|
||||||
c.JSON(200, dataResponse(conf.Conf.Info))
|
c.JSON(200, DataResponse(conf.Conf.Info))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle refresh_cache request
|
||||||
func RefreshCache(c *gin.Context) {
|
func RefreshCache(c *gin.Context) {
|
||||||
password:=c.Param("password")
|
password:=c.Param("password")
|
||||||
if conf.Conf.Cache.Enable {
|
if conf.Conf.Cache.Enable {
|
||||||
if password == conf.Conf.Cache.RefreshPassword {
|
if password == conf.Conf.Cache.RefreshPassword {
|
||||||
conf.Cache.Flush()
|
conf.Cache.Flush()
|
||||||
c.JSON(200,metaResponse(200,"flush success."))
|
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."))
|
c.JSON(200, MetaResponse(400,"disabled cache."))
|
||||||
return
|
return
|
||||||
}
|
}
|
@ -2,26 +2,35 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
|
"github.com/Xhofe/alist/server/controllers"
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CrosHandler() gin.HandlerFunc {
|
// handle cors request
|
||||||
|
func CorsHandler() gin.HandlerFunc {
|
||||||
return func(context *gin.Context) {
|
return func(context *gin.Context) {
|
||||||
method := context.Request.Method
|
origin:=context.GetHeader("Origin")
|
||||||
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
// 同源
|
||||||
context.Header("Access-Control-Allow-Origin", conf.Conf.Info.SiteUrl) // 设置允许访问所有域
|
if origin == "" {
|
||||||
context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
|
context.Next()
|
||||||
context.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma,token,openid,opentoken")
|
return
|
||||||
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
|
}
|
||||||
context.Header("Access-Control-Max-Age", "172800")
|
method := context.Request.Method
|
||||||
context.Header("Access-Control-Allow-Credentials", "true")
|
// 设置跨域
|
||||||
//context.Set("content-type", "application/json") //设置返回格式是json
|
context.Header("Access-Control-Allow-Origin",origin)
|
||||||
|
context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
|
||||||
if method == "OPTIONS" {
|
context.Header("Access-Control-Allow-Headers", "Content-Length,session,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language, Keep-Alive, User-Agent, Cache-Control, Content-Type")
|
||||||
context.JSON(http.StatusOK, gin.H{})
|
context.Header("Access-Control-Expose-Headers", "Content-Length,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified")
|
||||||
|
context.Header("Access-Control-Max-Age", "172800")
|
||||||
|
// 信任域名
|
||||||
|
if conf.Conf.Server.SiteUrl!="*"&&utils.ContainsString(conf.Origins,context.GetHeader("Origin"))==-1 {
|
||||||
|
context.JSON(200,controllers.MetaResponse(413,"The origin is not in the site_url list, please configure it correctly."))
|
||||||
|
context.Abort()
|
||||||
|
}
|
||||||
|
if method == "OPTIONS" {
|
||||||
|
context.AbortWithStatus(204)
|
||||||
}
|
}
|
||||||
|
|
||||||
//处理请求
|
//处理请求
|
||||||
context.Next()
|
context.Next()
|
||||||
}
|
}
|
||||||
|
@ -8,23 +8,26 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// init router
|
||||||
func InitRouter(engine *gin.Engine) {
|
func InitRouter(engine *gin.Engine) {
|
||||||
log.Infof("初始化路由...")
|
log.Infof("初始化路由...")
|
||||||
engine.Use(CrosHandler())
|
engine.Use(CorsHandler())
|
||||||
InitApiRouter(engine)
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitApiRouter(engine *gin.Engine) {
|
|
||||||
engine.Use(static.Serve("/",static.LocalFile(conf.Conf.Server.Static,false)))
|
engine.Use(static.Serve("/",static.LocalFile(conf.Conf.Server.Static,false)))
|
||||||
engine.NoRoute(func(c *gin.Context) {
|
engine.NoRoute(func(c *gin.Context) {
|
||||||
c.File(conf.Conf.Server.Static+"/index.html")
|
c.File(conf.Conf.Server.Static+"/index.html")
|
||||||
})
|
})
|
||||||
|
InitApiRouter(engine)
|
||||||
|
}
|
||||||
|
|
||||||
|
// init api router
|
||||||
|
func InitApiRouter(engine *gin.Engine) {
|
||||||
v2:=engine.Group("/api")
|
v2:=engine.Group("/api")
|
||||||
{
|
{
|
||||||
v2.GET("/info",controllers.Info)
|
v2.GET("/info",controllers.Info)
|
||||||
v2.POST("/get",controllers.Get)
|
v2.POST("/get",controllers.Get)
|
||||||
v2.POST("/list",controllers.List)
|
v2.POST("/list",controllers.List)
|
||||||
v2.POST("/search",controllers.Search)
|
v2.POST("/search",controllers.Search)
|
||||||
|
v2.POST("/office_preview",controllers.OfficePreview)
|
||||||
}
|
}
|
||||||
engine.GET("/d/*file_id",controllers.Down)
|
engine.GET("/d/*file_id",controllers.Down)
|
||||||
engine.GET("/cache/:password",controllers.RefreshCache)
|
engine.GET("/cache/:password",controllers.RefreshCache)
|
||||||
|
@ -3,8 +3,11 @@ package utils
|
|||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// get code from url
|
||||||
func GetCode(rawUrl string) string {
|
func GetCode(rawUrl string) string {
|
||||||
u,err:=url.Parse(rawUrl)
|
u,err:=url.Parse(rawUrl)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
@ -13,4 +16,43 @@ func GetCode(rawUrl string) string {
|
|||||||
}
|
}
|
||||||
code:=u.Query().Get("code")
|
code:=u.Query().Get("code")
|
||||||
return code
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
// determine whether to include
|
||||||
|
func ContainsString(array []string, val string) (index int) {
|
||||||
|
index = -1
|
||||||
|
for i := 0; i < len(array); i++ {
|
||||||
|
if array[i] == val {
|
||||||
|
index = i
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// compare version
|
||||||
|
func VersionCompare(version1, version2 string) int {
|
||||||
|
a := strings.Split(version1, ".")
|
||||||
|
b := strings.Split(version2, ".")
|
||||||
|
flag := 1
|
||||||
|
if len(a) > len(b) {
|
||||||
|
a, b = b, a
|
||||||
|
flag = -1
|
||||||
|
}
|
||||||
|
for i := range a {
|
||||||
|
x, _ := strconv.Atoi(a[i])
|
||||||
|
y, _ := strconv.Atoi(b[i])
|
||||||
|
if x < y {
|
||||||
|
return -1 * flag
|
||||||
|
} else if x > y {
|
||||||
|
return 1 * flag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, v:= range b[len(a):] {
|
||||||
|
y, _ := strconv.Atoi(v)
|
||||||
|
if y > 0 {
|
||||||
|
return -1 * flag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
}
|
}
|
@ -8,6 +8,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// determine whether the file exists
|
||||||
func Exists(name string) bool {
|
func Exists(name string) bool {
|
||||||
if _, err := os.Stat(name); err != nil {
|
if _, err := os.Stat(name); err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@ -17,6 +18,7 @@ func Exists(name string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 嵌套创建文件
|
||||||
func CreatNestedFile(path string) (*os.File, error) {
|
func CreatNestedFile(path string) (*os.File, error) {
|
||||||
basePath := filepath.Dir(path)
|
basePath := filepath.Dir(path)
|
||||||
if !Exists(basePath) {
|
if !Exists(basePath) {
|
||||||
@ -29,6 +31,7 @@ func CreatNestedFile(path string) (*os.File, error) {
|
|||||||
return os.Create(path)
|
return os.Create(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write struct to yaml file
|
||||||
func WriteToYml(src string,conf interface{}){
|
func WriteToYml(src string,conf interface{}){
|
||||||
data,err := yaml.Marshal(conf)
|
data,err := yaml.Marshal(conf)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
|
Reference in New Issue
Block a user