Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/handlers #100

Merged
merged 14 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/handlers/docs/handleAllDocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func HandleAllDocs(c *gin.Context) {
var entries []models.Doc

database.DB.Find(&entries, &models.Doc{})

c.JSON(http.StatusOK, entries)
Expand Down
27 changes: 15 additions & 12 deletions src/handlers/docs/handleDocDelete.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package handlers

import (
"fmt"
"net/http"
"os"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
Expand All @@ -18,19 +16,24 @@ func HandleDocDelete(c *gin.Context) {
})
return
}

deletedFileName, success := database.DeleteDoc(fileName)
filePath := fmt.Sprintf("%v/uploads/docs/%v", util.ExPath, deletedFileName)
err := os.Remove(filePath)
if success && err == nil {
c.JSON(http.StatusOK, gin.H{
"message": "Document deleted successfuly",
"fileName": deletedFileName,
})
} else {
fmt.Println(success)
fmt.Println(err)
if !success {
c.JSON(http.StatusNotFound, gin.H{
"error": "Document not found",
})
return
}

err := util.DeleteFile(deletedFileName, "docs")
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"error": "Failed to delete document",
})
}

c.JSON(http.StatusOK, gin.H{
"message": "Document deleted successfuly",
"fileName": deletedFileName,
})
}
19 changes: 10 additions & 9 deletions src/handlers/docs/handleDocsRename.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ package handlers

import (
"net/http"
"os"
"path/filepath"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/util"
"github.com/kevinanielsen/go-fast-cdn/src/validations"
)

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

if oldName == "" || newName == "" {
c.String(http.StatusBadRequest, "Invalid request")
err := validations.ValidateRenameInput(oldName, newName)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}

prefix := filepath.Join(util.ExPath, "uploads", "docs")
filteredNewName, err := util.FilterFilename(newName)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}

err := os.Rename(
filepath.Join(prefix, oldName),
filepath.Join(prefix, newName),
)
err = util.RenameFile(oldName, filteredNewName, "docs")
if err != nil {
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error())
return
Expand Down
1 change: 1 addition & 0 deletions src/handlers/image/handleAllImages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func HandleAllImages(c *gin.Context) {
var entries []models.Image

database.DB.Find(&entries, &models.Image{})

c.JSON(http.StatusOK, entries)
Expand Down
26 changes: 16 additions & 10 deletions src/handlers/image/handleImageDelete.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package handlers

import (
"fmt"
"net/http"
"os"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
Expand All @@ -18,17 +16,25 @@ func HandleImageDelete(c *gin.Context) {
})
return
}

deletedFileName, success := database.DeleteImage(fileName)
filePath := fmt.Sprintf("%v/uploads/images/%v", util.ExPath, deletedFileName)
err := os.Remove(filePath)
if success && err == nil {
c.JSON(http.StatusOK, gin.H{
"message": "Image deleted successfully",
"fileName": deletedFileName,
})
} else {
if !success {
c.JSON(http.StatusNotFound, gin.H{
"error": "Image not found",
})
return
}

err := util.DeleteFile(deletedFileName, "images")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to delete image",
})
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Image deleted successfully",
"fileName": deletedFileName,
})
}
15 changes: 5 additions & 10 deletions src/handlers/image/handleImageRename.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package handlers

import (
"net/http"
"os"
"path/filepath"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/util"
"github.com/kevinanielsen/go-fast-cdn/src/validations"
)

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

if oldName == "" || newName == "" {
c.String(http.StatusBadRequest, "Invalid request")
err := validations.ValidateRenameInput(oldName, newName)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}

Expand All @@ -25,12 +25,7 @@ func HandleImageRename(c *gin.Context) {
return
}

prefix := filepath.Join(util.ExPath, "uploads", "images")

err = os.Rename(
filepath.Join(prefix, oldName),
filepath.Join(prefix, filteredNewName),
)
err = util.RenameFile(oldName, filteredNewName, "images")
if err != nil {
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/image/handleImageUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

func HandleImageUpload(c *gin.Context) {
fileHeader, err := c.FormFile("image")
newName := c.PostForm("filename")

fileHeader, err := c.FormFile("image")
if err != nil {
c.String(http.StatusBadRequest, "Failed to read file: %s", err.Error())
return
Expand Down
17 changes: 17 additions & 0 deletions src/util/deleteFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

import (
"fmt"
"os"
)

func DeleteFile(deletedFileName string, fileType string) error {
filePath := fmt.Sprintf("%v/uploads/%v/%v", ExPath, fileType, deletedFileName)

err := os.Remove(filePath)
if err != nil {
return err
}

return nil
}
20 changes: 20 additions & 0 deletions src/util/renameFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

import (
"os"
"path/filepath"
)

func RenameFile(oldName, newName, fileType string) error {
prefix := filepath.Join(ExPath, "uploads", fileType)

err := os.Rename(
filepath.Join(prefix, oldName),
filepath.Join(prefix, newName),
)
if err != nil {
return err
}

return nil
}
15 changes: 15 additions & 0 deletions src/validations/validations.go
kevinanielsen marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package validations

import "errors"

func ValidateRenameInput(oldName, newName string) error {
if oldName == "" || newName == "" {
return errors.New("'oldName' and 'newName' are required")
}

if oldName == newName {
return errors.New("New name must be different from the old name")
}

return nil
}
Loading