content_handlers.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package controller
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "log"
  7. "time"
  8. "git.aetherial.dev/aeth/keiji/pkg/storage"
  9. "github.com/gin-gonic/gin"
  10. )
  11. /*
  12. Serves the admin panel with all of the documents in each blog category for editing
  13. */
  14. func (c *Controller) ServeBlogDirectory(ctx *gin.Context) {
  15. tableData := storage.AdminPage{Tables: map[string][]storage.TableData{}}
  16. for i := range storage.Topics {
  17. docs := c.database.GetByCategory(storage.Topics[i])
  18. for z := range docs {
  19. tableData.Tables[storage.Topics[i]] = append(tableData.Tables[storage.Topics[i]],
  20. storage.TableData{
  21. DisplayName: docs[z].Title,
  22. Link: fmt.Sprintf("/admin/options/%s", docs[z].Ident),
  23. },
  24. )
  25. }
  26. }
  27. ctx.HTML(200, "admin", gin.H{
  28. "navigation": gin.H{
  29. "menu": c.database.GetDropdownElements(),
  30. "headers": c.database.GetNavBarLinks(),
  31. },
  32. "Tables": tableData.Tables,
  33. })
  34. }
  35. /*
  36. Serves the blogpost editor with the submit button set to PATCH a document
  37. */
  38. func (c *Controller) GetBlogPostEditor(ctx *gin.Context) {
  39. post, exist := ctx.Params.Get("id")
  40. if !exist {
  41. ctx.JSON(404, map[string]string{
  42. "Error": "the requested file could not be found",
  43. })
  44. return
  45. }
  46. doc, err := c.database.GetDocument(storage.Identifier(post))
  47. if err != nil {
  48. ctx.JSON(500, map[string]string{
  49. "Error": err.Error(),
  50. })
  51. return
  52. }
  53. ctx.HTML(200, "blogpost_editor", gin.H{
  54. "navigation": gin.H{
  55. "menu": c.database.GetDropdownElements(),
  56. "headers": c.database.GetNavBarLinks(),
  57. },
  58. "Ident": doc.Ident,
  59. "Topics": storage.Topics,
  60. "Title": doc.Title,
  61. "DefaultTopic": doc.Category,
  62. "Created": doc.Created,
  63. "Body": doc.Body,
  64. })
  65. }
  66. /*
  67. Update an existing blog post
  68. */
  69. func (c *Controller) UpdateBlogPost(ctx *gin.Context) {
  70. var doc storage.Document
  71. err := ctx.ShouldBind(&doc)
  72. if err != nil {
  73. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": "Update Failed!", "Color": "red"})
  74. return
  75. }
  76. err = c.database.UpdateDocument(doc)
  77. if err != nil {
  78. ctx.HTML(400, "upload_status", gin.H{"UpdateMessage": "Update Failed!", "Color": "red"})
  79. return
  80. }
  81. ctx.HTML(200, "upload_status", gin.H{"UpdateMessage": "Update Successful!", "Color": "green"})
  82. }
  83. /*
  84. Serving the new blogpost page. Serves the editor with the method to POST a new document
  85. */
  86. func (c *Controller) ServeNewBlogPage(ctx *gin.Context) {
  87. ctx.HTML(200, "blogpost_editor", gin.H{
  88. "navigation": gin.H{
  89. "menu": c.database.GetDropdownElements(),
  90. "headers": c.database.GetNavBarLinks(),
  91. },
  92. "Post": true,
  93. "Topics": storage.Topics,
  94. "Created": time.Now().UTC().String(),
  95. })
  96. }
  97. /*
  98. Reciever for the ServeNewBlogPage UI screen. Adds a new document to the database
  99. */
  100. func (c *Controller) MakeBlogPost(ctx *gin.Context) {
  101. var doc storage.Document
  102. err := ctx.ShouldBind(&doc)
  103. if err != nil {
  104. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": "Update Failed!", "Color": "red"})
  105. return
  106. }
  107. err = c.database.AddDocument(doc)
  108. if err != nil {
  109. ctx.HTML(400, "upload_status", gin.H{"UpdateMessage": "Update Failed!", "Color": "red"})
  110. return
  111. }
  112. ctx.HTML(200, "upload_status", gin.H{"UpdateMessage": "Update Successful!", "Color": "green"})
  113. }
  114. /*
  115. Serves the HTML page for a new visual media post
  116. */
  117. func (c *Controller) ServeFileUpload(ctx *gin.Context) {
  118. ctx.HTML(200, "upload", gin.H{
  119. "navigation": gin.H{
  120. "menu": c.database.GetDropdownElements(),
  121. "headers": c.database.GetNavBarLinks(),
  122. },
  123. })
  124. }
  125. /*
  126. Reciever for the page served to created a new visual media post
  127. */
  128. func (c *Controller) SaveFile(ctx *gin.Context) {
  129. var img storage.Image
  130. err := ctx.ShouldBind(&img)
  131. if err != nil {
  132. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
  133. return
  134. }
  135. file, err := ctx.FormFile("file")
  136. if err != nil {
  137. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
  138. return
  139. }
  140. fh, err := file.Open()
  141. if err != nil {
  142. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
  143. return
  144. }
  145. fb := make([]byte, file.Size)
  146. var output bytes.Buffer
  147. for {
  148. n, err := fh.Read(fb)
  149. if err == io.EOF {
  150. break
  151. }
  152. if err != nil {
  153. log.Fatal(err)
  154. }
  155. output.Write(fb[:n])
  156. }
  157. err = c.database.AddImage(fb, img.Title, img.Desc)
  158. if err != nil {
  159. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
  160. return
  161. }
  162. ctx.HTML(200, "upload_status", gin.H{"UpdateMessage": "Update Successful!", "Color": "green"})
  163. }
  164. // Serve the document deletion template
  165. func (c *Controller) PostOptions(ctx *gin.Context) {
  166. id, found := ctx.Params.Get("id")
  167. if !found {
  168. ctx.HTML(400, "upload_status", gin.H{"UpdateMessage": "No ID selected!", "Color": "red"})
  169. return
  170. }
  171. ctx.HTML(200, "post_options", gin.H{
  172. "Link": fmt.Sprintf("/admin/posts/%s", id),
  173. })
  174. }
  175. /*
  176. Delete a document from the database
  177. */
  178. func (c *Controller) DeleteDocument(ctx *gin.Context) {
  179. id, found := ctx.Params.Get("id")
  180. if !found {
  181. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": "No ID passed!", "Color": "red"})
  182. return
  183. }
  184. err := c.database.DeleteDocument(storage.Identifier(id))
  185. if err != nil {
  186. ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": "Delete Failed!", "Color": "red"})
  187. return
  188. }
  189. ctx.HTML(200, "upload_status", gin.H{"UpdateMessage": "Delete Successful!", "Color": "green"})
  190. }