diff --git a/inventory/share.go b/inventory/share.go index 642ce21c..0090154b 100644 --- a/inventory/share.go +++ b/inventory/share.go @@ -49,12 +49,14 @@ type ( Downloaded(ctx context.Context, share *ent.Share) error // Delete deletes the share. Delete(ctx context.Context, shareId int) error + // DeleteBatch deletes the shares with the given ids. + DeleteBatch(ctx context.Context, shareIds []int) error + // DeleteBatchByUserID deletes the shares with the given ids and user id + DeleteBatchByUserID(ctx context.Context, uid int, shareIds []int) error // List returns a list of shares with the given args. List(ctx context.Context, args *ListShareArgs) (*ListShareResult, error) // CountByTimeRange counts the number of shares created in the given time range. CountByTimeRange(ctx context.Context, start, end *time.Time) (int, error) - // DeleteBatch deletes the shares with the given ids. - DeleteBatch(ctx context.Context, shareIds []int) error } CreateShareParams struct { @@ -195,6 +197,11 @@ func (c *shareClient) DeleteBatch(ctx context.Context, shareIds []int) error { return err } +func (c *shareClient) DeleteBatchByUserID(ctx context.Context, uid int, shareIds []int) error { + _, err := c.client.Share.Delete().Where(share.IDIn(shareIds...)).Where(share.HasUserWith(user.ID(uid))).Exec(ctx) + return err +} + func (c *shareClient) Delete(ctx context.Context, shareId int) error { return c.client.Share.DeleteOneID(shareId).Exec(ctx) } diff --git a/routers/controllers/share.go b/routers/controllers/share.go index 3aa549ca..9899d98b 100644 --- a/routers/controllers/share.go +++ b/routers/controllers/share.go @@ -72,6 +72,16 @@ func DeleteShare(c *gin.Context) { c.JSON(200, serializer.Response{}) } +func BatchDeleteShare(c *gin.Context) { + service := ParametersFromContext[*share.BatchDeleteShareService](c, share.BatchDeleteParamCtx{}) + err := service.Delete(c) + if err != nil { + c.JSON(200, serializer.Err(c, err)) + } + + c.JSON(200, serializer.Response{}) +} + func ShareRedirect(c *gin.Context) { service := ParametersFromContext[*share.ShortLinkRedirectService](c, share.ShortLinkRedirectParamCtx{}) c.Redirect(http.StatusFound, service.RedirectTo(c)) diff --git a/routers/router.go b/routers/router.go index 8b357d82..70f345ea 100644 --- a/routers/router.go +++ b/routers/router.go @@ -38,7 +38,6 @@ func InitRouter(dep dependency.Dep) *gin.Engine { l.Info("Current running mode: Slave.") return initSlaveRouter(dep) - } func newGinEngine(dep dependency.Dep) *gin.Engine { @@ -842,6 +841,12 @@ func initMasterRouter(dep dependency.Dep) *gin.Engine { middleware.HashID(hashid.ShareID), controllers.DeleteShare, ) + share.DELETE("", + middleware.LoginRequired(), + middleware.RequiredScopes(types.ScopeSharesWrite), + controllers.FromJSON[sharesvc.BatchDeleteShareService](sharesvc.BatchDeleteParamCtx{}), + controllers.BatchDeleteShare, + ) //// 获取README文本文件内容 //share.GET("readme/:id", // middleware.CheckShareUnlocked(), diff --git a/service/share/manage.go b/service/share/manage.go index b8ad4d1d..f1121e7c 100644 --- a/service/share/manage.go +++ b/service/share/manage.go @@ -2,6 +2,7 @@ package share import ( "context" + "fmt" "time" "github.com/cloudreve/Cloudreve/v4/application/dependency" @@ -10,6 +11,7 @@ import ( "github.com/cloudreve/Cloudreve/v4/inventory/types" "github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs" "github.com/cloudreve/Cloudreve/v4/pkg/filemanager/manager" + "github.com/cloudreve/Cloudreve/v4/pkg/hashid" "github.com/cloudreve/Cloudreve/v4/pkg/serializer" "github.com/cloudreve/Cloudreve/v4/service/explorer" "github.com/gin-gonic/gin" @@ -27,8 +29,36 @@ type ( ShowReadMe bool `json:"show_readme"` } ShareCreateParamCtx struct{} + + BatchDeleteShareService struct { + ShareIDs []string `json:"ids" binding:"required"` + } + BatchDeleteParamCtx struct{} ) +func (service *BatchDeleteShareService) Delete(c *gin.Context) error { + dep := dependency.FromContext(c) + uid := inventory.UserIDFromContext(c) + shareClient := dep.ShareClient() + + var ids []int + + for _, v := range service.ShareIDs { + id, err := dep.HashIDEncoder().Decode(v, hashid.ShareID) + if err != nil { + return fmt.Errorf("failed to decode hash id %q: %w", v, err) + } + + ids = append(ids, id) + } + + if err := shareClient.DeleteBatchByUserID(c, uid, ids); err != nil { + return serializer.NewError(serializer.CodeDBError, "Failed to delete shares", err) + } + + return nil +} + // Upsert 创建或更新分享 func (service *ShareCreateService) Upsert(c *gin.Context, existed int) (string, error) { dep := dependency.FromContext(c)