diff --git a/handlers/docs/handleDocMetadata.go b/handlers/docs/handleDocMetadata.go new file mode 100644 index 0000000..f37907d --- /dev/null +++ b/handlers/docs/handleDocMetadata.go @@ -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(), + }) +} diff --git a/router/api.go b/router/api.go index 225e71f..79de378 100644 --- a/router/api.go +++ b/router/api.go @@ -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")