alist/server/handles/fsread.go

398 lines
10 KiB
Go
Raw Permalink 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"
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"
"github.com/alist-org/alist/v3/internal/conf"
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/internal/op"
"github.com/alist-org/alist/v3/internal/setting"
2022-08-03 05:03:45 +00:00
"github.com/alist-org/alist/v3/internal/sign"
2022-06-27 11:51:23 +00:00
"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 {
model.PageReq
2022-06-27 11:51:23 +00:00
Path string `json:"path" form:"path"`
Password string `json:"password" form:"password"`
2022-08-29 11:15:52 +00:00
Refresh bool `json:"refresh"`
2022-06-27 11:51:23 +00:00
}
2022-07-10 06:09:31 +00:00
type DirReq struct {
Path string `json:"path" form:"path"`
Password string `json:"password" form:"password"`
ForceRoot bool `json:"force_root" form:"force_root"`
2022-07-10 06:09:31 +00:00
}
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"`
Created time.Time `json:"created"`
Sign string `json:"sign"`
Thumb string `json:"thumb"`
Type int `json:"type"`
HashInfoStr string `json:"hashinfo"`
HashInfo map[*utils.HashType]string `json:"hash_info"`
2022-06-27 11:51:23 +00:00
}
2022-06-27 13:15:39 +00:00
type FsListResp struct {
2022-09-22 08:04:10 +00:00
Content []ObjResp `json:"content"`
Total int64 `json:"total"`
Readme string `json:"readme"`
Header string `json:"header"`
2022-09-22 08:04:10 +00:00
Write bool `json:"write"`
Provider string `json:"provider"`
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)
reqPath, err := user.JoinPath(req.Path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
meta, err := op.GetNearestMeta(reqPath)
2022-06-28 10:00:11 +00:00
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 !common.CanAccess(user, meta, reqPath, req.Password) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
2022-06-27 11:51:23 +00:00
return
}
if !user.CanWrite() && !common.CanWrite(meta, reqPath) && req.Refresh {
2022-08-29 11:15:52 +00:00
common.ErrorStrResp(c, "Refresh without permission", 403)
return
}
objs, err := fs.List(c, reqPath, &fs.ListArgs{Refresh: req.Refresh})
2022-06-27 11:51:23 +00:00
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-09-22 08:04:10 +00:00
provider := "unknown"
storage, err := fs.GetStorage(reqPath, &fs.GetStoragesArgs{})
2022-09-22 08:04:10 +00:00
if err == nil {
provider = storage.GetStorage().Driver
}
2022-06-27 13:15:39 +00:00
common.SuccessResp(c, FsListResp{
Content: toObjsResp(objs, reqPath, isEncrypt(meta, reqPath)),
2022-09-22 08:04:10 +00:00
Total: int64(total),
Readme: getReadme(meta, reqPath),
Header: getHeader(meta, reqPath),
Write: user.CanWrite() || common.CanWrite(meta, reqPath),
2022-09-22 08:04:10 +00:00
Provider: provider,
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)
reqPath := req.Path
if req.ForceRoot {
if !user.IsAdmin() {
common.ErrorStrResp(c, "Permission denied", 403)
return
}
} else {
tmp, err := user.JoinPath(req.Path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
reqPath = tmp
}
meta, err := op.GetNearestMeta(reqPath)
2022-07-10 06:09:31 +00:00
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500, true)
return
}
}
c.Set("meta", meta)
if !common.CanAccess(user, meta, reqPath, req.Password) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
2022-07-10 06:09:31 +00:00
return
}
objs, err := fs.List(c, reqPath, &fs.ListArgs{})
2022-07-10 06:09:31 +00:00
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(),
2022-07-10 06:09:31 +00:00
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 ""
}
func getHeader(meta *model.Meta, path string) string {
if meta != nil && (utils.PathEqual(meta.Path, path) || meta.HeaderSub) {
return meta.Header
}
return ""
}
2022-09-11 06:14:14 +00:00
func isEncrypt(meta *model.Meta, path string) bool {
if common.IsStorageSignEnabled(path) {
return true
}
2022-09-11 06:14:14 +00:00
if meta == nil || meta.Password == "" {
return false
}
if !utils.PathEqual(meta.Path, path) && !meta.PSub {
return false
}
return true
}
func pagination(objs []model.Obj, req *model.PageReq) (int, []model.Obj) {
2022-08-14 15:46:30 +00:00
pageIndex, pageSize := req.Page, req.PerPage
2022-06-27 11:51:23 +00:00
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]
}
func toObjsResp(objs []model.Obj, parent string, encrypt bool) []ObjResp {
2022-06-27 11:51:23 +00:00
var resp []ObjResp
for _, obj := range objs {
thumb, _ := model.GetThumb(obj)
2022-06-27 11:51:23 +00:00
resp = append(resp, ObjResp{
Name: obj.GetName(),
Size: obj.GetSize(),
IsDir: obj.IsDir(),
Modified: obj.ModTime(),
Created: obj.CreateTime(),
HashInfoStr: obj.GetHash().String(),
HashInfo: obj.GetHash().Export(),
Sign: common.Sign(obj, parent, encrypt),
Thumb: thumb,
Type: utils.GetObjType(obj.GetName(), obj.IsDir()),
2022-06-27 11:51:23 +00:00
})
}
return resp
}
2022-06-29 08:08:55 +00:00
2022-08-14 15:46:30 +00:00
type FsGetReq 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
2022-08-10 02:48:14 +00:00
RawURL string `json:"raw_url"`
Readme string `json:"readme"`
Header string `json:"header"`
2022-08-10 02:48:14 +00:00
Provider string `json:"provider"`
Related []ObjResp `json:"related"`
2022-06-29 08:08:55 +00:00
}
func FsGet(c *gin.Context) {
2022-08-14 15:46:30 +00:00
var req FsGetReq
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)
reqPath, err := user.JoinPath(req.Path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
meta, err := op.GetNearestMeta(reqPath)
2022-06-29 08:08:55 +00:00
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500)
return
}
}
c.Set("meta", meta)
if !common.CanAccess(user, meta, reqPath, req.Password) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
2022-06-29 08:08:55 +00:00
return
}
obj, err := fs.Get(c, reqPath, &fs.GetArgs{})
2022-06-29 08:08:55 +00:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
2022-06-29 08:23:31 +00:00
var rawURL string
2022-08-07 16:58:32 +00:00
storage, err := fs.GetStorage(reqPath, &fs.GetStoragesArgs{})
2022-08-07 16:58:32 +00:00
provider := "unknown"
if err == nil {
provider = storage.Config().Name
}
2022-07-08 07:56:29 +00:00
if !obj.IsDir() {
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
query := ""
if isEncrypt(meta, reqPath) || setting.GetBool(conf.SignAll) {
query = "?sign=" + sign.Sign(reqPath)
}
if storage.GetStorage().DownProxyUrl != "" {
rawURL = fmt.Sprintf("%s%s?sign=%s",
strings.Split(storage.GetStorage().DownProxyUrl, "\n")[0],
utils.EncodePath(reqPath, true),
sign.Sign(reqPath))
} else {
rawURL = fmt.Sprintf("%s/p%s%s",
common.GetApiUrl(c.Request),
utils.EncodePath(reqPath, true),
query)
2022-08-07 16:58:32 +00:00
}
} else {
// file have raw url
if url, ok := model.GetUrl(obj); ok {
rawURL = url
2022-07-08 07:56:29 +00:00
} 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, reqPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
HttpReq: c.Request,
})
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-08-10 02:48:14 +00:00
var related []model.Obj
parentPath := stdpath.Dir(reqPath)
sameLevelFiles, err := fs.List(c, parentPath, &fs.ListArgs{})
2022-08-07 13:01:29 +00:00
if err == nil {
related = filterRelated(sameLevelFiles, obj)
}
parentMeta, _ := op.GetNearestMeta(parentPath)
thumb, _ := model.GetThumb(obj)
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(),
Created: obj.CreateTime(),
HashInfoStr: obj.GetHash().String(),
HashInfo: obj.GetHash().Export(),
Sign: common.Sign(obj, parentPath, isEncrypt(meta, reqPath)),
Type: utils.GetFileType(obj.GetName()),
Thumb: thumb,
2022-06-29 08:08:55 +00:00
},
2022-08-07 16:58:32 +00:00
RawURL: rawURL,
Readme: getReadme(meta, reqPath),
Header: getHeader(meta, reqPath),
2022-08-07 16:58:32 +00:00
Provider: provider,
Related: toObjsResp(related, parentPath, isEncrypt(parentMeta, parentPath)),
2022-06-29 08:08:55 +00:00
})
}
2022-08-03 06:14:37 +00:00
2022-08-10 02:48:14 +00:00
func filterRelated(objs []model.Obj, obj model.Obj) []model.Obj {
var related []model.Obj
2022-08-07 13:01:29 +00:00
nameWithoutExt := strings.TrimSuffix(obj.GetName(), stdpath.Ext(obj.GetName()))
for _, o := range objs {
if o.GetName() == obj.GetName() {
continue
}
if strings.HasPrefix(o.GetName(), nameWithoutExt) {
2022-08-10 02:48:14 +00:00
related = append(related, o)
2022-08-07 13:01:29 +00:00
}
}
return related
}
2022-08-03 06:14:37 +00:00
type FsOtherReq struct {
model.FsOtherArgs
Password string `json:"password" form:"password"`
}
func FsOther(c *gin.Context) {
var req FsOtherReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
user := c.MustGet("user").(*model.User)
var err error
req.Path, err = user.JoinPath(req.Path)
if err != nil {
common.ErrorResp(c, err, 403)
return
}
meta, err := op.GetNearestMeta(req.Path)
2022-08-03 06:14:37 +00:00
if err != nil {
if !errors.Is(errors.Cause(err), errs.MetaNotFound) {
common.ErrorResp(c, err, 500)
return
}
}
c.Set("meta", meta)
if !common.CanAccess(user, meta, req.Path, req.Password) {
common.ErrorStrResp(c, "password is incorrect or you have no permission", 403)
2022-08-03 06:14:37 +00:00
return
}
res, err := fs.Other(c, req.FsOtherArgs)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, res)
}