You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.9 KiB

  1. package Api
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "mime/multipart"
  7. "net/http"
  8. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  9. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  10. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Util"
  11. "github.com/gofrs/uuid"
  12. )
  13. func createPostImage(w http.ResponseWriter, r *http.Request) {
  14. var (
  15. postData Models.Post
  16. postID string
  17. postUUID uuid.UUID
  18. postImage Models.PostImage
  19. formData *multipart.Form
  20. fileHeaders []*multipart.FileHeader
  21. fileHeader *multipart.FileHeader
  22. file multipart.File
  23. fileBytes []byte
  24. fileObject Util.FileObject
  25. returnJson []byte
  26. err error
  27. )
  28. postID, err = Util.GetPostId(r)
  29. if err != nil {
  30. log.Printf("Error encountered getting id\n")
  31. Util.JsonReturn(w, 500, "An error occured")
  32. return
  33. }
  34. postUUID = uuid.FromStringOrNil(postID)
  35. err = r.ParseMultipartForm(20 << 20)
  36. if err != nil {
  37. log.Printf("Error encountered parsing multipart form: %s\n", err.Error())
  38. Util.JsonReturn(w, 500, "An error occured")
  39. return
  40. }
  41. formData = r.MultipartForm
  42. fileHeaders = formData.File["files"]
  43. for _, fileHeader = range fileHeaders {
  44. file, err = fileHeader.Open()
  45. if err != nil {
  46. log.Printf("Error encountered while post image upload: %s\n", err.Error())
  47. Util.JsonReturn(w, 500, "An error occured")
  48. return
  49. }
  50. defer file.Close()
  51. fileBytes, err = ioutil.ReadAll(file)
  52. if err != nil {
  53. log.Printf("Error encountered while post image upload: %s\n", err.Error())
  54. Util.JsonReturn(w, 500, "An error occured")
  55. return
  56. }
  57. fileObject, err = Util.WriteFile(fileBytes, "image")
  58. if err != nil {
  59. log.Printf("Error encountered while post image upload: %s\n", err.Error())
  60. Util.JsonReturn(w, 415, "Invalid filetype")
  61. return
  62. }
  63. postImage = Models.PostImage{
  64. PostID: postUUID,
  65. Filepath: fileObject.Filepath,
  66. PublicFilepath: fileObject.PublicFilepath,
  67. Mimetype: fileObject.Mimetype,
  68. Size: fileObject.Size,
  69. }
  70. err = Database.CreatePostImage(&postImage)
  71. if err != nil {
  72. log.Printf("Error encountered while creating post_image record: %s\n", err.Error())
  73. Util.JsonReturn(w, 500, "An error occured")
  74. return
  75. }
  76. }
  77. postData, err = Util.GetPostById(w, r)
  78. if err != nil {
  79. return
  80. }
  81. returnJson, err = json.MarshalIndent(postData, "", " ")
  82. if err != nil {
  83. Util.JsonReturn(w, 500, "An error occured")
  84. return
  85. }
  86. // Return updated json
  87. w.WriteHeader(http.StatusOK)
  88. w.Write(returnJson)
  89. }
  90. func deletePostImage(w http.ResponseWriter, r *http.Request) {
  91. var (
  92. postImageData Models.PostImage
  93. err error
  94. )
  95. postImageData, err = Util.GetPostImageById(w, r)
  96. if err != nil {
  97. return
  98. }
  99. err = Database.DeletePostImage(&postImageData)
  100. if err != nil {
  101. log.Printf("An error occured: %s\n", err.Error())
  102. Util.JsonReturn(w, 500, "An error occured")
  103. return
  104. }
  105. // Return updated json
  106. w.WriteHeader(http.StatusOK)
  107. }