Compare commits
39 Commits
Author | SHA1 | Date | |
---|---|---|---|
f31fc9155a | |||
d3db012dd0 | |||
74b86ef5e5 | |||
6be90429ad | |||
a6bbff2199 | |||
3827d3ed67 | |||
e242867f41 | |||
e09a75a87c | |||
e0a80b1477 | |||
d693e27ec0 | |||
b20f0717fe | |||
427244d8d5 | |||
b613598c2b | |||
f0013320a6 | |||
974caf74d9 | |||
0bb02664c7 | |||
6c5a0cba6e | |||
7e21e12e11 | |||
11489d8856 | |||
6d824a4ee9 | |||
8d74d070d4 | |||
77aae6660e | |||
76081a81a6 | |||
8760ab283d | |||
61ab27398c | |||
2e64df7e3d | |||
9561f0c951 | |||
03f5a54764 | |||
9952c3e90b | |||
d94e319df3 | |||
b51b2deea1 | |||
7dda701f1e | |||
3b93445648 | |||
a353081126 | |||
a5b2f998ab | |||
4d0d892ce7 | |||
8e9ddcf81e | |||
5c6344cac0 | |||
7076efb6be |
15
.github/workflows/build.yml
vendored
15
.github/workflows/build.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
platform: [ubuntu-latest]
|
platform: [ubuntu-16.04]
|
||||||
go-version: [1.15]
|
go-version: [1.15]
|
||||||
name: Build
|
name: Build
|
||||||
runs-on: ${{ matrix.platform }}
|
runs-on: ${{ matrix.platform }}
|
||||||
@ -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
|
||||||
|
138
.github/workflows/release.yml
vendored
138
.github/workflows/release.yml
vendored
@ -5,24 +5,130 @@ on:
|
|||||||
- '*'
|
- '*'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
goreleaser:
|
release:
|
||||||
runs-on: ubuntu-latest
|
strategy:
|
||||||
|
matrix:
|
||||||
|
platform: [ ubuntu-16.04 ]
|
||||||
|
go-version: [ 1.15 ]
|
||||||
|
name: Build
|
||||||
|
runs-on: ${{ matrix.platform }}
|
||||||
steps:
|
steps:
|
||||||
-
|
- name: Set up Go
|
||||||
name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
-
|
|
||||||
name: Set up Go
|
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: ${{ matrix.go-version }}
|
||||||
-
|
|
||||||
name: Run GoReleaser
|
- name: Get version
|
||||||
uses: goreleaser/goreleaser-action@v2
|
id: get_version
|
||||||
with:
|
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}
|
||||||
version: latest
|
|
||||||
args: release --rm-dist
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Get dependencies
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get -y install gcc-mingw-w64-x86-64
|
||||||
|
sudo apt-get -y install gcc-arm-linux-gnueabihf libc6-dev-armhf-cross
|
||||||
|
sudo apt-get -y install gcc-aarch64-linux-gnu libc6-dev-arm64-cross
|
||||||
|
go get -v -t -d ./...
|
||||||
|
if [ -f Gopkg.toml ]; then
|
||||||
|
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
|
||||||
|
dep ensure
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Build linux
|
||||||
|
run: |
|
||||||
|
CC=gcc CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o linux_amd64/alist alist.go
|
||||||
|
CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -o linux_arm64/alist alist.go
|
||||||
|
CC=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm go build -o linux_arm/alist alist.go
|
||||||
|
|
||||||
|
- name: Build windows
|
||||||
|
run: |
|
||||||
|
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
|
||||||
|
run: |
|
||||||
|
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_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
|
||||||
|
|
||||||
|
- name: Build Changelog
|
||||||
|
id: github_release
|
||||||
|
uses: mikepenz/release-changelog-builder-action@main
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
id: create_release
|
||||||
|
uses: actions/create-release@v1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
tag_name: ${{ github.ref }}
|
||||||
|
release_name: Release ${{ github.ref }}
|
||||||
|
body: ${{steps.github_release.outputs.changelog}}
|
||||||
|
draft: false
|
||||||
|
prerelease: false
|
||||||
|
|
||||||
|
- name: Upload alist_linux_amd64
|
||||||
|
id: upload-release-linux-amd64
|
||||||
|
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_amd64.tar.gz
|
||||||
|
asset_name: alist_${{ steps.get_version.outputs.VERSION }}_linux_amd64.tar.gz
|
||||||
|
asset_content_type: application/gzip
|
||||||
|
|
||||||
|
- name: Upload alist_linux_arm64
|
||||||
|
id: upload-release-linux-arm64
|
||||||
|
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_arm64.tar.gz
|
||||||
|
asset_name: alist_${{ steps.get_version.outputs.VERSION }}_linux_arm64.tar.gz
|
||||||
|
asset_content_type: application/gzip
|
||||||
|
|
||||||
|
- name: Upload alist_linux_arm
|
||||||
|
id: upload-release-linux-arm
|
||||||
|
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_arm.tar.gz
|
||||||
|
asset_name: alist_${{ steps.get_version.outputs.VERSION }}_linux_arm.tar.gz
|
||||||
|
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
|
||||||
|
id: upload-release-windows-amd64
|
||||||
|
uses: actions/upload-release-asset@v1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||||
|
asset_path: alist_windows_amd64.zip
|
||||||
|
asset_name: alist_${{ steps.get_version.outputs.VERSION }}_windows_amd64.zip
|
||||||
|
asset_content_type: application/zip
|
@ -2,77 +2,48 @@ package alidrive
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/utils"
|
"github.com/Xhofe/alist/utils"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// use token login
|
|
||||||
func TokenLogin() (*TokenLoginResp, error) {
|
|
||||||
log.Infof("尝试使用token登录...")
|
|
||||||
url := "https://auth.aliyundrive.com/v2/oauth/token_login"
|
|
||||||
req := TokenLoginReq{Token: conf.Conf.AliDrive.LoginToken}
|
|
||||||
log.Debugf("token_login_req:%+v", req)
|
|
||||||
var tokenLogin TokenLoginResp
|
|
||||||
if body, err := DoPost(url, req, false); err != nil {
|
|
||||||
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
if err = json.Unmarshal(body, &tokenLogin); err != nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tokenLogin.IsAvailable() {
|
|
||||||
return &tokenLogin, nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("登录token失效,请更换:%s", tokenLogin.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// get access token
|
|
||||||
func GetToken(tokenLogin *TokenLoginResp) (*TokenResp, error) {
|
|
||||||
log.Infof("获取API token...")
|
|
||||||
url := "https://websv.aliyundrive.com/token/get"
|
|
||||||
code := utils.GetCode(tokenLogin.Goto)
|
|
||||||
if code == "" {
|
|
||||||
return nil, fmt.Errorf("获取code出错")
|
|
||||||
}
|
|
||||||
req := GetTokenReq{Code: code}
|
|
||||||
var token TokenResp
|
|
||||||
if body, err := DoPost(url, req, false); err != nil {
|
|
||||||
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
if err = json.Unmarshal(body, &token); err != nil {
|
|
||||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
|
||||||
log.Errorf("此处json解析失败应该是code失效")
|
|
||||||
return nil, fmt.Errorf("code失效")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &token, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// refresh access_token token by refresh_token
|
// refresh access_token token by refresh_token
|
||||||
func RefreshToken() bool {
|
func RefreshToken(drive *conf.Drive) bool {
|
||||||
log.Infof("刷新token...")
|
log.Infof("刷新[%s]token...", drive.Name)
|
||||||
url := "https://websv.aliyundrive.com/token/refresh"
|
url := "https://auth.aliyundrive.com/v2/account/token"
|
||||||
req := RefreshTokenReq{RefreshToken: conf.Conf.AliDrive.RefreshToken}
|
req := RefreshTokenReq{RefreshToken: drive.RefreshToken , GrantType: "refresh_token"}
|
||||||
var token TokenResp
|
var token TokenResp
|
||||||
if body, err := DoPost(url, req, false); err != nil {
|
if body, err := DoPost(url, req, ""); err != nil {
|
||||||
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
log.Errorf("tokenLogin-doPost出错:%s", err.Error())
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
if err = json.Unmarshal(body, &token); err != nil {
|
if err = json.Unmarshal(body, &token); err != nil {
|
||||||
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
log.Errorf("解析json[%s]出错:%s", string(body), err.Error())
|
||||||
log.Errorf("此处json解析失败应该是refresh_token失效")
|
log.Errorf("此处json解析失败应该是[%s]refresh_token失效", drive.Name)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//刷新成功 更新token并写入文件
|
if token.Code != "" {
|
||||||
conf.Conf.AliDrive.AccessToken = token.AccessToken
|
log.Errorf("盘[%s]刷新token出错:%s", drive.Name, token.Message)
|
||||||
conf.Conf.AliDrive.RefreshToken = token.RefreshToken
|
return false
|
||||||
conf.Authorization = token.TokenType + "\t" + token.AccessToken
|
}
|
||||||
utils.WriteToYml(conf.Con, conf.Conf)
|
//刷新成功 更新token
|
||||||
|
drive.AccessToken = token.AccessToken
|
||||||
|
drive.RefreshToken = token.RefreshToken
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RefreshTokenAll() string {
|
||||||
|
log.Infof("刷新所有token...")
|
||||||
|
res := ""
|
||||||
|
for i, drive := range conf.Conf.AliDrive.Drives {
|
||||||
|
if !RefreshToken(&conf.Conf.AliDrive.Drives[i]) {
|
||||||
|
res = res + drive.Name + ","
|
||||||
|
}
|
||||||
|
}
|
||||||
|
utils.WriteToYml(conf.ConfigFile, conf.Conf)
|
||||||
|
if res != "" {
|
||||||
|
return res[:len(res)-1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package alidrive
|
|
||||||
|
|
||||||
var (
|
|
||||||
User *UserInfo
|
|
||||||
)
|
|
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
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package alidrive
|
package alidrive
|
||||||
|
|
||||||
// list request bean
|
// ListReq 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"`
|
||||||
@ -14,7 +14,7 @@ type ListReq struct {
|
|||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// get request bean
|
// GetReq 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"`
|
||||||
@ -22,7 +22,7 @@ type GetReq struct {
|
|||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// download request bean
|
// DownloadReq download request bean
|
||||||
type DownloadReq struct {
|
type DownloadReq struct {
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
@ -30,7 +30,7 @@ type DownloadReq struct {
|
|||||||
FileName string `json:"file_name"`
|
FileName string `json:"file_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// search request bean
|
// SearchReq 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"`
|
||||||
@ -44,24 +44,32 @@ type SearchReq struct {
|
|||||||
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
VideoThumbnailProcess string `json:"video_thumbnail_process"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// token_login request bean
|
// TokenLoginReq token_login request bean
|
||||||
type TokenLoginReq struct {
|
type TokenLoginReq struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_token request bean
|
// GetTokenReq get_token request bean
|
||||||
type GetTokenReq struct {
|
type GetTokenReq struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// refresh_token request bean
|
// RefreshTokenReq 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
|
// OfficePreviewUrlReq office_preview_url request bean
|
||||||
type OfficePreviewUrlReq struct {
|
type OfficePreviewUrlReq struct {
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
DriveId string `json:"drive_id"`
|
DriveId string `json:"drive_id"`
|
||||||
FileId string `json:"file_id"`
|
FileId string `json:"file_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VideoPreviewUrlReq video preview url request bean
|
||||||
|
type VideoPreviewUrlReq struct {
|
||||||
|
DriveId string `json:"drive_id"`
|
||||||
|
FileId string `json:"file_id"`
|
||||||
|
ExpireSec int `json:"expire_sec"`
|
||||||
|
}
|
@ -1,53 +1,46 @@
|
|||||||
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
|
||||||
func GetFile(fileId string) (*File, error) {
|
func GetFile(fileId string, drive *conf.Drive) (*File, error) {
|
||||||
url := conf.Conf.AliDrive.ApiUrl + "/file/get"
|
url := conf.Conf.AliDrive.ApiUrl + "/file/get"
|
||||||
req := GetReq{
|
req := GetReq{
|
||||||
DriveId: User.DefaultDriveId,
|
DriveId: drive.DefaultDriveId,
|
||||||
FileId: fileId,
|
FileId: fileId,
|
||||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var resp File
|
var resp File
|
||||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get download_url
|
// get download_url
|
||||||
func GetDownLoadUrl(fileId string) (*DownloadResp, error) {
|
func GetDownLoadUrl(fileId string, drive *conf.Drive) (*DownloadResp, error) {
|
||||||
url := conf.Conf.AliDrive.ApiUrl + "/file/get_download_url"
|
url := conf.Conf.AliDrive.ApiUrl + "/file/get_download_url"
|
||||||
req := DownloadReq{
|
req := DownloadReq{
|
||||||
DriveId: User.DefaultDriveId,
|
DriveId: drive.DefaultDriveId,
|
||||||
FileId: fileId,
|
FileId: fileId,
|
||||||
ExpireSec: 14400,
|
ExpireSec: 14400,
|
||||||
}
|
}
|
||||||
var resp DownloadResp
|
var resp DownloadResp
|
||||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// search by keyword
|
// search by keyword
|
||||||
func Search(key string, limit int, marker string) (*Files, error) {
|
func Search(key string, limit int, marker string, drive *conf.Drive) (*Files, error) {
|
||||||
url := conf.Conf.AliDrive.ApiUrl + "/file/search"
|
url := conf.Conf.AliDrive.ApiUrl + "/file/search"
|
||||||
req := SearchReq{
|
req := SearchReq{
|
||||||
DriveId: User.DefaultDriveId,
|
DriveId: drive.DefaultDriveId,
|
||||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||||
ImageUrlProcess: conf.ImageUrlProcess,
|
ImageUrlProcess: conf.ImageUrlProcess,
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
@ -57,22 +50,22 @@ func Search(key string, limit int, marker string) (*Files, error) {
|
|||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var resp Files
|
var resp Files
|
||||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get root folder
|
// 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, drive *conf.Drive) (*Files, error) {
|
||||||
return GetList(conf.Conf.AliDrive.RootFolder, limit, marker, orderBy, orderDirection)
|
return GetList(drive.RootFolder, limit, marker, orderBy, orderDirection, drive)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get folder list by file_id
|
// 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, drive *conf.Drive) (*Files, error) {
|
||||||
url := conf.Conf.AliDrive.ApiUrl + "/file/list"
|
url := conf.Conf.AliDrive.ApiUrl + "/file/list"
|
||||||
req := ListReq{
|
req := ListReq{
|
||||||
DriveId: User.DefaultDriveId,
|
DriveId: drive.DefaultDriveId,
|
||||||
Fields: "*",
|
Fields: "*",
|
||||||
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
ImageThumbnailProcess: conf.ImageThumbnailProcess,
|
||||||
ImageUrlProcess: conf.ImageUrlProcess,
|
ImageUrlProcess: conf.ImageUrlProcess,
|
||||||
@ -84,121 +77,48 @@ func GetList(parent string, limit int, marker string, orderBy string, orderDirec
|
|||||||
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
VideoThumbnailProcess: conf.VideoThumbnailProcess,
|
||||||
}
|
}
|
||||||
var resp Files
|
var resp Files
|
||||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get user info
|
// get user info
|
||||||
func GetUserInfo() (*UserInfo, error) {
|
func GetUserInfo(drive *conf.Drive) (*UserInfo, error) {
|
||||||
url := conf.Conf.AliDrive.ApiUrl + "/user/get"
|
url := conf.Conf.AliDrive.ApiUrl + "/user/get"
|
||||||
var resp UserInfo
|
var resp UserInfo
|
||||||
if err := BodyToJson(url, map[string]interface{}{}, &resp, true); err != nil {
|
if err := BodyToJson(url, map[string]interface{}{}, &resp, drive); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get office preview url and token
|
// get office preview url and token
|
||||||
func GetOfficePreviewUrl(fileId string) (*OfficePreviewUrlResp, error) {
|
func GetOfficePreviewUrl(fileId string, drive *conf.Drive) (*OfficePreviewUrlResp, error) {
|
||||||
url := conf.Conf.AliDrive.ApiUrl + "/file/get_office_preview_url"
|
url := conf.Conf.AliDrive.ApiUrl + "/file/get_office_preview_url"
|
||||||
req := OfficePreviewUrlReq{
|
req := OfficePreviewUrlReq{
|
||||||
AccessToken: conf.Conf.AliDrive.AccessToken,
|
AccessToken: drive.AccessToken,
|
||||||
DriveId: User.DefaultDriveId,
|
DriveId: drive.DefaultDriveId,
|
||||||
FileId: fileId,
|
FileId: fileId,
|
||||||
}
|
}
|
||||||
var resp OfficePreviewUrlResp
|
var resp OfficePreviewUrlResp
|
||||||
if err := BodyToJson(url, req, &resp, true); err != nil {
|
if err := BodyToJson(url, req, &resp, drive); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert body to json
|
// get video preview url
|
||||||
func BodyToJson(url string, req interface{}, resp RespHandle, auth bool) error {
|
func GetVideoPreviewUrl(fileId string, drive *conf.Drive) (*VideoPreviewUrlResp, error) {
|
||||||
if body, err := DoPost(url, req, auth); 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() {
|
|
||||||
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) {
|
|
||||||
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.Authorization)
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetPaths(fileId string) (*[]Path, error) {
|
|
||||||
paths := make([]Path, 0)
|
|
||||||
for fileId != conf.Conf.AliDrive.RootFolder && fileId != "root" {
|
|
||||||
file, err := GetFile(fileId)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("获取path出错:%s", err.Error())
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
paths = append(paths, Path{
|
|
||||||
Name: file.Name,
|
|
||||||
FileId: file.FileId,
|
|
||||||
})
|
|
||||||
fileId = file.ParentFileId
|
|
||||||
}
|
|
||||||
paths = append(paths, Path{
|
|
||||||
Name: "Root",
|
|
||||||
FileId: "root",
|
|
||||||
})
|
|
||||||
return &paths, nil
|
|
||||||
}
|
}
|
||||||
|
@ -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 ""
|
||||||
|
}
|
@ -10,28 +10,24 @@ import (
|
|||||||
func InitAliDrive() bool {
|
func InitAliDrive() bool {
|
||||||
log.Infof("初始化阿里云盘...")
|
log.Infof("初始化阿里云盘...")
|
||||||
//首先token_login
|
//首先token_login
|
||||||
if conf.Conf.AliDrive.RefreshToken == "" {
|
res := alidrive.RefreshTokenAll()
|
||||||
tokenLogin, err := alidrive.TokenLogin()
|
if res != "" {
|
||||||
if err != nil {
|
log.Errorf("盘[%s]refresh_token失效,请检查", res)
|
||||||
log.Errorf("登录失败:%s", err.Error())
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
//然后get_token
|
|
||||||
token, err := alidrive.GetToken(tokenLogin)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
conf.Authorization = token.TokenType + "\t" + token.AccessToken
|
|
||||||
} else {
|
|
||||||
conf.Authorization = conf.Bearer + conf.Conf.AliDrive.AccessToken
|
|
||||||
}
|
}
|
||||||
log.Debugf("token:%s", conf.Authorization)
|
log.Debugf("config:%+v", conf.Conf)
|
||||||
user, err := alidrive.GetUserInfo()
|
for i, _ := range conf.Conf.AliDrive.Drives {
|
||||||
if err != nil {
|
InitDriveId(&conf.Conf.AliDrive.Drives[i])
|
||||||
log.Errorf("初始化用户失败:%s", err.Error())
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
log.Infof("当前用户信息:%+v", user)
|
return true
|
||||||
alidrive.User = user
|
}
|
||||||
|
|
||||||
|
func InitDriveId(drive *conf.Drive) bool {
|
||||||
|
user, err := alidrive.GetUserInfo(drive)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("初始化盘[%s]失败:%s", drive.Name, err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
drive.DefaultDriveId = user.DefaultDriveId
|
||||||
|
log.Infof("初始化盘[%s]成功:%+v", drive.Name, user)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
@ -13,7 +14,7 @@ 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.BoolVar(&conf.Version, "version", false, "show version info")
|
||||||
flag.StringVar(&conf.Con, "conf", "conf.yml", "config file")
|
flag.StringVar(&conf.ConfigFile, "conf", "conf.yml", "config file")
|
||||||
flag.BoolVar(&conf.SkipUpdate, "skip-update", false, "skip update")
|
flag.BoolVar(&conf.SkipUpdate, "skip-update", false, "skip update")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +53,7 @@ func start() {
|
|||||||
if !conf.SkipUpdate {
|
if !conf.SkipUpdate {
|
||||||
CheckUpdate()
|
CheckUpdate()
|
||||||
}
|
}
|
||||||
if !ReadConf(conf.Con) {
|
if !ReadConf(conf.ConfigFile) {
|
||||||
log.Errorf("读取配置文件时出现错误,启动失败.")
|
log.Errorf("读取配置文件时出现错误,启动失败.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -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)
|
||||||
|
@ -14,7 +14,9 @@ func ReadConf(config string) bool {
|
|||||||
log.Infof("读取配置文件...")
|
log.Infof("读取配置文件...")
|
||||||
if !utils.Exists(config) {
|
if !utils.Exists(config) {
|
||||||
log.Infof("找不到配置文件:%s", config)
|
log.Infof("找不到配置文件:%s", config)
|
||||||
return false
|
if !Write(config) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
confFile, err := ioutil.ReadFile(config)
|
confFile, err := ioutil.ReadFile(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -27,6 +29,56 @@ func ReadConf(config string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
log.Debugf("config:%+v", conf.Conf)
|
log.Debugf("config:%+v", conf.Conf)
|
||||||
|
conf.Conf.Info.Roots = utils.GetNames()
|
||||||
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl, ",")
|
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl, ",")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
func Write(path string) bool {
|
||||||
|
log.Infof("创建默认配置文件")
|
||||||
|
file, err := utils.CreatNestedFile(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("无法创建配置文件, %s", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = file.Close()
|
||||||
|
}()
|
||||||
|
str := `
|
||||||
|
info:
|
||||||
|
title: AList #标题
|
||||||
|
logo: "" #网站logo 如果填写,则会替换掉默认的
|
||||||
|
footer_text: Xhofe's Blog #网页底部文字
|
||||||
|
footer_url: https://www.nn.ci #网页底部文字链接
|
||||||
|
music_img: https://img.xhofe.top/2020/12/19/0f8b57866bdb5.gif #预览音乐文件时的图片
|
||||||
|
check_update: true #前端是否显示更新
|
||||||
|
script: #自定义脚本,可以是脚本的链接,也可以直接是脚本内容
|
||||||
|
autoplay: true #视频是否自动播放
|
||||||
|
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] #要预览的文本文件的后缀,可以自行添加
|
||||||
|
server:
|
||||||
|
address: "0.0.0.0"
|
||||||
|
port: "5244"
|
||||||
|
search: true
|
||||||
|
static: dist
|
||||||
|
site_url: '*'
|
||||||
|
password: password #用于重建目录
|
||||||
|
ali_drive:
|
||||||
|
api_url: https://api.aliyundrive.com/v2
|
||||||
|
max_files_count: 100
|
||||||
|
drives:
|
||||||
|
- refresh_token: xxx #refresh_token
|
||||||
|
root_folder: root #根目录的file_id
|
||||||
|
name: drive0 #盘名,多个盘不可重复,这里只是示例,不是一定要叫这个名字,可随意修改
|
||||||
|
password: pass #该盘密码,空('')则不设密码,修改需要重建生效
|
||||||
|
hide: false #是否在主页隐藏该盘,不可全部隐藏,至少暴露一个
|
||||||
|
database:
|
||||||
|
type: sqlite3
|
||||||
|
dBFile: alist.db
|
||||||
|
`
|
||||||
|
_, err = file.WriteString(str)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("无法写入配置文件, %s", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
@ -10,7 +10,7 @@ var Cron *cron.Cron
|
|||||||
|
|
||||||
// refresh token func for cron
|
// refresh token func for cron
|
||||||
func refreshToken() {
|
func refreshToken() {
|
||||||
alidrive.RefreshToken()
|
alidrive.RefreshTokenAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
// init cron jobs
|
// init cron jobs
|
||||||
|
@ -1,26 +1,34 @@
|
|||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"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/Xhofe/alist/utils"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/schema"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitModel() bool {
|
func InitModel() bool {
|
||||||
log.Infof("初始化数据库...")
|
log.Infof("初始化数据库...")
|
||||||
switch conf.Conf.Database.Type {
|
dbConfig := conf.Conf.Database
|
||||||
|
switch dbConfig.Type {
|
||||||
case "sqlite3":
|
case "sqlite3":
|
||||||
{
|
{
|
||||||
if !(strings.HasSuffix(conf.Conf.Database.DBFile, ".db") && len(conf.Conf.Database.DBFile) > 3) {
|
if !(strings.HasSuffix(dbConfig.DBFile, ".db") && len(dbConfig.DBFile) > 3) {
|
||||||
log.Errorf("db名称不正确.")
|
log.Errorf("db名称不正确.")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
needMigrate := !utils.Exists(conf.Conf.Database.DBFile)
|
needMigrate := !utils.Exists(dbConfig.DBFile)
|
||||||
db, err := gorm.Open(sqlite.Open(conf.Conf.Database.DBFile), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open(dbConfig.DBFile), &gorm.Config{
|
||||||
|
NamingStrategy: schema.NamingStrategy{
|
||||||
|
TablePrefix: dbConfig.TablePrefix,
|
||||||
|
},
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("连接数据库出现错误:%s", err.Error())
|
log.Errorf("连接数据库出现错误:%s", err.Error())
|
||||||
return false
|
return false
|
||||||
@ -33,14 +41,34 @@ func InitModel() bool {
|
|||||||
log.Errorf("数据库迁移失败:%s", err.Error())
|
log.Errorf("数据库迁移失败:%s", err.Error())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if err := models.BuildTree(); err != nil {
|
//models.BuildTreeAll()
|
||||||
log.Errorf("构建目录树失败:%s", err.Error())
|
}
|
||||||
}
|
return true
|
||||||
|
}
|
||||||
|
case "mysql":
|
||||||
|
{
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
|
dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Name)
|
||||||
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||||
|
NamingStrategy: schema.NamingStrategy{
|
||||||
|
TablePrefix: dbConfig.TablePrefix,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("连接数据库出现错误:%s", err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
conf.DB = db
|
||||||
|
log.Infof("迁移数据库...")
|
||||||
|
err = conf.DB.AutoMigrate(&models.File{})
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("数据库迁移失败:%s", err.Error())
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Errorf("不支持的数据库类型:%s", conf.Conf.Database.Type)
|
log.Errorf("不支持的数据库类型:%s", dbConfig.Type)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,23 +8,28 @@ info:
|
|||||||
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 #视频是否自动播放
|
autoplay: true #视频是否自动播放
|
||||||
preview:
|
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] #要预览的文本文件的后缀,可以自行添加
|
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" #程序监听端口
|
address: "0.0.0.0"
|
||||||
search: true #是否开启搜索接口,开启搜索之后密码和根目录都会失效,所以前端暂时不做搜索
|
port: "5244"
|
||||||
static: dist #前端文件目录
|
search: true
|
||||||
site_url: '*' #建议直接填*,若有信任域名要求,可填写其他,逗号分割
|
static: dist
|
||||||
cache:
|
site_url: '*'
|
||||||
enable: true #是否开启缓存
|
password: password #用于重建目录
|
||||||
expiration: 60 #缓存失效时间(单位:分钟)
|
|
||||||
cleanup_interval: 120 #清理失效缓存间隔
|
|
||||||
refresh_password: password #手动清理缓存密码
|
|
||||||
ali_drive:
|
ali_drive:
|
||||||
api_url: https://api.aliyundrive.com/v2 #阿里云盘api,无需修改
|
api_url: https://api.aliyundrive.com/v2
|
||||||
root_folder: root #根目录
|
max_files_count: 50 #重建目录时每次请求的文件
|
||||||
refresh_token: need
|
drives:
|
||||||
max_files_count: 3000
|
- refresh_token: xxx #refresh_token
|
||||||
|
root_folder: root #根目录的file_id
|
||||||
|
name: drive0 #盘名,多个盘不可重复
|
||||||
|
password: pass #该盘密码,空则不设密码,修改需要重建生效
|
||||||
|
hide: false #是否在主页隐藏该盘,不可全部隐藏,至少暴露一个
|
||||||
|
- refresh_token: xxx
|
||||||
|
root_folder: root
|
||||||
|
name: drive1
|
||||||
|
password: pass
|
||||||
|
hide: false
|
||||||
|
database:
|
||||||
|
type: sqlite3
|
||||||
|
dBFile: alist.db
|
||||||
|
@ -1,16 +1,27 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
|
type Drive struct {
|
||||||
|
AccessToken string `yaml:"-"`
|
||||||
|
RefreshToken string `yaml:"refresh_token"`
|
||||||
|
RootFolder string `yaml:"root_folder"` //根目录id
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
|
Hide bool `yaml:"hide"`
|
||||||
|
DefaultDriveId string `yaml:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
// config struct
|
// config struct
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Info struct {
|
Info struct {
|
||||||
Title string `yaml:"title" json:"title"`
|
Title string `yaml:"title" json:"title"`
|
||||||
Logo string `yaml:"logo" json:"logo"`
|
Roots []string `yaml:"-" json:"roots"`
|
||||||
FooterText string `yaml:"footer_text" json:"footer_text"`
|
Logo string `yaml:"logo" json:"logo"`
|
||||||
FooterUrl string `yaml:"footer_url" json:"footer_url"`
|
FooterText string `yaml:"footer_text" json:"footer_text"`
|
||||||
MusicImg string `yaml:"music_img" json:"music_img"`
|
FooterUrl string `yaml:"footer_url" json:"footer_url"`
|
||||||
CheckUpdate bool `yaml:"check_update" json:"check_update"`
|
MusicImg string `yaml:"music_img" json:"music_img"`
|
||||||
Script string `yaml:"script" json:"script"`
|
CheckUpdate bool `yaml:"check_update" json:"check_update"`
|
||||||
Autoplay bool `yaml:"autoplay" json:"autoplay"`
|
Script string `yaml:"script" json:"script"`
|
||||||
|
Autoplay bool `yaml:"autoplay" json:"autoplay"`
|
||||||
Preview struct {
|
Preview struct {
|
||||||
Url string `yaml:"url" json:"url"`
|
Url string `yaml:"url" json:"url"`
|
||||||
PreProcess []string `yaml:"pre_process" json:"pre_process"`
|
PreProcess []string `yaml:"pre_process" json:"pre_process"`
|
||||||
@ -20,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"`
|
||||||
@ -27,13 +39,9 @@ type Config struct {
|
|||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
} `yaml:"server"`
|
} `yaml:"server"`
|
||||||
AliDrive struct {
|
AliDrive struct {
|
||||||
ApiUrl string `yaml:"api_url"` //阿里云盘api
|
ApiUrl string `yaml:"api_url"` //阿里云盘api
|
||||||
RootFolder string `yaml:"root_folder"` //根目录id
|
MaxFilesCount int `yaml:"max_files_count"`
|
||||||
//Authorization string `yaml:"authorization"`//授权token
|
Drives []Drive `yaml:"drives"`
|
||||||
LoginToken string `yaml:"login_token"`
|
|
||||||
AccessToken string `yaml:"access_token"`
|
|
||||||
RefreshToken string `yaml:"refresh_token"`
|
|
||||||
MaxFilesCount int `yaml:"max_files_count"`
|
|
||||||
} `yaml:"ali_drive"`
|
} `yaml:"ali_drive"`
|
||||||
Database struct {
|
Database struct {
|
||||||
Type string `yaml:"type"`
|
Type string `yaml:"type"`
|
||||||
|
@ -9,11 +9,10 @@ var (
|
|||||||
Debug bool // is debug command
|
Debug bool // is debug command
|
||||||
Help bool // is help command
|
Help bool // is help command
|
||||||
Version bool // is print version command
|
Version bool // is print version command
|
||||||
Con string // config file
|
ConfigFile string // config file
|
||||||
SkipUpdate bool // skip update
|
SkipUpdate bool // skip update
|
||||||
|
|
||||||
Client *http.Client // request client
|
Client *http.Client // request client
|
||||||
Authorization string // authorization string
|
|
||||||
|
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ var (
|
|||||||
var Conf = new(Config)
|
var Conf = new(Config)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "v1.0.0"
|
VERSION = "v1.0.5"
|
||||||
|
|
||||||
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"
|
||||||
|
3
go.mod
3
go.mod
@ -19,6 +19,7 @@ require (
|
|||||||
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect
|
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect
|
||||||
google.golang.org/protobuf v1.25.0 // indirect
|
google.golang.org/protobuf v1.25.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
|
gorm.io/driver/mysql v1.0.5
|
||||||
gorm.io/driver/sqlite v1.1.4
|
gorm.io/driver/sqlite v1.1.4
|
||||||
gorm.io/gorm v1.21.1
|
gorm.io/gorm v1.21.3
|
||||||
)
|
)
|
||||||
|
6
go.sum
6
go.sum
@ -27,6 +27,8 @@ github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1
|
|||||||
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||||
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
|
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
|
||||||
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
|
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
|
||||||
|
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
|
||||||
|
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
@ -166,10 +168,14 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
|||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/driver/mysql v1.0.5 h1:WAAmvLK2rG0tCOqrf5XcLi2QUwugd4rcVJ/W3aoon9o=
|
||||||
|
gorm.io/driver/mysql v1.0.5/go.mod h1:N1OIhHAIhx5SunkMGqWbGFVeh4yTNWKmMo1GOAsohLI=
|
||||||
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
|
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
|
||||||
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
|
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
|
||||||
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
||||||
gorm.io/gorm v1.21.1 h1:ACwUZ+jzH8eG8zxgqTnMIdgWd+lGfCKZTUxL/uQ1ZQo=
|
gorm.io/gorm v1.21.1 h1:ACwUZ+jzH8eG8zxgqTnMIdgWd+lGfCKZTUxL/uQ1ZQo=
|
||||||
gorm.io/gorm v1.21.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
gorm.io/gorm v1.21.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
||||||
|
gorm.io/gorm v1.21.3 h1:qDFi55ZOsjZTwk5eN+uhAmHi8GysJ/qCTichM/yO7ME=
|
||||||
|
gorm.io/gorm v1.21.3/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
@ -1,24 +1,25 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
type Response struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
// common meta response
|
// MetaResponse common meta response
|
||||||
func MetaResponse(code int, msg string) gin.H {
|
func MetaResponse(code int, msg string) Response {
|
||||||
return gin.H{
|
return Response{
|
||||||
"meta": gin.H{
|
Code: code,
|
||||||
"code": code,
|
Data: nil,
|
||||||
"msg": msg,
|
Message: msg,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// common data response
|
// DataResponse common data response
|
||||||
func DataResponse(data interface{}) gin.H {
|
func DataResponse(data interface{}) Response {
|
||||||
return gin.H{
|
return Response{
|
||||||
"meta": gin.H{
|
Code: 200,
|
||||||
"code": 200,
|
Data: data,
|
||||||
"msg": "success",
|
Message: "ok",
|
||||||
},
|
|
||||||
"data": data,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,11 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/alidrive"
|
"github.com/Xhofe/alist/alidrive"
|
||||||
"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"
|
log "github.com/sirupsen/logrus"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DownReq struct {
|
type DownReq struct {
|
||||||
@ -31,7 +33,7 @@ func Down(c *gin.Context) {
|
|||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if fileModel.Password != "" && fileModel.Password != down.Password {
|
if fileModel.Password != "" && down.Password != utils.Get16MD5Encode(fileModel.Password) {
|
||||||
if down.Password == "" {
|
if down.Password == "" {
|
||||||
c.JSON(200, MetaResponse(401, "need password."))
|
c.JSON(200, MetaResponse(401, "need password."))
|
||||||
} else {
|
} else {
|
||||||
@ -43,7 +45,12 @@ func Down(c *gin.Context) {
|
|||||||
c.JSON(200, MetaResponse(406, "无法下载目录."))
|
c.JSON(200, MetaResponse(406, "无法下载目录."))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file, err := alidrive.GetDownLoadUrl(fileModel.FileId)
|
drive := utils.GetDriveByName(strings.Split(filePath, "/")[0])
|
||||||
|
if drive == nil {
|
||||||
|
c.JSON(200, MetaResponse(500, "找不到drive."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
file, err := alidrive.GetDownLoadUrl(fileModel.FileId, drive)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
|
@ -3,9 +3,11 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/alidrive"
|
"github.com/Xhofe/alist/alidrive"
|
||||||
"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"
|
log "github.com/sirupsen/logrus"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// get request bean
|
// get request bean
|
||||||
@ -40,7 +42,12 @@ func Get(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
down, err := alidrive.GetDownLoadUrl(file.FileId)
|
drive := utils.GetDriveByName(strings.Split(get.Path, "/")[0])
|
||||||
|
if drive == nil {
|
||||||
|
c.JSON(200, MetaResponse(500, "找不到drive."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
down, err := alidrive.GetDownLoadUrl(file.FileId, drive)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
|
@ -2,6 +2,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Xhofe/alist/alidrive"
|
"github.com/Xhofe/alist/alidrive"
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -12,16 +13,22 @@ type OfficePreviewReq struct {
|
|||||||
|
|
||||||
// handle office_preview request
|
// handle office_preview request
|
||||||
func OfficePreview(c *gin.Context) {
|
func OfficePreview(c *gin.Context) {
|
||||||
|
drive := utils.GetDriveByName(c.Param("drive"))
|
||||||
|
if drive == nil {
|
||||||
|
c.JSON(200, MetaResponse(400, "drive isn't exist."))
|
||||||
|
return
|
||||||
|
}
|
||||||
var req OfficePreviewReq
|
var req OfficePreviewReq
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf("preview_req:%+v", req)
|
log.Debugf("preview_req:%+v", req)
|
||||||
preview, err := alidrive.GetOfficePreviewUrl(req.FileId)
|
preview, err := alidrive.GetOfficePreviewUrl(req.FileId, drive)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
c.JSON(200, MetaResponse(500, err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
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()))
|
||||||
@ -44,6 +44,11 @@ func Path(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
// file
|
// file
|
||||||
if file.Type == "file" {
|
if file.Type == "file" {
|
||||||
|
if file.Password == "" {
|
||||||
|
file.Password = "n"
|
||||||
|
} else {
|
||||||
|
file.Password = "y"
|
||||||
|
}
|
||||||
c.JSON(200, DataResponse(file))
|
c.JSON(200, DataResponse(file))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -55,7 +60,11 @@ func Path(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
// delete password
|
// delete password
|
||||||
for i, _ := range *files {
|
for i, _ := range *files {
|
||||||
(*files)[i].Password = ""
|
if (*files)[i].Password == "" {
|
||||||
|
(*files)[i].Password = "n"
|
||||||
|
} else {
|
||||||
|
(*files)[i].Password = "y"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.JSON(200, DataResponse(files))
|
c.JSON(200, DataResponse(files))
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ 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/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// handle info request
|
// handle info request
|
||||||
@ -11,9 +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) {
|
||||||
password := c.Param("password")[1:]
|
var req RebuildReq
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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."))
|
||||||
@ -22,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(); err != nil {
|
if err := models.BuildTreeWithPath(req.Path, req.Depth); err != nil {
|
||||||
c.JSON(200, MetaResponse(500, err.Error()))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := models.BuildTree(); 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,13 +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(depth int) {
|
||||||
|
for i, _ := range conf.Conf.AliDrive.Drives {
|
||||||
|
if err := BuildTree(&conf.Conf.AliDrive.Drives[i], depth); err != nil {
|
||||||
|
log.Errorf("盘[%s]构建目录树失败:%s", conf.Conf.AliDrive.Drives[i].Name, err.Error())
|
||||||
|
} else {
|
||||||
|
log.Infof("盘[%s]构建目录树成功", conf.Conf.AliDrive.Drives[i].Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// build tree
|
// build tree
|
||||||
func BuildTree() 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() {
|
||||||
@ -22,59 +34,172 @@ func BuildTree() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
rootFile := File{
|
rootFile := File{
|
||||||
Dir: "",
|
Dir: "",
|
||||||
FileId: conf.Conf.AliDrive.RootFolder,
|
FileId: drive.RootFolder,
|
||||||
Name: "root",
|
Name: drive.Name,
|
||||||
Type: "folder",
|
Type: "folder",
|
||||||
|
Password: drive.Password,
|
||||||
}
|
}
|
||||||
if err := tx.Create(&rootFile).Error; err != nil {
|
if err := tx.Create(&rootFile).Error; err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := BuildOne(conf.Conf.AliDrive.RootFolder, "root/", tx, ""); 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) error {
|
/*
|
||||||
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, "", "", "")
|
递归构建目录树,插入指定目录下的所有文件
|
||||||
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); err != nil {
|
for _, file := range files.Items {
|
||||||
|
name := file.Name
|
||||||
|
password := parentPassword
|
||||||
|
if strings.HasSuffix(name, ".hide") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.Contains(name, ".ln-") {
|
||||||
|
index := strings.Index(name, ".ln-")
|
||||||
|
name = file.Name[:index]
|
||||||
|
fileId := file.Name[index+4:]
|
||||||
|
newFile := File{
|
||||||
|
Dir: path,
|
||||||
|
FileExtension: "",
|
||||||
|
FileId: fileId,
|
||||||
|
Name: name,
|
||||||
|
Type: "folder",
|
||||||
|
UpdatedAt: file.UpdatedAt,
|
||||||
|
Category: "",
|
||||||
|
ContentType: "",
|
||||||
|
Size: 0,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
log.Debugf("插入file:%+v", newFile)
|
||||||
|
if err = tx.Create(&newFile).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = BuildOne(fileId, fmt.Sprintf("%s%s/", path, name), tx, password, drive, depth-1); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
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,14 +18,18 @@ 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 {
|
||||||
return conf.DB.Create(file).Error
|
return conf.DB.Create(file).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func Clear() error {
|
func Clear(drive *conf.Drive) error {
|
||||||
return conf.DB.Where("1 = 1").Delete(&File{}).Error
|
if err := conf.DB.Where("dir = '' AND name = ?", drive.Name).Delete(&File{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return conf.DB.Where("dir like ?", fmt.Sprintf("%s%%", drive.Name)).Delete(&File{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFileByDirAndName(dir, name string) (*File, error) {
|
func GetFileByDirAndName(dir, name string) (*File, error) {
|
||||||
@ -59,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
|
||||||
|
}
|
@ -26,10 +26,11 @@ func InitApiRouter(engine *gin.Engine) {
|
|||||||
apiV2.GET("/info", controllers.Info)
|
apiV2.GET("/info", controllers.Info)
|
||||||
apiV2.POST("/get", controllers.Get)
|
apiV2.POST("/get", controllers.Get)
|
||||||
apiV2.POST("/path", controllers.Path)
|
apiV2.POST("/path", controllers.Path)
|
||||||
apiV2.POST("/office_preview", 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/*password", controllers.RebuildTree)
|
apiV2.POST("/rebuild", controllers.RebuildTree)
|
||||||
}
|
}
|
||||||
engine.GET("/d/*path", controllers.Down)
|
engine.GET("/d/*path", controllers.Down)
|
||||||
}
|
}
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/Xhofe/alist/alidrive"
|
|
||||||
"github.com/Xhofe/alist/bootstrap"
|
|
||||||
"github.com/Xhofe/alist/conf"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func setup() {
|
|
||||||
bootstrap.InitLog()
|
|
||||||
bootstrap.ReadConf("../conf.yml")
|
|
||||||
bootstrap.InitClient()
|
|
||||||
bootstrap.InitAliDrive()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetUserInfo(t *testing.T) {
|
|
||||||
user, err := alidrive.GetUserInfo()
|
|
||||||
fmt.Println(err)
|
|
||||||
fmt.Println(user)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetRoot(t *testing.T) {
|
|
||||||
files, err := alidrive.GetRoot(50, "", conf.OrderUpdatedAt, conf.DESC)
|
|
||||||
fmt.Println(err)
|
|
||||||
fmt.Println(files)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearch(t *testing.T) {
|
|
||||||
files, err := alidrive.Search("测试文件", 50, "")
|
|
||||||
fmt.Println(err)
|
|
||||||
fmt.Println(files)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
|
||||||
file, err := alidrive.GetFile("5fb7c80e85e4f335cd344008be1b1b5349f74414")
|
|
||||||
fmt.Println(err)
|
|
||||||
fmt.Println(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
setup()
|
|
||||||
code := m.Run()
|
|
||||||
os.Exit(code)
|
|
||||||
}
|
|
@ -2,6 +2,7 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/Xhofe/alist/utils"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -14,14 +15,18 @@ func TestSplit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPassword(t *testing.T) {
|
func TestPassword(t *testing.T) {
|
||||||
fullName:="hello.password-xhf"
|
fullName := "hello.password-xhf"
|
||||||
index:=strings.Index(fullName,".password-")
|
index := strings.Index(fullName, ".password-")
|
||||||
name:=fullName[:index]
|
name := fullName[:index]
|
||||||
password:=fullName[index+10:]
|
password := fullName[index+10:]
|
||||||
fmt.Printf("name:%s, password:%s\n",name,password)
|
fmt.Printf("name:%s, password:%s\n", name, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDir(t *testing.T) {
|
func TestDir(t *testing.T) {
|
||||||
dir,file:=filepath.Split("root")
|
dir, file := filepath.Split("root")
|
||||||
fmt.Printf("dir:%s\nfile:%s\n",dir,file)
|
fmt.Printf("dir:%s\nfile:%s\n", dir, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMD5(t *testing.T) {
|
||||||
|
fmt.Printf("%s\n", utils.Get16MD5Encode("123456"))
|
||||||
|
}
|
||||||
|
@ -2,7 +2,6 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Xhofe/alist/alidrive"
|
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/utils"
|
"github.com/Xhofe/alist/utils"
|
||||||
"testing"
|
"testing"
|
||||||
@ -13,7 +12,5 @@ func TestStr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteYml(t *testing.T) {
|
func TestWriteYml(t *testing.T) {
|
||||||
alidrive.RefreshToken()
|
|
||||||
utils.WriteToYml("../conf.yml", conf.Conf)
|
utils.WriteToYml("../conf.yml", conf.Conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
utils/config.go
Normal file
22
utils/config.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "github.com/Xhofe/alist/conf"
|
||||||
|
|
||||||
|
func GetDriveByName(name string) *conf.Drive {
|
||||||
|
for i, drive := range conf.Conf.AliDrive.Drives{
|
||||||
|
if drive.Name == name {
|
||||||
|
return &conf.Conf.AliDrive.Drives[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNames() []string {
|
||||||
|
res := make([]string, 0)
|
||||||
|
for _, drive := range conf.Conf.AliDrive.Drives{
|
||||||
|
if !drive.Hide {
|
||||||
|
res = append(res, drive.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
18
utils/md5.go
Normal file
18
utils/md5.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
//返回一个32位md5加密后的字符串
|
||||||
|
func GetMD5Encode(data string) string {
|
||||||
|
h := md5.New()
|
||||||
|
h.Write([]byte(data))
|
||||||
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
//返回一个16位md5加密后的字符串
|
||||||
|
func Get16MD5Encode(data string) string {
|
||||||
|
return GetMD5Encode(data)[8:24]
|
||||||
|
}
|
Reference in New Issue
Block a user