refactor(server): use boxstore helpers and file status consts
- Move box ID validation, file listing/pathing, manifest creation, and uploads to `boxstore` - Use shared helpers for safe filenames and polling interval env parsing - Add file status constants to `models` to avoid duplicated magic strings across handlersrefactor(server): use boxstore helpers and file status consts - Move box ID validation, file listing/pathing, manifest creation, and uploads to `boxstore` - Use shared helpers for safe filenames and polling interval env parsing - Add file status constants to `models` to avoid duplicated magic strings across handlers
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"warpbox/lib/boxstore"
|
||||
"warpbox/lib/helpers"
|
||||
"warpbox/lib/models"
|
||||
)
|
||||
|
||||
@@ -18,12 +20,12 @@ func handleIndex(ctx *gin.Context) {
|
||||
|
||||
func handleShowBox(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
if !validBoxID(boxID) {
|
||||
if !boxstore.ValidBoxID(boxID) {
|
||||
ctx.String(http.StatusBadRequest, "Invalid box id")
|
||||
return
|
||||
}
|
||||
|
||||
files, err := listBoxFiles(boxID)
|
||||
files, err := boxstore.ListFiles(boxID)
|
||||
if err != nil {
|
||||
ctx.String(http.StatusNotFound, "Box not found")
|
||||
return
|
||||
@@ -34,18 +36,18 @@ func handleShowBox(ctx *gin.Context) {
|
||||
"Files": files,
|
||||
"FileCount": len(files),
|
||||
"DownloadAll": "/box/" + boxID + "/download",
|
||||
"PollMS": boxPollingIntervalMS(),
|
||||
"PollMS": helpers.EnvInt("WARPBOX_BOX_POLL_INTERVAL_MS", 5000, 1000),
|
||||
})
|
||||
}
|
||||
|
||||
func handleBoxStatus(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
if !validBoxID(boxID) {
|
||||
if !boxstore.ValidBoxID(boxID) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box id"})
|
||||
return
|
||||
}
|
||||
|
||||
files, err := listBoxFiles(boxID)
|
||||
files, err := boxstore.ListFiles(boxID)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": "Box not found"})
|
||||
return
|
||||
@@ -56,12 +58,12 @@ func handleBoxStatus(ctx *gin.Context) {
|
||||
|
||||
func handleDownloadBox(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
if !validBoxID(boxID) {
|
||||
if !boxstore.ValidBoxID(boxID) {
|
||||
ctx.String(http.StatusBadRequest, "Invalid box id")
|
||||
return
|
||||
}
|
||||
|
||||
files, err := listBoxFiles(boxID)
|
||||
files, err := boxstore.ListFiles(boxID)
|
||||
if err != nil {
|
||||
ctx.String(http.StatusNotFound, "Box not found")
|
||||
return
|
||||
@@ -78,7 +80,7 @@ func handleDownloadBox(ctx *gin.Context) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := addFileToZip(zipWriter, boxID, file.Name); err != nil {
|
||||
if err := boxstore.AddFileToZip(zipWriter, boxID, file.Name); err != nil {
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -87,13 +89,13 @@ func handleDownloadBox(ctx *gin.Context) {
|
||||
|
||||
func handleDownloadFile(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
filename, ok := safeFilename(ctx.Param("filename"))
|
||||
if !validBoxID(boxID) || !ok {
|
||||
filename, ok := helpers.SafeFilename(ctx.Param("filename"))
|
||||
if !boxstore.ValidBoxID(boxID) || !ok {
|
||||
ctx.String(http.StatusBadRequest, "Invalid file")
|
||||
return
|
||||
}
|
||||
|
||||
path, ok := safeBoxFilePath(boxID, filename)
|
||||
path, ok := boxstore.SafeBoxFilePath(boxID, filename)
|
||||
if !ok {
|
||||
ctx.String(http.StatusBadRequest, "Invalid file")
|
||||
return
|
||||
@@ -108,13 +110,13 @@ func handleDownloadFile(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
func handleCreateBox(ctx *gin.Context) {
|
||||
boxID, err := newBoxID()
|
||||
boxID, err := boxstore.NewBoxID()
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create upload box"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(boxPath(boxID), 0755); err != nil {
|
||||
if err := os.MkdirAll(boxstore.BoxPath(boxID), 0755); err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Could not prepare upload box"})
|
||||
return
|
||||
}
|
||||
@@ -125,7 +127,7 @@ func handleCreateBox(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
files, err := createBoxManifest(boxID, request.Files)
|
||||
files, err := boxstore.CreateManifest(boxID, request.Files)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -137,21 +139,21 @@ func handleCreateBox(ctx *gin.Context) {
|
||||
func handleManifestFileUpload(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
fileID := ctx.Param("file_id")
|
||||
if !validBoxID(boxID) {
|
||||
if !boxstore.ValidBoxID(boxID) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box id"})
|
||||
return
|
||||
}
|
||||
|
||||
file, err := ctx.FormFile("file")
|
||||
if err != nil {
|
||||
markManifestFileStatus(boxID, fileID, fileStatusFailed)
|
||||
boxstore.MarkFileStatus(boxID, fileID, models.FileStatusFailed)
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "No file received"})
|
||||
return
|
||||
}
|
||||
|
||||
savedFile, err := saveManifestUploadedFile(ctx, boxID, fileID, file)
|
||||
savedFile, err := boxstore.SaveManifestUpload(boxID, fileID, file)
|
||||
if err != nil {
|
||||
markManifestFileStatus(boxID, fileID, fileStatusFailed)
|
||||
boxstore.MarkFileStatus(boxID, fileID, models.FileStatusFailed)
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -162,7 +164,7 @@ func handleManifestFileUpload(ctx *gin.Context) {
|
||||
func handleFileStatusUpdate(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
fileID := ctx.Param("file_id")
|
||||
if !validBoxID(boxID) {
|
||||
if !boxstore.ValidBoxID(boxID) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box id"})
|
||||
return
|
||||
}
|
||||
@@ -173,7 +175,7 @@ func handleFileStatusUpdate(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
file, err := markManifestFileStatus(boxID, fileID, request.Status)
|
||||
file, err := boxstore.MarkFileStatus(boxID, fileID, request.Status)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -184,7 +186,7 @@ func handleFileStatusUpdate(ctx *gin.Context) {
|
||||
|
||||
func handleDirectBoxUpload(ctx *gin.Context) {
|
||||
boxID := ctx.Param("id")
|
||||
if !validBoxID(boxID) {
|
||||
if !boxstore.ValidBoxID(boxID) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid box id"})
|
||||
return
|
||||
}
|
||||
@@ -195,7 +197,7 @@ func handleDirectBoxUpload(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
savedFile, err := saveUploadedFile(ctx, boxID, file)
|
||||
savedFile, err := boxstore.SaveUpload(boxID, file)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -217,20 +219,20 @@ func handleLegacyUpload(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
boxID, err := newBoxID()
|
||||
boxID, err := boxstore.NewBoxID()
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create upload box"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(boxPath(boxID), 0755); err != nil {
|
||||
if err := os.MkdirAll(boxstore.BoxPath(boxID), 0755); err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Could not prepare upload box"})
|
||||
return
|
||||
}
|
||||
|
||||
savedFiles := make([]gin.H, 0, len(files))
|
||||
savedFiles := make([]models.BoxFile, 0, len(files))
|
||||
for _, file := range files {
|
||||
savedFile, err := saveUploadedFile(ctx, boxID, file)
|
||||
savedFile, err := boxstore.SaveUpload(boxID, file)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user