Skip to content

Commit

Permalink
feat: Add API endpoint for renaming files (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Westra <[email protected]>
  • Loading branch information
webstradev authored Dec 19, 2023
1 parent f722ec9 commit 3a1853a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
8 changes: 8 additions & 0 deletions database/renameDoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package database

import "github.com/go-fast-cdn/models"

func RenameDoc(oldFileName, newFileName string) error {
doc := models.Doc{}
return DB.Model(&doc).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error
}
8 changes: 8 additions & 0 deletions database/renameImage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package database

import "github.com/go-fast-cdn/models"

func RenameImage(oldFileName, newFileName string) error {
image := models.Image{}
return DB.Model(&image).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error
}
6 changes: 6 additions & 0 deletions router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ func AddApiRoutes(r *gin.Engine) {
delete.DELETE("/image/:filename", iHandlers.HandleImageDelete)
delete.DELETE("/doc/:filename", dHandlers.HandleDocDelete)
}

rename := cdn.Group("/rename")
{
rename.PUT("/image", iHandlers.HandleImageRename)
rename.PUT("/doc", dHandlers.HandleDocsRename)
}
}
26 changes: 26 additions & 0 deletions util/handlers/docs/handleDocsRename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package handlers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-fast-cdn/database"
)

func HandleDocsRename(c *gin.Context) {
oldName := c.PostForm("filename")
newName := c.PostForm("newname")

if oldName == "" || newName == "" {
c.String(http.StatusBadRequest, "Invalid request")
return
}

err := database.RenameDoc(oldName, newName)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error())
return
}

c.JSON(http.StatusOK, gin.H{"status": "File renamed successfully"})
}
26 changes: 26 additions & 0 deletions util/handlers/image/handleImageRename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package handlers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-fast-cdn/database"
)

func HandleImageRename(c *gin.Context) {
oldName := c.PostForm("filename")
newName := c.PostForm("newname")

if oldName == "" || newName == "" {
c.String(http.StatusBadRequest, "Invalid request")
return
}

err := database.RenameImage(oldName, newName)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error())
return
}

c.JSON(http.StatusOK, gin.H{"status": "File renamed successfully"})
}

0 comments on commit 3a1853a

Please sign in to comment.