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. // TODO: Change arg to bitwise op
  36. err = r.ParseMultipartForm(200000)
  37. if err != nil {
  38. log.Printf("Error encountered parsing multipart form: %s\n", err.Error())
  39. JsonReturn(w, 500, "An error occured")
  40. return
  41. }
  42. formData = r.MultipartForm
  43. fileHeaders = formData.File["files"]
  44. for _, fileHeader = range fileHeaders {
  45. file, err = fileHeader.Open()
  46. if err != nil {
  47. log.Printf("Error encountered while post image upload: %s\n", err.Error())
  48. JsonReturn(w, 500, "An error occured")
  49. return
  50. }
  51. defer file.Close()
  52. fileBytes, err = ioutil.ReadAll(file)
  53. if err != nil {
  54. log.Printf("Error encountered while post image upload: %s\n", err.Error())
  55. JsonReturn(w, 500, "An error occured")
  56. return
  57. }
  58. fileObject, err = Util.WriteFile(fileBytes, "image")
  59. if err != nil {
  60. log.Printf("Error encountered while post image upload: %s\n", err.Error())
  61. JsonReturn(w, 415, "Invalid filetype")
  62. return
  63. }
  64. postImage = Models.PostImage{
  65. PostID: postUUID,
  66. Filepath: fileObject.Filepath,
  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. JsonReturn(w, 500, "An error occured")
  74. return
  75. }
  76. }
  77. postData, err = getPostById(w, r)
  78. if err != nil {
  79. return
  80. }
  81. returnJson, err = json.MarshalIndent(postData, "", " ")
  82. if err != nil {
  83. 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 = 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. JsonReturn(w, 500, "An error occured")
  103. return
  104. }
  105. // Return updated json
  106. w.WriteHeader(http.StatusOK)
  107. }