[server][api] Merge cat and download routes to contents

This commit is contained in:
Abhishek Shroff
2025-04-05 16:33:54 +05:30
parent 8e42752e5c
commit 93828f210a
3 changed files with 23 additions and 30 deletions
-26
View File
@@ -1,26 +0,0 @@
package fs
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/api/auth"
"github.com/shroff/phylum/server/internal/api/serve"
"github.com/shroff/phylum/server/internal/core/fs"
)
func handleCatRequest(c *gin.Context) {
resourceID, err := uuid.Parse(c.Param("id"))
if err != nil {
panic(errResourceIDInvalid)
}
f := auth.GetFileSystem(c)
r, err := f.ResourceByID(resourceID)
if r.Dir() {
err = fs.ErrResourceCollection
}
if err != nil {
panic(err)
}
serve.Serve(c.Writer, c.Request, r)
}
@@ -3,14 +3,19 @@ package fs
import (
"archive/zip"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/api/auth"
"github.com/shroff/phylum/server/internal/api/serve"
"github.com/shroff/phylum/server/internal/core/errors"
"github.com/shroff/phylum/server/internal/core/fs"
)
func handleDownloadRequest(c *gin.Context) {
var errGetCollectionContents = errors.NewError(http.StatusBadRequest, "resource_is_collection", "Cannot get contents of collection")
func handleContentsRequest(c *gin.Context) {
resourceID, err := uuid.Parse(c.Param("id"))
if err != nil {
panic(errResourceIDInvalid)
@@ -22,8 +27,23 @@ func handleDownloadRequest(c *gin.Context) {
panic(err)
}
download := c.Request.URL.Query().Has("download")
if download {
name := c.Request.URL.Query().Get("name")
if name == "" {
name = r.Name() + ".zip"
}
c.Writer.Header().Set("Content-Disposition", "attachment; filename=\""+name+"\"")
}
if !r.Dir() {
serve.Serve(c.Writer, c.Request, r)
return
}
if !download {
panic(errGetCollectionContents)
}
c.Writer.Header().Set("Content-Type", "application/zip")
c.Writer.Header().Set("Content-Disposition", "attachment; filename=\""+r.Name()+".zip\"")
var zip = zip.NewWriter(c.Writer)
close := c.Writer.CloseNotify()
+1 -2
View File
@@ -14,8 +14,7 @@ var (
func SetupRoutes(r *gin.RouterGroup) {
group := r.Group("/fs")
group.GET("/cat/:id", handleCatRequest)
group.GET("/download/:id", handleDownloadRequest)
group.GET("/contents/:id", handleContentsRequest)
group.GET("/du/:id", handleDuRequest)
group.GET("/ls/:id", handleLsRequest)
group.POST("/mkdir/:id", handleMkdirRequest)