alist/server/handles/fsread.go

250 lines
5.9 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-27 11:51:23 +00:00
import (
2022-06-29 08:23:31 +00:00
"fmt"
"github.com/alist-org/alist/v3/internal/sign"
2022-06-28 10:12:53 +00:00
stdpath "path"
2022-06-29 08:23:31 +00:00
"strings"
2022-06-28 10:12:53 +00:00
"time"
2022-06-27 11:51:23 +00:00
"github.com/alist-org/alist/v3/internal/db"
2022-06-28 10:00:11 +00:00
"github.com/alist-org/alist/v3/internal/errs"
2022-06-27 11:51:23 +00: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 10:00:11 +00:00
"github.com/pkg/errors"
2022-06-27 11:51:23 +00:00
)
type ListReq struct {
common.PageReq
Path string `json:"path" form:"path"`
Password string `json:"password" form:"password"`
}
2022-07-10 06:09:31 +00:00
type DirReq struct {
Path string `json:"path" form:"path"`
Password string `json:"password" form:"password"`
}
2022-06-27 11:51:23 +00:00
type ObjResp struct {
Name string `json:"name"`
Size int64 `json:"size"`
IsDir bool `json:"is_dir"`
Modified time.Time `json:"modified"`
2022-06-28 07:12:40 +00:00
Sign string `json:"sign"`
2022-06-27 11:51:23 +00:00
}
2022-06-27 13:15:39 +00:00
type FsListResp struct {
2022-06-27 11:51:23 +00:00
Content []ObjResp `json:"content"`
Total int64 `json:"total"`
2022-06-30 07:41:58 +00:00
Readme string `json:"readme"`
2022-06-30 07:53:57 +00:00
Write bool `json:"write"`
2022-06-27 11:51:23 +00:00
}
2022-06-27 13:15:39 +00:00
func FsList(c *gin.Context) {
2022-06-27 11:51:23 +00: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 12:37:05 +00:00
req.Path = stdpath.Join(user.BasePath, req.Path)
2022-06-28 10:00:11 +00: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 11:51:23 +00:00
c.Set("meta", meta)
if !canAccess(user, meta, req.Path, req.Password) {
2022-07-10 06:09:31 +00:00
common.ErrorStrResp(c, "password is incorrect", 403)
2022-06-27 11:51:23 +00:00
return
}
objs, err := fs.List(c, req.Path)
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500)
2022-06-27 11:51:23 +00:00
return
}
total, objs := pagination(objs, &req.PageReq)
2022-06-27 13:15:39 +00:00
common.SuccessResp(c, FsListResp{
2022-06-29 08:08:55 +00:00
Content: toObjResp(objs),
2022-06-27 11:51:23 +00:00
Total: int64(total),
2022-06-30 07:41:58 +00:00
Readme: getReadme(meta, req.Path),
2022-06-30 07:53:57 +00:00
Write: user.CanWrite() || canWrite(meta, req.Path),
2022-06-27 11:51:23 +00:00
})
}
2022-07-10 06:09:31 +00:00
func FsDirs(c *gin.Context) {
var req DirReq
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, true)
return
}
}
c.Set("meta", meta)
if !canAccess(user, meta, req.Path, req.Password) {
common.ErrorStrResp(c, "password is incorrect", 403)
return
}
objs, err := fs.List(c, req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
dirs := filterDirs(objs)
common.SuccessResp(c, dirs)
}
type DirResp struct {
Name string `json:"name"`
Modified time.Time `json:"modified"`
}
func filterDirs(objs []model.Obj) []DirResp {
var dirs []DirResp
for _, obj := range objs {
if obj.IsDir() {
dirs = append(dirs, DirResp{
Name: obj.GetName(),
Modified: obj.ModTime(),
})
}
}
return dirs
}
2022-06-30 07:41:58 +00: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 11:51:23 +00:00
func canAccess(user *model.User, meta *model.Meta, path string, password string) bool {
// if is not guest, can access
2022-06-29 10:03:12 +00:00
if user.CanAccessWithoutPassword() {
2022-06-27 11:51:23 +00: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 07:41:58 +00:00
if !utils.PathEqual(meta.Path, path) && !meta.PSub {
2022-06-27 11:51:23 +00: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 08:08:55 +00:00
func toObjResp(objs []model.Obj) []ObjResp {
2022-06-27 11:51:23 +00: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 13:58:46 +00:00
Sign: common.Sign(obj),
2022-06-27 11:51:23 +00:00
})
}
return resp
}
2022-06-29 08:08:55 +00:00
2022-06-29 09:08:31 +00:00
type FsGetOrLinkReq struct {
2022-06-29 08:08:55 +00: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 09:08:31 +00:00
var req FsGetOrLinkReq
2022-06-29 08:08:55 +00: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) {
2022-07-10 06:09:31 +00:00
common.ErrorStrResp(c, "password is incorrect", 403)
2022-06-29 08:08:55 +00:00
return
}
obj, err := fs.Get(c, req.Path)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-06-29 08:23:31 +00:00
var rawURL string
2022-07-08 07:56:29 +00:00
// file have raw url
if !obj.IsDir() {
if u, ok := obj.(model.URL); ok {
rawURL = u.URL()
2022-06-29 08:23:31 +00:00
} else {
2022-07-10 06:45:39 +00:00
storage, _ := fs.GetStorage(req.Path)
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
if storage.GetStorage().DownProxyUrl != "" {
rawURL = fmt.Sprintf("%s%s?sign=%s", strings.Split(storage.GetStorage().DownProxyUrl, "\n")[0], req.Path, sign.Sign(obj.GetName()))
2022-07-08 07:56:29 +00:00
} else {
rawURL = fmt.Sprintf("%s/p%s?sign=%s", common.GetBaseUrl(c.Request), req.Path, sign.Sign(obj.GetName()))
}
} else {
2022-07-10 06:45:39 +00:00
// if storage is not proxy, use raw url by fs.Link
link, _, err := fs.Link(c, req.Path, model.LinkArgs{IP: c.ClientIP()})
2022-07-08 07:56:29 +00:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
rawURL = link.URL
2022-06-29 08:23:31 +00:00
}
}
}
2022-06-29 08:08:55 +00: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 08:23:31 +00:00
RawURL: rawURL,
2022-06-29 08:08:55 +00:00
})
}