Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
d693e27ec0 | |||
b20f0717fe | |||
427244d8d5 | |||
b613598c2b | |||
f0013320a6 | |||
974caf74d9 | |||
0bb02664c7 | |||
6c5a0cba6e | |||
7e21e12e11 | |||
11489d8856 | |||
6d824a4ee9 | |||
8d74d070d4 | |||
77aae6660e | |||
76081a81a6 | |||
8760ab283d | |||
61ab27398c | |||
2e64df7e3d | |||
9561f0c951 | |||
03f5a54764 |
13
.github/workflows/build.yml
vendored
13
.github/workflows/build.yml
vendored
@ -46,6 +46,11 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
CC=x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o alist_windows_amd64.exe alist.go
|
CC=x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o alist_windows_amd64.exe alist.go
|
||||||
|
|
||||||
|
- name: Build linux_386
|
||||||
|
run: |
|
||||||
|
sudo apt-get -y install libc6-dev-i386
|
||||||
|
CC=gcc CGO_ENABLED=1 GOOS=linux GOARCH=386 go build -o alist_linux_386 alist.go
|
||||||
|
|
||||||
- name: Upload artifacts linux_amd64
|
- name: Upload artifacts linux_amd64
|
||||||
uses: actions/upload-artifact@v2
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
@ -68,4 +73,10 @@ jobs:
|
|||||||
uses: actions/upload-artifact@v2
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
name: alist_windows_amd64
|
name: alist_windows_amd64
|
||||||
path: alist_windows_amd64.exe
|
path: alist_windows_amd64.exe
|
||||||
|
|
||||||
|
- name: Upload artifacts linux_386
|
||||||
|
uses: actions/upload-artifact@v2
|
||||||
|
with:
|
||||||
|
name: alist_linux_386
|
||||||
|
path: alist_linux_386
|
||||||
|
17
.github/workflows/release.yml
vendored
17
.github/workflows/release.yml
vendored
@ -47,11 +47,17 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
CC=x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o windows_amd64/alist.exe alist.go
|
CC=x86_64-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o windows_amd64/alist.exe alist.go
|
||||||
|
|
||||||
|
- name: Build linux_386
|
||||||
|
run: |
|
||||||
|
sudo apt-get -y install libc6-dev-i386
|
||||||
|
CC=gcc CGO_ENABLED=1 GOOS=linux GOARCH=386 go build -o linux_386/alist alist.go
|
||||||
|
|
||||||
- name: compress
|
- name: compress
|
||||||
run: |
|
run: |
|
||||||
tar -czvf alist_linux_amd64.tar.gz linux_amd64/alist conf.yml.example
|
tar -czvf alist_linux_amd64.tar.gz linux_amd64/alist conf.yml.example
|
||||||
tar -czvf alist_linux_arm64.tar.gz linux_arm64/alist conf.yml.example
|
tar -czvf alist_linux_arm64.tar.gz linux_arm64/alist conf.yml.example
|
||||||
tar -czvf alist_linux_arm.tar.gz linux_arm/alist conf.yml.example
|
tar -czvf alist_linux_arm.tar.gz linux_arm/alist conf.yml.example
|
||||||
|
tar -czvf alist_linux_386.tar.gz linux_386/alist conf.yml.example
|
||||||
zip alist_windows_amd64.zip windows_amd64/alist.exe conf.yml.example
|
zip alist_windows_amd64.zip windows_amd64/alist.exe conf.yml.example
|
||||||
|
|
||||||
- name: Build Changelog
|
- name: Build Changelog
|
||||||
@ -105,6 +111,17 @@ jobs:
|
|||||||
asset_name: alist_${{ steps.get_version.outputs.VERSION }}_linux_arm.tar.gz
|
asset_name: alist_${{ steps.get_version.outputs.VERSION }}_linux_arm.tar.gz
|
||||||
asset_content_type: application/gzip
|
asset_content_type: application/gzip
|
||||||
|
|
||||||
|
- name: Upload alist_linux_386
|
||||||
|
id: upload-release-linux-386
|
||||||
|
uses: actions/upload-release-asset@v1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||||
|
asset_path: alist_linux_386.tar.gz
|
||||||
|
asset_name: alist_${{ steps.get_version.outputs.VERSION }}_linux_386.tar.gz
|
||||||
|
asset_content_type: application/gzip
|
||||||
|
|
||||||
- name: Upload alist_windows_amd64
|
- name: Upload alist_windows_amd64
|
||||||
id: upload-release-windows-amd64
|
id: upload-release-windows-amd64
|
||||||
uses: actions/upload-release-asset@v1
|
uses: actions/upload-release-asset@v1
|
||||||
|
@ -10,8 +10,8 @@ import (
|
|||||||
// refresh access_token token by refresh_token
|
// refresh access_token token by refresh_token
|
||||||
func RefreshToken(drive *conf.Drive) bool {
|
func RefreshToken(drive *conf.Drive) bool {
|
||||||
log.Infof("刷新[%s]token...", drive.Name)
|
log.Infof("刷新[%s]token...", drive.Name)
|
||||||
url := "https://websv.aliyundrive.com/token/refresh"
|
url := "https://auth.aliyundrive.com/v2/account/token"
|
||||||
req := RefreshTokenReq{RefreshToken: drive.RefreshToken}
|
req := RefreshTokenReq{RefreshToken: drive.RefreshToken , GrantType: "refresh_token"}
|
||||||
var token TokenResp
|
var token TokenResp
|
||||||
if body, err := DoPost(url, req, ""); err != nil {
|
if body, err := DoPost(url, req, ""); err != nil {
|
||||||
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
||||||
@ -23,6 +23,10 @@ func RefreshToken(drive *conf.Drive) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if token.Code != "" {
|
||||||
|
log.Errorf("盘[%s]刷新token出错:%s", drive.Name, token.Message)
|
||||||
|
return false
|
||||||
|
}
|
||||||
//刷新成功 更新token
|
//刷新成功 更新token
|
||||||
drive.AccessToken = token.AccessToken
|
drive.AccessToken = token.AccessToken
|
||||||
drive.RefreshToken = token.RefreshToken
|
drive.RefreshToken = token.RefreshToken
|
||||||
|
80
alidrive/post_json.go
Normal file
80
alidrive/post_json.go
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package alidrive
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/Xhofe/alist/conf"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// convert body to json
|
||||||
|
func BodyToJson(url string, req interface{}, resp RespHandle, drive *conf.Drive) error {
|
||||||
|
if body, err := DoPost(url, req, drive.AccessToken); 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(drive) {
|
||||||
|
return BodyToJson(url, req, resp, drive)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf(resp.GetMessage())
|
||||||
|
}
|
||||||
|
|
||||||
|
// do post request
|
||||||
|
func DoPost(url string, request interface{}, auth string) (body []byte, err error) {
|
||||||
|
var (
|
||||||
|
resp *http.Response
|
||||||
|
)
|
||||||
|
requestBody := new(bytes.Buffer)
|
||||||
|
err = json.NewEncoder(requestBody).Encode(request)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("创建requestBody出错:%s", err.Error())
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", url, requestBody)
|
||||||
|
log.Debugf("do_post_req:%+v", req)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("创建request出错:%s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if auth != "" {
|
||||||
|
req.Header.Set("authorization", conf.Bearer+auth)
|
||||||
|
}
|
||||||
|
req.Header.Add("content-type", "application/json")
|
||||||
|
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
||||||
|
req.Header.Add("origin", "https://aliyundrive.com")
|
||||||
|
req.Header.Add("accept", "*/*")
|
||||||
|
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
|
||||||
|
req.Header.Add("Connection", "keep-alive")
|
||||||
|
|
||||||
|
for retryCount := 3; retryCount >= 0; retryCount-- {
|
||||||
|
if resp, err = conf.Client.Do(req); err != nil && strings.Contains(err.Error(), "timeout") {
|
||||||
|
<-time.After(time.Second)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("请求阿里云盘api时出错:%s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
||||||
|
log.Errorf("读取api返回内容失败")
|
||||||
|
}
|
||||||
|
log.Debugf("请求返回信息:%s", string(body))
|
||||||
|
return
|
||||||
|
}
|
@ -57,6 +57,7 @@ type GetTokenReq struct {
|
|||||||
// refresh_token request bean
|
// refresh_token request bean
|
||||||
type RefreshTokenReq struct {
|
type RefreshTokenReq struct {
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
GrantType string `json:"grant_type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// office_preview_url request bean
|
// office_preview_url request bean
|
||||||
@ -65,3 +66,10 @@ type OfficePreviewUrlReq struct {
|
|||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// video preview url request bean
|
||||||
|
type VideoPreviewUrlReq struct {
|
||||||
|
DriveId string `json:"drive_id"`
|
||||||
|
FileId string `json:"file_id"`
|
||||||
|
ExpireSec int `json:"expire_sec"`
|
||||||
|
}
|
||||||
|
@ -1,15 +1,8 @@
|
|||||||
package alidrive
|
package alidrive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// get file
|
// get file
|
||||||
@ -115,69 +108,17 @@ func GetOfficePreviewUrl(fileId string, drive *conf.Drive) (*OfficePreviewUrlRes
|
|||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert body to json
|
// get video preview url
|
||||||
func BodyToJson(url string, req interface{}, resp RespHandle, drive *conf.Drive) error {
|
func GetVideoPreviewUrl(fileId string, drive *conf.Drive) (*VideoPreviewUrlResp, error) {
|
||||||
if body, err := DoPost(url, req, drive.AccessToken); err != nil {
|
url := conf.Conf.AliDrive.ApiUrl + "/databox/get_video_play_info"
|
||||||
log.Errorf("doPost出错:%s", err.Error())
|
req := VideoPreviewUrlReq{
|
||||||
return err
|
DriveId: drive.DefaultDriveId,
|
||||||
} else {
|
FileId: fileId,
|
||||||
if err = json.Unmarshal(body, &resp); err != nil {
|
ExpireSec: 14400,
|
||||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if resp.IsAvailable() {
|
var resp VideoPreviewUrlResp
|
||||||
return nil
|
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.GetCode() == conf.AccessTokenInvalid {
|
return &resp, nil
|
||||||
resp.SetCode("")
|
|
||||||
if RefreshToken(drive) {
|
|
||||||
return BodyToJson(url, req, resp, drive)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf(resp.GetMessage())
|
|
||||||
}
|
|
||||||
|
|
||||||
// do post request
|
|
||||||
func DoPost(url string, request interface{}, auth string) (body []byte, err error) {
|
|
||||||
var (
|
|
||||||
resp *http.Response
|
|
||||||
)
|
|
||||||
requestBody := new(bytes.Buffer)
|
|
||||||
err = json.NewEncoder(requestBody).Encode(request)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("创建requestBody出错:%s", err.Error())
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", url, requestBody)
|
|
||||||
log.Debugf("do_post_req:%+v", req)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("创建request出错:%s", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if auth != "" {
|
|
||||||
req.Header.Set("authorization", conf.Bearer+auth)
|
|
||||||
}
|
|
||||||
req.Header.Add("content-type", "application/json")
|
|
||||||
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
|
|
||||||
req.Header.Add("origin", "https://aliyundrive.com")
|
|
||||||
req.Header.Add("accept", "*/*")
|
|
||||||
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
|
|
||||||
req.Header.Add("Connection", "keep-alive")
|
|
||||||
|
|
||||||
for retryCount := 3; retryCount >= 0; retryCount-- {
|
|
||||||
if resp, err = conf.Client.Do(req); err != nil && strings.Contains(err.Error(), "timeout") {
|
|
||||||
<-time.After(time.Second)
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("请求阿里云盘api时出错:%s", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if body, err = ioutil.ReadAll(resp.Body); err != nil {
|
|
||||||
log.Errorf("读取api返回内容失败")
|
|
||||||
}
|
|
||||||
log.Debugf("请求返回信息:%s", string(body))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
package alidrive
|
package alidrive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -72,6 +68,14 @@ type Path struct {
|
|||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 秒传
|
||||||
|
{
|
||||||
|
"name":"mikuclub.mp4",
|
||||||
|
"content_hash":"C733AC50D1F964C0398D0E403F3A30C37EFC2ADD",
|
||||||
|
"size":1141068377,
|
||||||
|
"content_type":"video/mp4"
|
||||||
|
}
|
||||||
|
*/
|
||||||
// file response bean
|
// file response bean
|
||||||
type File struct {
|
type File struct {
|
||||||
RespError
|
RespError
|
||||||
@ -150,35 +154,11 @@ type OfficePreviewUrlResp struct {
|
|||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// check password
|
type VideoPreviewUrlResp struct {
|
||||||
func HasPassword(files *Files) string {
|
RespError
|
||||||
fileList := files.Items
|
TemplateList []struct {
|
||||||
for i, file := range fileList {
|
TemplateId string `json:"template_id"`
|
||||||
if strings.HasPrefix(file.Name, ".password-") {
|
Status string `json:"status"`
|
||||||
files.Items = fileList[:i+copy(fileList[i:], fileList[i+1:])]
|
Url string `json:"url"`
|
||||||
return file.Name[10:]
|
} `json:"template_list"`
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: check readme, implemented by the front end now
|
|
||||||
func HasReadme(files *Files) string {
|
|
||||||
fileList := files.Items
|
|
||||||
for _, file := range fileList {
|
|
||||||
if file.Name == "Readme.md" {
|
|
||||||
resp, err := http.Get(file.Url)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Get Readme出错:%s", err.Error())
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("读取 Readme出错:%s", err.Error())
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return string(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
41
alidrive/utils.go
Normal file
41
alidrive/utils.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package alidrive
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// check password
|
||||||
|
func HasPassword(files *Files) string {
|
||||||
|
fileList := files.Items
|
||||||
|
for i, file := range fileList {
|
||||||
|
if strings.HasPrefix(file.Name, ".password-") {
|
||||||
|
files.Items = fileList[:i+copy(fileList[i:], fileList[i+1:])]
|
||||||
|
return file.Name[10:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deprecated: check readme, implemented by the front end now
|
||||||
|
func HasReadme(files *Files) string {
|
||||||
|
fileList := files.Items
|
||||||
|
for _, file := range fileList {
|
||||||
|
if file.Name == "Readme.md" {
|
||||||
|
resp, err := http.Get(file.Url)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Get Readme出错:%s", err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("读取 Readme出错:%s", err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -9,5 +10,8 @@ import (
|
|||||||
// init request client
|
// init request client
|
||||||
func InitClient() {
|
func InitClient() {
|
||||||
log.Infof("初始化client...")
|
log.Infof("初始化client...")
|
||||||
conf.Client = &http.Client{}
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
conf.Client = &http.Client{Transport: tr}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package bootstrap
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"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"
|
||||||
@ -71,7 +72,7 @@ func start() {
|
|||||||
|
|
||||||
// start http server
|
// start http server
|
||||||
func server() {
|
func server() {
|
||||||
baseServer := "0.0.0.0:" + conf.Conf.Server.Port
|
baseServer := conf.Conf.Server.Address + ":" + conf.Conf.Server.Port
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
serv.InitRouter(r)
|
serv.InitRouter(r)
|
||||||
log.Infof("Starting server @ %s", baseServer)
|
log.Infof("Starting server @ %s", baseServer)
|
||||||
|
@ -10,6 +10,7 @@ info:
|
|||||||
preview:
|
preview:
|
||||||
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] #要预览的文本文件的后缀,可以自行添加
|
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] #要预览的文本文件的后缀,可以自行添加
|
||||||
server:
|
server:
|
||||||
|
address: "0.0.0.0"
|
||||||
port: "5244"
|
port: "5244"
|
||||||
search: true
|
search: true
|
||||||
static: dist
|
static: dist
|
||||||
@ -17,7 +18,7 @@ server:
|
|||||||
password: password #用于重建目录
|
password: password #用于重建目录
|
||||||
ali_drive:
|
ali_drive:
|
||||||
api_url: https://api.aliyundrive.com/v2
|
api_url: https://api.aliyundrive.com/v2
|
||||||
max_files_count: 3000
|
max_files_count: 50 #重建目录时每次请求的文件
|
||||||
drives:
|
drives:
|
||||||
- refresh_token: xxx #refresh_token
|
- refresh_token: xxx #refresh_token
|
||||||
root_folder: root #根目录的file_id
|
root_folder: root #根目录的file_id
|
||||||
|
@ -31,6 +31,7 @@ type Config struct {
|
|||||||
} `yaml:"preview" json:"preview"`
|
} `yaml:"preview" json:"preview"`
|
||||||
} `yaml:"info"`
|
} `yaml:"info"`
|
||||||
Server struct {
|
Server struct {
|
||||||
|
Address string `yaml:"address"`
|
||||||
Port string `yaml:"port"` //端口
|
Port string `yaml:"port"` //端口
|
||||||
Search bool `yaml:"search"` //允许搜索
|
Search bool `yaml:"search"` //允许搜索
|
||||||
Static string `yaml:"static"`
|
Static string `yaml:"static"`
|
||||||
|
@ -22,7 +22,7 @@ var (
|
|||||||
var Conf = new(Config)
|
var Conf = new(Config)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "v1.0.1"
|
VERSION = "v1.0.4"
|
||||||
|
|
||||||
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"
|
||||||
|
@ -45,7 +45,7 @@ func Down(c *gin.Context) {
|
|||||||
c.JSON(200, MetaResponse(406, "无法下载目录."))
|
c.JSON(200, MetaResponse(406, "无法下载目录."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
drive := utils.GetDriveByName(strings.Split(dir, "/")[0])
|
drive := utils.GetDriveByName(strings.Split(filePath, "/")[0])
|
||||||
if drive == nil {
|
if drive == nil {
|
||||||
c.JSON(200, MetaResponse(500, "找不到drive."))
|
c.JSON(200, MetaResponse(500, "找不到drive."))
|
||||||
return
|
return
|
||||||
|
@ -42,7 +42,7 @@ func Get(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
drive := utils.GetDriveByName(strings.Split(dir, "/")[0])
|
drive := utils.GetDriveByName(strings.Split(get.Path, "/")[0])
|
||||||
if drive == nil {
|
if drive == nil {
|
||||||
c.JSON(200, MetaResponse(500, "找不到drive."))
|
c.JSON(200, MetaResponse(500, "找不到drive."))
|
||||||
return
|
return
|
||||||
|
@ -31,3 +31,4 @@ func OfficePreview(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.JSON(200, DataResponse(preview))
|
c.JSON(200, DataResponse(preview))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ func Path(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// folder model not exist
|
// folder model not exist
|
||||||
if file == nil {
|
if file == nil {
|
||||||
c.JSON(200, MetaResponse(404, "path not found."))
|
c.JSON(200, MetaResponse(404, "path not found.(第一次请先点击网页底部rebuild)"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
@ -3,8 +3,8 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/server/models"
|
"github.com/Xhofe/alist/server/models"
|
||||||
"github.com/Xhofe/alist/utils"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handle info request
|
// handle info request
|
||||||
@ -12,14 +12,21 @@ func Info(c *gin.Context) {
|
|||||||
c.JSON(200, DataResponse(conf.Conf.Info))
|
c.JSON(200, DataResponse(conf.Conf.Info))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RebuildReq struct {
|
||||||
|
Path string `json:"path" binding:"required"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Depth int `json:"depth"`
|
||||||
|
}
|
||||||
|
|
||||||
// rebuild tree
|
// rebuild tree
|
||||||
func RebuildTree(c *gin.Context) {
|
func RebuildTree(c *gin.Context) {
|
||||||
drive := utils.GetDriveByName(c.Param("drive"))
|
var req RebuildReq
|
||||||
if drive == nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(200, MetaResponse(400, "drive isn't exist."))
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
password := c.Param("password")
|
log.Debugf("rebuild:%+v", req)
|
||||||
|
password := req.Password
|
||||||
if password != conf.Conf.Server.Password {
|
if password != conf.Conf.Server.Password {
|
||||||
if password == "" {
|
if password == "" {
|
||||||
c.JSON(200, MetaResponse(401, "need password."))
|
c.JSON(200, MetaResponse(401, "need password."))
|
||||||
@ -28,11 +35,7 @@ func RebuildTree(c *gin.Context) {
|
|||||||
c.JSON(200, MetaResponse(401, "wrong password."))
|
c.JSON(200, MetaResponse(401, "wrong password."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := models.Clear(drive); err != nil {
|
if err := models.BuildTreeWithPath(req.Path, req.Depth); err != nil {
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := models.BuildTree(drive); err != nil {
|
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
33
server/controllers/video_preview.go
Normal file
33
server/controllers/video_preview.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Xhofe/alist/alidrive"
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type VideoPreviewReq struct {
|
||||||
|
FileId string `json:"file_id" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle video_preview request
|
||||||
|
func VideoPreview(c *gin.Context) {
|
||||||
|
drive := utils.GetDriveByName(c.Param("drive"))
|
||||||
|
if drive == nil {
|
||||||
|
c.JSON(200, MetaResponse(400, "drive isn't exist."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req VideoPreviewReq
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("preview_req:%+v", req)
|
||||||
|
preview, err := alidrive.GetVideoPreviewUrl(req.FileId, drive)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, DataResponse(preview))
|
||||||
|
}
|
@ -4,23 +4,25 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Xhofe/alist/alidrive"
|
"github.com/Xhofe/alist/alidrive"
|
||||||
"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"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BuildTreeAll() {
|
func BuildTreeAll(depth int) {
|
||||||
for i, _ := range conf.Conf.AliDrive.Drives {
|
for i, _ := range conf.Conf.AliDrive.Drives {
|
||||||
if err := BuildTree(&conf.Conf.AliDrive.Drives[i]); err != nil {
|
if err := BuildTree(&conf.Conf.AliDrive.Drives[i], depth); err != nil {
|
||||||
log.Errorf("盘[%s]构建目录树失败:%s", err.Error())
|
log.Errorf("盘[%s]构建目录树失败:%s", conf.Conf.AliDrive.Drives[i].Name, err.Error())
|
||||||
} else {
|
} else {
|
||||||
log.Infof("盘[%s]构建目录树成功")
|
log.Infof("盘[%s]构建目录树成功", conf.Conf.AliDrive.Drives[i].Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// build tree
|
// build tree
|
||||||
func BuildTree(drive *conf.Drive) error {
|
func BuildTree(drive *conf.Drive, depth int) error {
|
||||||
log.Infof("开始构建目录树...")
|
log.Infof("开始构建目录树...")
|
||||||
tx := conf.DB.Begin()
|
tx := conf.DB.Begin()
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -42,50 +44,137 @@ func BuildTree(drive *conf.Drive) error {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := BuildOne(drive.RootFolder, drive.Name+"/", tx, drive.Password, drive); err != nil {
|
if err := BuildOne(drive.RootFolder, drive.Name+"/", tx, drive.Password, drive, depth); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return tx.Commit().Error
|
return tx.Commit().Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string, drive *conf.Drive) error {
|
/*
|
||||||
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, "", "", "", drive)
|
递归构建目录树,插入指定目录下的所有文件
|
||||||
if err != nil {
|
parent 父目录的file_id
|
||||||
return err
|
path 指定的目录
|
||||||
|
parentPassword 父目录所携带的密码
|
||||||
|
drive 要构建的盘
|
||||||
|
*/
|
||||||
|
func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string, drive *conf.Drive, depth int) error {
|
||||||
|
if depth == 0 {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
for _, file := range files.Items {
|
marker := "first"
|
||||||
name := file.Name
|
for marker != "" {
|
||||||
if strings.HasSuffix(name, ".hide") {
|
if marker == "first" {
|
||||||
continue
|
marker = ""
|
||||||
}
|
}
|
||||||
password := parentPassword
|
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, marker, "", "", drive)
|
||||||
if strings.Contains(name, ".password-") {
|
if err != nil {
|
||||||
index := strings.Index(name, ".password-")
|
|
||||||
name = file.Name[:index]
|
|
||||||
password = file.Name[index+10:]
|
|
||||||
}
|
|
||||||
newFile := File{
|
|
||||||
Dir: path,
|
|
||||||
FileExtension: file.FileExtension,
|
|
||||||
FileId: file.FileId,
|
|
||||||
Name: name,
|
|
||||||
Type: file.Type,
|
|
||||||
UpdatedAt: file.UpdatedAt,
|
|
||||||
Category: file.Category,
|
|
||||||
ContentType: file.ContentType,
|
|
||||||
Size: file.Size,
|
|
||||||
Password: password,
|
|
||||||
}
|
|
||||||
log.Debugf("插入file:%+v", newFile)
|
|
||||||
if err := tx.Create(&newFile).Error; err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if file.Type == "folder" {
|
marker = files.NextMarker
|
||||||
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, name), tx, password, drive); err != nil {
|
for _, file := range files.Items {
|
||||||
|
name := file.Name
|
||||||
|
if strings.HasSuffix(name, ".hide") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
password := parentPassword
|
||||||
|
if strings.Contains(name, ".password-") {
|
||||||
|
index := strings.Index(name, ".password-")
|
||||||
|
name = file.Name[:index]
|
||||||
|
password = file.Name[index+10:]
|
||||||
|
}
|
||||||
|
newFile := File{
|
||||||
|
Dir: path,
|
||||||
|
FileExtension: file.FileExtension,
|
||||||
|
FileId: file.FileId,
|
||||||
|
Name: name,
|
||||||
|
Type: file.Type,
|
||||||
|
UpdatedAt: file.UpdatedAt,
|
||||||
|
Category: file.Category,
|
||||||
|
ContentType: file.ContentType,
|
||||||
|
Size: file.Size,
|
||||||
|
Password: password,
|
||||||
|
ContentHash: file.ContentHash,
|
||||||
|
}
|
||||||
|
log.Debugf("插入file:%+v", newFile)
|
||||||
|
if err := tx.Create(&newFile).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if file.Type == "folder" {
|
||||||
|
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, name), tx, password, drive, depth-1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//重建指定路径与深度的目录树: 先删除该目录与该目录下所有文件的model,再重新插入
|
||||||
|
func BuildTreeWithPath(path string, depth int) error {
|
||||||
|
dir, name := filepath.Split(path)
|
||||||
|
driveName := strings.Split(path, "/")[0]
|
||||||
|
drive := utils.GetDriveByName(driveName)
|
||||||
|
if drive == nil {
|
||||||
|
return fmt.Errorf("找不到drive[%s]", driveName)
|
||||||
|
}
|
||||||
|
file := &File{
|
||||||
|
Dir: "",
|
||||||
|
FileId: drive.RootFolder,
|
||||||
|
Name: drive.Name,
|
||||||
|
Type: "folder",
|
||||||
|
Password: drive.Password,
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
if dir != "" {
|
||||||
|
file, err = GetFileByDirAndName(dir, name)
|
||||||
|
if err != nil {
|
||||||
|
if file == nil {
|
||||||
|
return fmt.Errorf("path not found")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tx := conf.DB.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if err = tx.Error; err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = tx.Where("dir = ? AND name = ?", file.Dir, file.Name).Delete(file).Error; err != nil{
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = tx.Where("dir like ?", fmt.Sprintf("%s%%", path)).Delete(&File{}).Error; err != nil{
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//if dir != "" {
|
||||||
|
// aliFile, err := alidrive.GetFile(file.FileId, drive)
|
||||||
|
// if err != nil {
|
||||||
|
// tx.Rollback()
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// aliName := aliFile.Name
|
||||||
|
// if strings.HasSuffix(aliName, ".hide") {
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
// if strings.Contains(aliName, ".password-") {
|
||||||
|
// index := strings.Index(name, ".password-")
|
||||||
|
// file.Name = aliName[:index]
|
||||||
|
// file.Password = aliName[index+10:]
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
if err = tx.Create(&file).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = BuildOne(file.FileId, path+"/", tx, file.Password, drive, depth); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit().Error
|
||||||
|
}
|
||||||
|
@ -18,6 +18,7 @@ type File struct {
|
|||||||
Size int64 `json:"size"`
|
Size int64 `json:"size"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Url string `json:"url" gorm:"-"`
|
Url string `json:"url" gorm:"-"`
|
||||||
|
ContentHash string `json:"content_hash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (file *File) Create() error {
|
func (file *File) Create() error {
|
||||||
@ -62,3 +63,7 @@ func SearchByNameInDir(keyword string, dir string) (*[]File, error) {
|
|||||||
}
|
}
|
||||||
return &files, nil
|
return &files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteWithDir(dir string) error {
|
||||||
|
return conf.DB.Where("dir like ?", fmt.Sprintf("%s%%", dir)).Delete(&File{}).Error
|
||||||
|
}
|
@ -27,9 +27,10 @@ func InitApiRouter(engine *gin.Engine) {
|
|||||||
apiV2.POST("/get", controllers.Get)
|
apiV2.POST("/get", controllers.Get)
|
||||||
apiV2.POST("/path", controllers.Path)
|
apiV2.POST("/path", controllers.Path)
|
||||||
apiV2.POST("/office_preview/:drive", controllers.OfficePreview)
|
apiV2.POST("/office_preview/:drive", controllers.OfficePreview)
|
||||||
|
apiV2.POST("/video_preview/:drive", controllers.VideoPreview)
|
||||||
apiV2.POST("/local_search", controllers.LocalSearch)
|
apiV2.POST("/local_search", controllers.LocalSearch)
|
||||||
apiV2.POST("/global_search", controllers.GlobalSearch)
|
apiV2.POST("/global_search", controllers.GlobalSearch)
|
||||||
apiV2.GET("/rebuild/:drive/:password", controllers.RebuildTree)
|
apiV2.POST("/rebuild", controllers.RebuildTree)
|
||||||
}
|
}
|
||||||
engine.GET("/d/*path", controllers.Down)
|
engine.GET("/d/*path", controllers.Down)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user