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.

127 lines
2.8 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 = getPostId(r)
  29. if err != nil {
  30. log.Printf("Error encountered getting id\n")
  31. 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. 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. 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. 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. JsonReturn(w, 415, "Invalid filetype")
  61. return
  62. }
  63. postImage = Models.PostImage{
  64. PostID: postUUID,
  65. Filepath: fileObject.Filepath,
  66. Mimetype: fileObject.Mimetype,
  67. Size: fileObject.Size,
  68. }
  69. err = Database.CreatePostImage(&postImage)
  70. if err != nil {
  71. log.Printf("Error encountered while creating post_image record: %s\n", err.Error())
  72. JsonReturn(w, 500, "An error occured")
  73. return
  74. }
  75. }
  76. postData, err = getPostById(w, r)
  77. if err != nil {
  78. return
  79. }
  80. returnJson, err = json.MarshalIndent(postData, "", " ")
  81. if err != nil {
  82. JsonReturn(w, 500, "An error occured")
  83. return
  84. }
  85. // Return updated json
  86. w.WriteHeader(http.StatusOK)
  87. w.Write(returnJson)
  88. }
  89. func deletePostImage(w http.ResponseWriter, r *http.Request) {
  90. var (
  91. postImageData Models.PostImage
  92. err error
  93. )
  94. postImageData, err = getPostImageById(w, r)
  95. if err != nil {
  96. return
  97. }
  98. err = Database.DeletePostImage(&postImageData)
  99. if err != nil {
  100. log.Printf("An error occured: %s\n", err.Error())
  101. JsonReturn(w, 500, "An error occured")
  102. return
  103. }
  104. // Return updated json
  105. w.WriteHeader(http.StatusOK)
  106. }