feat(be/files): enable shareID for sharing APIs

This commit is contained in:
hexxa 2022-01-15 14:33:52 +08:00 committed by Hexxa
parent 772ec7992c
commit a378296980
5 changed files with 217 additions and 85 deletions

View file

@ -28,6 +28,7 @@ var (
// queries
FilePathQuery = "fp"
ListDirQuery = "dp"
ShareIDQuery = "sh"
// headers
rangeHeader = "Range"
@ -113,8 +114,8 @@ func (h *FileHandlers) canAccess(userName, role, op, sharedPath string) bool {
return false
}
_, ok := h.deps.FileInfos().GetSharing(sharedPath)
return ok
isSharing, ok := h.deps.FileInfos().GetSharing(sharedPath)
return isSharing && ok
}
type CreateReq struct {
@ -535,7 +536,8 @@ func (h *FileHandlers) Download(c *gin.Context) {
}
role := c.MustGet(q.RoleParam).(string)
userName := c.MustGet(q.UserParam).(string)
if !h.canAccess(userName, role, "download", filePath) {
dirPath := filepath.Dir(filePath)
if !h.canAccess(userName, role, "download", dirPath) {
c.JSON(q.ErrResp(c, 403, q.ErrAccessDenied))
return
}
@ -895,6 +897,7 @@ type SharingResp struct {
SharingDirs []string `json:"sharingDirs"`
}
// Deprecated: use ListSharingIDs instead
func (h *FileHandlers) ListSharings(c *gin.Context) {
// TODO: move canAccess to authedFS
userName := c.MustGet(q.UserParam).(string)
@ -912,6 +915,22 @@ func (h *FileHandlers) ListSharings(c *gin.Context) {
c.JSON(200, &SharingResp{SharingDirs: dirs})
}
type SharingIDsResp struct {
IDs map[string]string `json:"IDs"`
}
func (h *FileHandlers) ListSharingIDs(c *gin.Context) {
// TODO: move canAccess to authedFS
userName := c.MustGet(q.UserParam).(string)
dirToID, err := h.deps.FileInfos().ListSharings(q.FsRootPath(userName, "/"))
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(200, &SharingIDsResp{IDs: dirToID})
}
type GenerateHashReq struct {
FilePath string `json:"filePath"`
}
@ -952,6 +971,25 @@ func (h *FileHandlers) GenerateHash(c *gin.Context) {
c.JSON(q.Resp(200))
}
type GetSharingDirResp struct {
SharingDir string `json:"sharingDir"`
}
func (h *FileHandlers) GetSharingDir(c *gin.Context) {
shareID := c.Query(ShareIDQuery)
if shareID == "" {
c.JSON(q.ErrResp(c, 400, errors.New("invalid share ID")))
return
}
dirPath, err := h.deps.FileInfos().GetSharingDir(shareID)
if err != nil {
c.JSON(q.ErrResp(c, 500, err))
return
}
c.JSON(200, &GetSharingDirResp{SharingDir: dirPath})
}
func (h *FileHandlers) GetStreamReader(userID uint64, fd io.Reader) (io.ReadCloser, error) {
pr, pw := io.Pipe()
chunkSize := 100 * 1024 // notice: it can not be greater than limiter's token count

View file

@ -73,6 +73,8 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.AdminRole, "DELETE", "/v1/fs/sharings"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/exist"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/dirs"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/ids"): true,
apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/hashes/sha1"): true,
// user rules
apiRuleCname(userstore.UserRole, "GET", "/"): true,
@ -105,7 +107,9 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.UserRole, "DELETE", "/v1/fs/sharings"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings/exist"): true,
apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/hashes/sha1"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings/dirs"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/sharings/ids"): true,
apiRuleCname(userstore.UserRole, "POST", "/v1/fs/hashes/sha1"): true,
// visitor rules
apiRuleCname(userstore.VisitorRole, "GET", "/"): true,
apiRuleCname(userstore.VisitorRole, "GET", publicPath): true,
@ -120,6 +124,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/imgs"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/sharings/exist"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/sharings/dirs"): true,
}
return &MultiUsersSvc{