Skip to content

Commit

Permalink
add delete endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Jul 22, 2023
1 parent 2b2d101 commit 2beb24a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
87 changes: 87 additions & 0 deletions web/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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,
})
}

func (c *Client) Purge(g *gin.Context) {
// parse query
b := new(purgeRequest)
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.Directory = filepath.Join(c.uploadDirectory, b.Hash)

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

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

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

g.JSON(http.StatusOK, &fileResponse{
Message: fmt.Sprintf("Purged directory with hash %v", b.Hash),
Error: false,
})
}
8 changes: 7 additions & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ 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 @@ -43,8 +48,9 @@ func New(c *Config, uploadDirectory string) *Client {
}

func (c *Client) SetHandlers(r *gin.Engine) {
// core
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))
}

0 comments on commit 2beb24a

Please sign in to comment.