194 lines
4.7 KiB
Go
Raw Normal View History

2022-06-27 19:51:23 +08:00
package controllers
import (
2022-06-29 16:23:31 +08:00
"fmt"
"github.com/alist-org/alist/v3/internal/sign"
2022-06-28 18:12:53 +08:00
stdpath "path"
2022-06-29 16:23:31 +08:00
"strings"
2022-06-28 18:12:53 +08:00
"time"
2022-06-27 19:51:23 +08:00
"github.com/alist-org/alist/v3/internal/db"
2022-06-28 18:00:11 +08:00
"github.com/alist-org/alist/v3/internal/errs"
2022-06-27 19:51:23 +08:00
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
2022-06-28 18:00:11 +08:00
"github.com/pkg/errors"
2022-06-27 19:51:23 +08:00
)
type ListReq struct {
common.PageReq
Path string `json:"path" form:"path"`
Password string `json:"password" form:"password"`
}
type ObjResp struct {
Name string `json:"name"`
Size int64 `json:"size"`
IsDir bool `json:"is_dir"`
Modified time.Time `json:"modified"`
2022-06-28 15:12:40 +08:00
Sign string `json:"sign"`
2022-06-27 19:51:23 +08:00
}
2022-06-27 21:15:39 +08:00
type FsListResp struct {
2022-06-27 19:51:23 +08:00
Content []ObjResp `json:"content"`
Total int64 `json:"total"`
2022-06-30 15:41:58 +08:00
Readme string `json:"readme"`
2022-06-27 19:51:23 +08:00
}
2022-06-27 21:15:39 +08:00
func FsList(c *gin.Context) {
2022-06-27 19:51:23 +08:00
var req ListReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Validate()
user := c.MustGet("user").(*model.User)
2022-06-27 20:37:05 +08:00
req.Path = stdpath.Join(user.BasePath, req.Path)
2022-06-28 18:00:11 +08:00
meta, err := db.GetNearestMeta(req.Path)
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
return
}
}
2022-06-27 19:51:23 +08:00
c.Set("meta", meta)
if !canAccess(user, meta, req.Path, req.Password) {
common.ErrorStrResp(c, "password is incorrect", 401)
return
}
objs, err := fs.List(c, req.Path)
if err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 500)
2022-06-27 19:51:23 +08:00
return
}
total, objs := pagination(objs, &req.PageReq)
2022-06-27 21:15:39 +08:00
common.SuccessResp(c, FsListResp{
2022-06-29 16:08:55 +08:00
Content: toObjResp(objs),
2022-06-27 19:51:23 +08:00
Total: int64(total),
2022-06-30 15:41:58 +08:00
Readme: getReadme(meta, req.Path),
2022-06-27 19:51:23 +08:00
})
}
2022-06-30 15:41:58 +08:00
func getReadme(meta *model.Meta, path string) string {
if meta != nil && (utils.PathEqual(meta.Path, path) || meta.RSub) {
return meta.Readme
}
return ""
}
2022-06-27 19:51:23 +08:00
func canAccess(user *model.User, meta *model.Meta, path string, password string) bool {
// if is not guest, can access
2022-06-29 18:03:12 +08:00
if user.CanAccessWithoutPassword() {
2022-06-27 19:51:23 +08:00
return true
}
// if meta is nil or password is empty, can access
if meta == nil || meta.Password == "" {
return true
}
// if meta doesn't apply to sub_folder, can access
2022-06-30 15:41:58 +08:00
if !utils.PathEqual(meta.Path, path) && !meta.PSub {
2022-06-27 19:51:23 +08:00
return true
}
// validate password
return meta.Password == password
}
func pagination(objs []model.Obj, req *common.PageReq) (int, []model.Obj) {
pageIndex, pageSize := req.PageIndex, req.PageSize
total := len(objs)
start := (pageIndex - 1) * pageSize
if start > total {
return total, []model.Obj{}
}
end := start + pageSize
if end > total {
end = total
}
return total, objs[start:end]
}
2022-06-29 16:08:55 +08:00
func toObjResp(objs []model.Obj) []ObjResp {
2022-06-27 19:51:23 +08:00
var resp []ObjResp
for _, obj := range objs {
resp = append(resp, ObjResp{
Name: obj.GetName(),
Size: obj.GetSize(),
IsDir: obj.IsDir(),
Modified: obj.ModTime(),
2022-06-28 21:58:46 +08:00
Sign: common.Sign(obj),
2022-06-27 19:51:23 +08:00
})
}
return resp
}
2022-06-29 16:08:55 +08:00
2022-06-29 17:08:31 +08:00
type FsGetOrLinkReq struct {
2022-06-29 16:08:55 +08:00
Path string `json:"path" form:"path"`
Password string `json:"password" form:"password"`
}
type FsGetResp struct {
ObjResp
RawURL string `json:"raw_url"`
}
func FsGet(c *gin.Context) {
2022-06-29 17:08:31 +08:00
var req FsGetOrLinkReq
2022-06-29 16:08:55 +08:00
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
req.Path = stdpath.Join(user.BasePath, req.Path)
meta, err := db.GetNearestMeta(req.Path)
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500)
return
}
}
c.Set("meta", meta)
if !canAccess(user, meta, req.Path, req.Password) {
common.ErrorStrResp(c, "password is incorrect", 401)
return
}
obj, err := fs.Get(c, req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-06-29 16:23:31 +08:00
var rawURL string
// obj have raw url
if u, ok := obj.(model.URL); ok {
rawURL = u.URL()
} else {
account, _ := fs.GetAccount(req.Path)
if account.Config().MustProxy() || account.GetAccount().WebProxy {
if account.GetAccount().DownProxyUrl != "" {
rawURL = fmt.Sprintf("%s%s?sign=%s", strings.Split(account.GetAccount().DownProxyUrl, "\n")[0], req.Path, sign.Sign(obj.GetName()))
} else {
rawURL = fmt.Sprintf("%s/p%s?sign=%s", common.GetBaseUrl(c.Request), req.Path, sign.Sign(obj.GetName()))
}
} else {
// if account is not proxy, use raw url by fs.Link
link, _, err := fs.Link(c, req.Path, model.LinkArgs{})
if err != nil {
common.ErrorResp(c, err, 500)
return
}
rawURL = link.URL
}
}
2022-06-29 16:08:55 +08:00
common.SuccessResp(c, FsGetResp{
ObjResp: ObjResp{
Name: obj.GetName(),
Size: obj.GetSize(),
IsDir: obj.IsDir(),
Modified: obj.ModTime(),
Sign: common.Sign(obj),
},
2022-06-29 16:23:31 +08:00
RawURL: rawURL,
2022-06-29 16:08:55 +08:00
})
}