refactor: init v3

This commit is contained in:
Noah Hsu
2022-06-06 16:28:37 +08:00
parent eb15bce24b
commit b76060570e
185 changed files with 14 additions and 30438 deletions

View File

@ -1,106 +0,0 @@
package controllers
import (
"fmt"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strconv"
"time"
)
func GetAccounts(c *gin.Context) {
accounts, err := model.GetAccounts()
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, accounts)
}
func CreateAccount(c *gin.Context) {
var req model.Account
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
driver, ok := base.GetDriver(req.Type)
if !ok {
common.ErrorStrResp(c, fmt.Sprintf("No [%s] driver", req.Type), 400)
return
}
now := time.Now()
req.UpdatedAt = &now
if err := model.CreateAccount(&req); err != nil {
common.ErrorResp(c, err, 500)
} else {
log.Debugf("new account: %+v", req)
//err = driver.Save(&req, nil)
err = operate.Save(driver, &req, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
}
func SaveAccount(c *gin.Context) {
var req model.Account
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
driver, ok := base.GetDriver(req.Type)
if !ok {
common.ErrorStrResp(c, fmt.Sprintf("No [%s] driver", req.Type), 400)
return
}
old, err := model.GetAccountById(req.ID)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
now := time.Now()
req.UpdatedAt = &now
if old.Name != req.Name {
model.DeleteAccountFromMap(old.Name)
}
if err := model.SaveAccount(&req); err != nil {
common.ErrorResp(c, err, 500)
} else {
log.Debugf("save account: %+v", req)
//err = driver.Save(&req, old)
err = operate.Save(driver, &req, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
}
func DeleteAccount(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if account, err := model.DeleteAccount(uint(id)); err != nil {
common.ErrorResp(c, err, 500)
return
} else {
driver, ok := base.GetDriver(account.Type)
if ok {
//_ = driver.Save(nil, account)
_ = operate.Save(driver, nil, account)
} else {
log.Errorf("no driver: %s", account.Type)
}
}
common.SuccessResp(c)
}

View File

@ -1,16 +0,0 @@
package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
func ClearCache(c *gin.Context) {
err := conf.Cache.Clear(conf.Ctx)
if err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}

View File

@ -1,32 +0,0 @@
package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func Down(c *gin.Context) {
rawPath := c.Param("path")
rawPath = utils.ParsePath(rawPath)
log.Debugf("down: %s", rawPath)
account, path_, driver, err := common.ParsePath(rawPath)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if driver.Config().OnlyProxy || account.Proxy || utils.IsContain(conf.DProxyTypes, utils.Ext(rawPath)) {
Proxy(c)
return
}
link, err := driver.Link(base.Args{Path: path_, IP: c.ClientIP()}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
c.Redirect(302, link.Url)
return
}

View File

@ -1,11 +0,0 @@
package controllers
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
func GetDrivers(c *gin.Context) {
common.SuccessResp(c, base.GetDrivers())
}

View File

@ -1,61 +0,0 @@
package file
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
func Copy(c *gin.Context) {
var req MoveCopyReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if len(req.Names) == 0 {
common.ErrorStrResp(c, "Empty file names", 400)
return
}
if model.AccountsCount() > 1 && (req.SrcDir == "/" || req.DstDir == "/") {
common.ErrorStrResp(c, "Can't operate root folder", 400)
return
}
srcAccount, srcPath, srcDriver, err := common.ParsePath(utils.Join(req.SrcDir, req.Names[0]))
if err != nil {
common.ErrorResp(c, err, 500)
return
}
dstAccount, dstPath, _, err := common.ParsePath(utils.Join(req.DstDir, req.Names[0]))
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if srcAccount.Name != dstAccount.Name {
common.ErrorStrResp(c, "Can't copy files between two accounts", 400)
return
}
if srcPath == "/" || dstPath == "/" {
common.ErrorStrResp(c, "Can't copy root folder", 400)
return
}
srcDir, dstDir := utils.Dir(srcPath), utils.Dir(dstPath)
for i, name := range req.Names {
clearCache := false
if i == len(req.Names)-1 {
clearCache = true
}
err := operate.Copy(srcDriver, srcAccount, utils.Join(srcDir, name), utils.Join(dstDir, name), clearCache)
if err != nil {
if i == 0 {
_ = base.DeleteCache(srcDir, srcAccount)
_ = base.DeleteCache(dstDir, dstAccount)
}
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

View File

@ -1,50 +0,0 @@
package file
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
type DeleteFilesReq struct {
Path string `json:"path"`
Names []string `json:"names"`
}
func DeleteFiles(c *gin.Context) {
var req DeleteFilesReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if len(req.Names) == 0 {
common.ErrorStrResp(c, "Empty file names", 400)
return
}
for i, name := range req.Names {
account, path_, driver, err := common.ParsePath(utils.Join(req.Path, name))
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if path_ == "/" {
common.ErrorStrResp(c, "Delete root folder is not allowed", 400)
return
}
clearCache := false
if i == len(req.Names)-1 {
clearCache = true
}
err = operate.Delete(driver, account, path_, clearCache)
if err != nil {
if i == 0 {
_ = base.DeleteCache(utils.Dir(path_), account)
}
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

View File

@ -1,39 +0,0 @@
package file
import (
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
type FolderReq struct {
Path string `json:"path"`
}
func Folder(c *gin.Context) {
var req FolderReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
var files = make([]model.File, 0)
_, rawFiles, _, _, _, err := common.Path(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if rawFiles == nil {
common.ErrorStrResp(c, "not a folder", 400)
return
}
for _, file := range rawFiles {
if file.IsDir() {
files = append(files, file)
}
}
c.JSON(200, common.Resp{
Code: 200,
Message: "success",
Data: files,
})
}

View File

@ -1,34 +0,0 @@
package file
import (
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
type MkdirReq struct {
Path string `json:"path"`
}
func Mkdir(c *gin.Context) {
var req MkdirReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
account, path_, driver, err := common.ParsePath(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if path_ == "/" {
common.ErrorStrResp(c, "Folder name can't be empty", 400)
return
}
err = operate.MakeDir(driver, account, path_, true)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

View File

@ -1,67 +0,0 @@
package file
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
type MoveCopyReq struct {
SrcDir string `json:"src_dir"`
DstDir string `json:"dst_dir"`
Names []string `json:"names"`
}
func Move(c *gin.Context) {
var req MoveCopyReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if len(req.Names) == 0 {
common.ErrorStrResp(c, "Empty file names", 400)
return
}
if model.AccountsCount() > 1 && (req.SrcDir == "/" || req.DstDir == "/") {
common.ErrorStrResp(c, "Can't operate root folder", 400)
return
}
srcAccount, srcPath, srcDriver, err := common.ParsePath(utils.Join(req.SrcDir, req.Names[0]))
if err != nil {
common.ErrorResp(c, err, 500)
return
}
dstAccount, dstPath, _, err := common.ParsePath(utils.Join(req.DstDir, req.Names[0]))
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if srcAccount.Name != dstAccount.Name {
common.ErrorStrResp(c, "Can't move files between two accounts", 400)
return
}
if srcPath == "/" || dstPath == "/" {
common.ErrorStrResp(c, "Can't move root folder", 400)
return
}
srcDir, dstDir := utils.Dir(srcPath), utils.Dir(dstPath)
for i, name := range req.Names {
clearCache := false
if i == len(req.Names)-1 {
clearCache = true
}
err := operate.Move(srcDriver, srcAccount, utils.Join(srcDir, name), utils.Join(dstDir, name), clearCache)
if err != nil {
if i == 0 {
_ = base.DeleteCache(srcDir, srcAccount)
_ = base.DeleteCache(dstDir, dstAccount)
}
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

View File

@ -1,36 +0,0 @@
package file
import (
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
type RenameReq struct {
Path string `json:"path"`
Name string `json:"name"`
}
func Rename(c *gin.Context) {
var req RenameReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
account, path_, driver, err := common.ParsePath(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if path_ == "/" {
common.ErrorStrResp(c, "Can't edit account name here", 400)
return
}
err = operate.Move(driver, account, path_, utils.Join(utils.Dir(path_), req.Name), true)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

View File

@ -1,34 +0,0 @@
package file
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
type RefreshReq struct {
Path string `json:"path"`
}
func RefreshFolder(c *gin.Context) {
var req RefreshReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
account, path_, _, err := common.ParsePath(req.Path)
if err != nil {
if err.Error() == "path not found" && req.Path == "/" {
common.SuccessResp(c)
return
}
common.ErrorResp(c, err, 500)
return
}
err = base.DeleteCache(path_, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

View File

@ -1,65 +0,0 @@
package file
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
func UploadFiles(c *gin.Context) {
path := c.PostForm("path")
path = utils.ParsePath(path)
token := c.GetHeader("Authorization")
if token != conf.Token {
password := c.PostForm("password")
meta, _ := model.GetMetaByPath(path)
if meta == nil || !meta.Upload {
common.ErrorStrResp(c, "Not allow upload", 403)
return
}
if meta.Password != "" && meta.Password != password {
common.ErrorStrResp(c, "Wrong password", 403)
return
}
}
account, path_, driver, err := common.ParsePath(path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
form, err := c.MultipartForm()
if err != nil {
common.ErrorResp(c, err, 400)
}
files := form.File["files"]
if err != nil {
return
}
for i, file := range files {
open, err := file.Open()
fileStream := model.FileStream{
File: open,
Size: uint64(file.Size),
ParentPath: path_,
Name: file.Filename,
MIMEType: file.Header.Get("Content-Type"),
}
clearCache := false
if i == len(files)-1 {
clearCache = true
}
err = operate.Upload(driver, account, &fileStream, clearCache)
if err != nil {
if i != 0 {
_ = base.DeleteCache(path_, account)
}
common.ErrorResp(c, err, 500)
return
}
}
common.SuccessResp(c)
}

View File

@ -1,47 +0,0 @@
package controllers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
type LinkReq struct {
Path string `json:"path"`
//Password string `json:"password"`
}
// Link 返回真实的链接,且携带头,只提供给中转程序使用
func Link(c *gin.Context) {
var req LinkReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.ParsePath(req.Path)
rawPath := req.Path
rawPath = utils.ParsePath(rawPath)
log.Debugf("link: %s", rawPath)
account, path, driver, err := common.ParsePath(rawPath)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if driver.Config().OnlyLocal {
common.SuccessResp(c, base.Link{
Url: fmt.Sprintf("//%s/p%s?d=1&sign=%s", c.Request.Host, req.Path, utils.SignWithToken(utils.Base(rawPath), conf.Token)),
})
return
}
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, link)
return
}

View File

@ -1,61 +0,0 @@
package controllers
import (
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
"strconv"
)
func GetMetas(c *gin.Context) {
metas,err := model.GetMetas()
if err != nil {
common.ErrorResp(c,err,500)
return
}
common.SuccessResp(c, metas)
}
func CreateMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.ParsePath(req.Path)
if err := model.CreateMeta(req); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}
func SaveMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.ParsePath(req.Path)
if err := model.SaveMeta(req); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}
func DeleteMeta(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
//path = utils.ParsePath(path)
if err := model.DeleteMeta(uint(id)); err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

View File

@ -1,80 +0,0 @@
package controllers
import (
"encoding/base64"
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
"net/url"
"strings"
)
func Favicon(c *gin.Context) {
c.Redirect(302, conf.GetStr("favicon"))
}
func Plist(c *gin.Context) {
data := c.Param("data")
data = strings.ReplaceAll(data, "_", "/")
data = strings.ReplaceAll(data, "-", "=")
bytes, err := base64.StdEncoding.DecodeString(data)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
u := string(bytes)
uUrl, err := url.Parse(u)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
name := utils.Base(u)
u = uUrl.String()
ipaIndex := strings.Index(name, ".ipa")
decodeName := name
if ipaIndex != -1 {
name = name[:ipaIndex]
decodeName = name
tmp, err := url.PathUnescape(name)
if err == nil {
decodeName = tmp
}
}
name = strings.ReplaceAll(name, "<", "[")
name = strings.ReplaceAll(name, ">", "]")
plist := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>%s</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>ci.nn.%s</string>
<key>bundle-version</key>
<string>4.4</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>%s</string>
</dict>
</dict>
</array>
</dict>
</plist>`, u, name, decodeName)
c.Header("Content-Type", "application/xml;charset=utf-8")
c.Status(200)
_, _ = c.Writer.WriteString(plist)
}

View File

@ -1,168 +0,0 @@
package controllers
import (
"errors"
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strings"
)
func Pagination(files []model.File, req *common.PathReq) (int, []model.File) {
pageNum, pageSize := req.PageNum, req.PageSize
total := len(files)
if isAll(req) {
return total, files
}
switch conf.GetStr("load type") {
case "all":
return total, files
//case "pagination":
//
}
start := (pageNum - 1) * pageSize
if start > total {
return total, []model.File{}
}
end := start + pageSize
if end > total {
end = total
}
return total, files[start:end]
}
func isAll(req *common.PathReq) bool {
return req.PageNum == 0 && req.PageSize == 0
}
func CheckPagination(req *common.PathReq) error {
if isAll(req) {
return nil
}
if conf.GetStr("loading type") == "all" {
return nil
}
if req.PageNum < 1 {
return errors.New("page_num can't be less than 1")
}
if req.PageSize == 0 {
req.PageSize = conf.GetInt("default page size", 30)
}
return nil
}
type Meta struct {
Driver string `json:"driver"`
Upload bool `json:"upload"`
Total int `json:"total"`
Readme string `json:"readme"`
//Pages int `json:"pages"`
}
type PathResp struct {
Type string `json:"type"`
Meta Meta `json:"meta"`
Files []model.File `json:"files"`
}
func Path(c *gin.Context) {
reqV, _ := c.Get("req")
req := reqV.(common.PathReq)
_, ok := c.Get("admin")
meta, _ := model.GetMetaByPath(req.Path)
upload := false
readme := ""
if meta != nil {
upload = meta.Upload
readme = meta.Readme
}
err := CheckPagination(&req)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
file, files, account, driver, path, err := common.Path(req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if file != nil {
// 对于中转文件或只能中转,将链接修改为中转链接
if driver.Config().OnlyProxy || account.Proxy {
if account.DownProxyUrl != "" {
file.Url = fmt.Sprintf("%s%s?sign=%s", strings.Split(account.DownProxyUrl, "\n")[0], req.Path, utils.SignWithToken(file.Name, conf.Token))
} else {
file.Url = fmt.Sprintf("//%s/p%s?sign=%s", c.Request.Host, req.Path, utils.SignWithToken(file.Name, conf.Token))
}
} else if !driver.Config().NoNeedSetLink {
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
file.Url = link.Url
}
c.JSON(200, common.Resp{
Code: 200,
Message: "success",
Data: PathResp{
Type: "file",
Meta: Meta{
Driver: driver.Config().Name,
},
Files: []model.File{*file},
},
})
} else {
if !ok {
files = common.Hide(meta, files)
}
driverName := "root"
if driver != nil {
if driver.Config().LocalSort {
model.SortFiles(files, account)
}
model.ExtractFolder(files, account)
driverName = driver.Config().Name
}
total, files := Pagination(files, &req)
c.JSON(200, common.Resp{
Code: 200,
Message: "success",
Data: PathResp{
Type: "folder",
Meta: Meta{
Driver: driverName,
Upload: upload,
Total: total,
Readme: readme,
},
Files: files,
},
})
}
}
func Preview(c *gin.Context) {
reqV, _ := c.Get("req")
req := reqV.(common.PathReq)
rawPath := req.Path
rawPath = utils.ParsePath(rawPath)
log.Debugf("preview: %s", rawPath)
account, path, driver, err := common.ParsePath(rawPath)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
data, err := driver.Preview(path, account)
if err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c, data)
}
}

View File

@ -1,101 +0,0 @@
package controllers
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"path/filepath"
"strings"
)
func Proxy(c *gin.Context) {
rawPath := c.Param("path")
rawPath = utils.ParsePath(rawPath)
log.Debugf("proxy: %s", rawPath)
account, path, driver, err := common.ParsePath(rawPath)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
// 只有以下几种情况允许中转:
// 1. 账号开启中转
// 2. driver只能中转
// 3. 是文本类型文件
// 4. 开启webdav中转需要验证sign
if !account.Proxy && !driver.Config().OnlyProxy &&
utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT &&
!utils.IsContain(conf.DProxyTypes, utils.Ext(rawPath)) {
// 只开启了webdav中转验证sign
ok := false
if account.WebdavProxy {
_, ok = c.Get("sign")
}
if !ok {
common.ErrorStrResp(c, fmt.Sprintf("[%s] not allowed proxy", account.Name), 403)
return
}
}
// 中转时有中转机器使用中转机器,若携带标志位则表明不能再走中转机器了
if account.DownProxyUrl != "" && c.Query("d") != "1" {
name := utils.Base(rawPath)
link := fmt.Sprintf("%s%s?sign=%s", strings.Split(account.DownProxyUrl, "\n")[0], rawPath, utils.SignWithToken(name, conf.Token))
c.Redirect(302, link)
return
}
// 检查文件
file, err := operate.File(driver, account, path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
// 对于中转不需要重设IP
link, err := driver.Link(base.Args{Path: path, Header: c.Request.Header}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
err = common.Proxy(c.Writer, c.Request, link, file)
log.Debugln("web proxy error:", err)
if err != nil {
common.ErrorResp(c, err, 500)
}
}
var client *resty.Client
func init() {
client = resty.New()
client.SetRetryCount(3).SetTimeout(base.DefaultTimeout)
}
func Text(c *gin.Context, link *base.Link) {
req := client.R()
if link.Headers != nil {
for _, header := range link.Headers {
req.SetHeader(header.Name, header.Value)
}
}
res, err := req.Get(link.Url)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
text := res.String()
t := utils.GetStrCoding(res.Body())
log.Debugf("text type: %s", t)
if t != utils.UTF8 {
body, err := utils.GbkToUtf8(res.Body())
if err != nil {
common.ErrorResp(c, err, 500)
return
}
text = string(body)
}
c.String(200, text)
}

View File

@ -1,31 +0,0 @@
package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
type SearchReq struct {
Path string `json:"path"`
Keyword string `json:"keyword"`
}
func Search(c *gin.Context) {
if !conf.GetBool("enable search") {
common.ErrorStrResp(c, "Not allowed search", 403)
return
}
var req SearchReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
files, err := model.SearchByNameAndPath(req.Path, req.Keyword)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, files)
}

View File

@ -1,71 +0,0 @@
package controllers
import (
"github.com/Xhofe/alist/drivers"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
"strconv"
)
func SaveSettings(c *gin.Context) {
var req []model.SettingItem
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := model.SaveSettings(req); err != nil {
common.ErrorResp(c, err, 500)
} else {
model.LoadSettings()
common.SuccessResp(c)
}
}
func GetSettings(c *gin.Context) {
groupStr := c.Query("group")
var settings []model.SettingItem
var err error
if groupStr == "" {
settings, err = model.GetSettings()
} else {
group, err := strconv.Atoi(groupStr)
if err == nil {
settings, err = model.GetSettingsByGroup(group)
}
}
if err != nil {
common.ErrorResp(c, err, 400)
return
}
common.SuccessResp(c, settings)
}
func GetSettingsPublic(c *gin.Context) {
settings, err := model.GetSettingsPublic()
if err != nil {
common.ErrorResp(c, err, 400)
return
}
settings = append(settings, []model.SettingItem{{
Key: "no cors",
Value: drivers.NoCors,
Description: "",
Type: "string",
}, {
Key: "no upload",
Value: drivers.NoUpload,
Description: "",
Type: "string",
}}...)
common.SuccessResp(c, settings)
}
func DeleteSetting(c *gin.Context) {
key := c.Query("key")
if err := model.DeleteSetting(key); err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}