Skip to content

Commit

Permalink
simplify delete option
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Jul 22, 2023
1 parent 2beb24a commit d2dc010
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 52 deletions.
51 changes: 6 additions & 45 deletions web/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,20 @@ package web

import (
"fmt"
"github.com/Cloudbox/crs/util"
"github.com/gin-gonic/gin"
"net/http"
"os"
"path/filepath"
"strings"
)

func (c *Client) Delete(g *gin.Context) {
// parse query
b := new(fileRequest)
if err := g.ShouldBindUri(b); err != nil {
g.AbortWithError(http.StatusBadRequest, fmt.Errorf("bind uri: %w", err))
return
}

b.Hash = filepath.Base(b.Hash)
b.Filename = filepath.Base(strings.ToLower(b.Filename))
b.Directory = filepath.Join(c.uploadDirectory, b.Hash)
b.Filepath = filepath.Join(b.Directory, b.Filename)

// validate request
if !util.StringListContains(c.allowedFiles, b.Filename) {
g.AbortWithError(http.StatusBadRequest, fmt.Errorf("file unsupported: %v", b.Filename))
return
}

// validate file
if _, err := os.Stat(b.Filepath); err != nil {
if os.IsNotExist(err) {
g.AbortWithError(http.StatusNotFound, fmt.Errorf("file not found: %v", b.Filename))
return
}

g.AbortWithError(http.StatusInternalServerError, fmt.Errorf("file stat: %w", err))
return
}

// delete file
if err := os.Remove(b.Filepath); err != nil {
g.AbortWithError(http.StatusInternalServerError, fmt.Errorf("file delete: %w", err))
return
}

g.JSON(http.StatusOK, &fileResponse{
Message: fmt.Sprintf("Deleted %v", b.Filename),
Error: false,
})
type deleteRequest struct {
Hash string `uri:"hash" binding:"required"`
Directory string
}

func (c *Client) Purge(g *gin.Context) {
func (c *Client) Delete(g *gin.Context) {
// parse query
b := new(purgeRequest)
b := new(deleteRequest)
if err := g.ShouldBindUri(b); err != nil {
g.AbortWithError(http.StatusBadRequest, fmt.Errorf("bind uri: %w", err))
return
Expand All @@ -81,7 +42,7 @@ func (c *Client) Purge(g *gin.Context) {
}

g.JSON(http.StatusOK, &fileResponse{
Message: fmt.Sprintf("Purged directory with hash %v", b.Hash),
Message: fmt.Sprintf("Deleted directory with hash %v", b.Hash),
Error: false,
})
}
8 changes: 1 addition & 7 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ type fileRequest struct {
Filepath string
}

type purgeRequest struct {
Hash string `uri:"hash" binding:"required"`
Directory string
}

type fileResponse struct {
Message string `json:"msg,omitempty"`
Error bool `json:"error"`
Expand All @@ -51,6 +46,5 @@ func (c *Client) SetHandlers(r *gin.Engine) {
r.GET("/load/:hash/:filename", c.WithErrorResponse(c.Load))
r.HEAD("/load/:hash/:filename", c.WithErrorResponse(c.Load))
r.POST("/save/:hash/:filename", c.WithErrorResponse(c.Save))
r.POST("/delete/:hash/:filename", c.WithErrorResponse(c.Delete))
r.POST("/delete/:hash/purge", c.WithErrorResponse(c.Purge))
r.POST("/delete/:hash", c.WithErrorResponse(c.Delete))
}

0 comments on commit d2dc010

Please sign in to comment.