Skip to content

Commit

Permalink
First pass at #75
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiiorg committed Dec 30, 2023
1 parent 92b5d00 commit 1548aff
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions handlers/docs/handleDocMetadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handlers

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

"github.com/kevinanielsen/go-fast-cdn/util"

"github.com/gin-gonic/gin"
)

func HandleDocMetadata(c *gin.Context) {
fileName := c.Param("filename")
if fileName == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Doc name is required",
})
return
}

filePath := fmt.Sprintf("%v/uploads/docs/%v", util.ExPath, fileName)
stat, err := os.Stat(filePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
c.JSON(http.StatusNotFound, gin.H{
"error": "Doc does not exist",
})
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Internal error",
})
}
return
}

c.JSON(http.StatusOK, gin.H{
"filename": fileName,
"download_url": c.Request.Host + "/api/cdn/download/docs/" + fileName,
"file_size": stat.Size(),
})
}
1 change: 1 addition & 0 deletions router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func AddApiRoutes(r *gin.Engine) {
{
cdn.GET("/size", handlers.GetSizeHandler)
cdn.GET("/doc/all", dHandlers.HandleAllDocs)
cdn.GET("/doc/:filename", dHandlers.HandleDocMetadata)
cdn.GET("/image/all", iHandlers.HandleAllImages)
cdn.POST("/drop/database", dbHandlers.HandleDropDB)
cdn.Static("/download/images", util.ExPath+"/uploads/images")
Expand Down

0 comments on commit 1548aff

Please sign in to comment.