Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
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:
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
</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" 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://github.com/Xhofe/alist/workflows/build/badge.svg" 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/latest/total" 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" alt="License"></a>
|
||||||
<a href="https://pay.xhofe.top">
|
<a href="https://pay.xhofe.top">
|
||||||
|
@ -1,46 +1,52 @@
|
|||||||
package alidrive
|
package alidrive
|
||||||
|
|
||||||
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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TokenLoginReq struct {
|
type TokenLoginReq struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetTokenReq struct {
|
type GetTokenReq struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RefreshTokenReq struct {
|
type RefreshTokenReq struct {
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OfficePreviewUrlReq struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
DriveId string `json:"drive_id"`
|
||||||
|
FileId string `json:"file_id"`
|
||||||
|
}
|
||||||
|
@ -20,25 +20,11 @@ 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Search(key string,limit int, marker string) (*Files, error) {
|
func Search(key string,limit int, marker string) (*Files, error) {
|
||||||
@ -53,25 +39,11 @@ 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
|
func GetRoot(limit int,marker string,orderBy string,orderDirection string) (*Files,error) {
|
||||||
@ -92,48 +64,56 @@ 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
}
|
||||||
|
return &resp,nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
}else {
|
||||||
if err = json.Unmarshal(body,&user);err !=nil {
|
if err = json.Unmarshal(body,&resp);err!=nil {
|
||||||
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
log.Errorf("解析json[%s]出错:%s",string(body),err.Error())
|
||||||
return nil,err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if user.IsAvailable() {
|
if resp.IsAvailable() {
|
||||||
return &user,nil
|
return nil
|
||||||
}
|
}
|
||||||
if user.Code==conf.AccessTokenInvalid {
|
if resp.GetCode() == conf.AccessTokenInvalid {
|
||||||
|
resp.SetCode("")
|
||||||
if RefreshToken() {
|
if RefreshToken() {
|
||||||
return GetUserInfo()
|
return BodyToJson(url,req,resp,auth)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil,fmt.Errorf(user.Message)
|
return fmt.Errorf(resp.GetMessage())
|
||||||
}
|
}
|
||||||
|
|
||||||
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
|
func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
|
||||||
|
@ -8,11 +8,34 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type RespHandle interface {
|
||||||
|
IsAvailable() bool
|
||||||
|
GetCode() string
|
||||||
|
GetMessage() string
|
||||||
|
SetCode(code string)
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
type UserInfo struct {
|
type UserInfo struct {
|
||||||
RespError
|
RespError
|
||||||
DomainId string `json:"domain_id"`
|
DomainId string `json:"domain_id"`
|
||||||
@ -36,7 +59,7 @@ type Files struct {
|
|||||||
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"`
|
||||||
Paths []Path `json:"paths"`
|
Paths []Path `json:"paths"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Path struct {
|
type Path struct {
|
||||||
@ -72,11 +95,7 @@ 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 {
|
|
||||||
return resp.Code == ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TokenLoginResp struct {
|
type TokenLoginResp struct {
|
||||||
@ -104,11 +123,17 @@ type TokenResp struct {
|
|||||||
DeviceId string `json:"device_id"`
|
DeviceId string `json:"device_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OfficePreviewUrlResp struct {
|
||||||
|
RespError
|
||||||
|
PreviewUrl string `json:"preview_url"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
}
|
||||||
|
|
||||||
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:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,6 +12,7 @@ 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")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,6 +22,10 @@ func Run() {
|
|||||||
flag.Usage()
|
flag.Usage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if conf.Version {
|
||||||
|
fmt.Println("Current version:"+conf.VERSION)
|
||||||
|
return
|
||||||
|
}
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReadConf(config string) bool {
|
func ReadConf(config string) bool {
|
||||||
@ -25,5 +26,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
|
||||||
}
|
}
|
@ -3,6 +3,7 @@ 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"
|
||||||
@ -15,6 +16,7 @@ type GithubRelease struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 +34,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 #缓存失效时间(单位:分钟)
|
||||||
|
@ -3,8 +3,6 @@ package conf
|
|||||||
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 +22,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"`
|
||||||
|
@ -8,17 +8,20 @@ import (
|
|||||||
var(
|
var(
|
||||||
Debug bool
|
Debug bool
|
||||||
Help bool
|
Help bool
|
||||||
|
Version bool
|
||||||
Con string
|
Con string
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
Authorization string
|
Authorization string
|
||||||
|
|
||||||
Cache *cache.Cache
|
Cache *cache.Cache
|
||||||
|
|
||||||
|
Origins []string
|
||||||
)
|
)
|
||||||
|
|
||||||
var Conf = new(Config)
|
var Conf = new(Config)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION="v0.1.4"
|
VERSION="v0.1.6"
|
||||||
|
|
||||||
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,7 @@ package controllers
|
|||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
func metaResponse(code int, msg string) gin.H {
|
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 +11,7 @@ func metaResponse(code int, msg string) gin.H {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dataResponse(data interface{}) gin.H {
|
func DataResponse(data interface{}) gin.H {
|
||||||
return gin.H{
|
return gin.H{
|
||||||
"meta":gin.H{
|
"meta":gin.H{
|
||||||
"code":200,
|
"code":200,
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
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,25 @@ 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
|
||||||
//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) {
|
||||||
@ -58,7 +58,7 @@ func Down(c *gin.Context) {
|
|||||||
//}
|
//}
|
||||||
file,err:=alidrive.GetFile(fileId)
|
file,err:=alidrive.GetFile(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 {
|
||||||
|
@ -17,7 +17,7 @@ type ListReq struct {
|
|||||||
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 +27,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,21 +47,21 @@ 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
|
||||||
@ -69,5 +69,5 @@ func List(c *gin.Context) {
|
|||||||
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))
|
||||||
}
|
}
|
22
server/controllers/offie_preview.go
Normal file
22
server/controllers/offie_preview.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Xhofe/alist/alidrive"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
@ -11,12 +11,12 @@ import (
|
|||||||
|
|
||||||
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 +26,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 +39,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))
|
||||||
}
|
}
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Info(c *gin.Context) {
|
func Info(c *gin.Context) {
|
||||||
c.JSON(200, dataResponse(conf.Conf.Info))
|
c.JSON(200, DataResponse(conf.Conf.Info))
|
||||||
}
|
}
|
||||||
|
|
||||||
func RefreshCache(c *gin.Context) {
|
func RefreshCache(c *gin.Context) {
|
||||||
@ -14,12 +14,12 @@ func RefreshCache(c *gin.Context) {
|
|||||||
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,34 @@ 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 {
|
func CrosHandler() 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()
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ func InitApiRouter(engine *gin.Engine) {
|
|||||||
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,6 +3,8 @@ package utils
|
|||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetCode(rawUrl string) string {
|
func GetCode(rawUrl string) string {
|
||||||
@ -13,4 +15,41 @@ func GetCode(rawUrl string) string {
|
|||||||
}
|
}
|
||||||
code:=u.Query().Get("code")
|
code:=u.Query().Get("code")
|
||||||
return code
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
Reference in New Issue
Block a user