cdn_handlers.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package controller
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "path"
  8. "strings"
  9. "git.aetherial.dev/aeth/keiji/pkg/storage"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // @Name ServeImage
  13. // @Summary serves image from the image store
  14. // @Tags cdn
  15. // @Router /images/{file} [get]
  16. func (c *Controller) ServeImage(ctx *gin.Context) {
  17. f, exist := ctx.Params.Get("file")
  18. if !exist {
  19. ctx.JSON(404, map[string]string{
  20. "Error": "the requested file could not be found",
  21. })
  22. }
  23. css := fmt.Sprintf("%s/%s", storage.GetImageStore(), f)
  24. b, err := os.ReadFile(css)
  25. if err != nil {
  26. ctx.JSON(500, map[string]string{
  27. "Error": "Could not serve the requested file",
  28. "msg": err.Error(),
  29. })
  30. }
  31. if path.Ext(css) == "webm" {
  32. ctx.Data(200, "video/webm", b)
  33. return
  34. }
  35. ctx.Data(200, "image/jpeg", b)
  36. }
  37. // @Name ServeAsset
  38. // @Summary serves file from the html file
  39. // @Tags cdn
  40. // @Router /api/v1/assets/{file} [get]
  41. func (c *Controller) ServeAsset(ctx *gin.Context) {
  42. f, exist := ctx.Params.Get("file")
  43. if !exist {
  44. ctx.JSON(404, map[string]string{
  45. "Error": "the requested file could not be found",
  46. })
  47. return
  48. }
  49. assets := c.database.GetAssets()
  50. for i := range assets {
  51. if strings.Contains(assets[i].Name, f) {
  52. ctx.Data(200, "image/png", assets[i].Data)
  53. return
  54. }
  55. }
  56. ctx.Data(http.StatusNotFound, "text", []byte("Couldnt find the image requested."))
  57. }
  58. // @Name ServeGeneric
  59. // @Summary serves file from the html file
  60. // @Tags cdn
  61. // @Router /api/v1/cdn/{file} [get]
  62. func (c *Controller) ServeGeneric(ctx *gin.Context) {
  63. f, exist := ctx.Params.Get("file")
  64. if !exist {
  65. ctx.JSON(404, map[string]string{
  66. "Error": "the requested file could not be found",
  67. })
  68. return
  69. }
  70. fext := strings.Split(f, ".")[len(strings.Split(f, "."))-1]
  71. var ctype string
  72. switch {
  73. case fext == "css":
  74. ctype = "text/css"
  75. case fext == "js":
  76. ctype = "text/javascript"
  77. case fext == "json" || fext == "map":
  78. ctype = "application/json"
  79. case fext == "png":
  80. ctype = "image/png"
  81. default:
  82. ctype = "text"
  83. }
  84. fh, err := c.FileIO.Open(path.Join("cdn", f))
  85. if err != nil {
  86. ctx.JSON(500, map[string]string{
  87. "Error": "Could not serve the requested file",
  88. "msg": err.Error(),
  89. })
  90. return
  91. }
  92. b, err := io.ReadAll(fh)
  93. if err != nil {
  94. ctx.JSON(500, map[string]string{
  95. "Error": "Could not serve the requested file",
  96. "msg": err.Error(),
  97. })
  98. return
  99. }
  100. ctx.Data(200, ctype, b)
  101. }