@@ -2,6 +2,7 @@ package server
2
2
3
3
import (
4
4
"errors"
5
+ "github.com/reaper47/recipya/web"
5
6
"io"
6
7
"log/slog"
7
8
"net/http"
@@ -80,6 +81,114 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
80
81
_ = components .SimplePage ("Page Not Found" , "The page you requested to view is not found. Please go back to the main page." ).Render (r .Context (), w )
81
82
}
82
83
84
+ func (s * Server ) placeholderPostHandler () http.Handler {
85
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
86
+ userID := getUserID (r )
87
+ userIDAttr := slog .Int64 ("userID" , userID )
88
+
89
+ name := r .FormValue ("name" )
90
+ if name != "recipe" && name != "cookbook" {
91
+ s .Brokers .SendToast (models .NewErrorFormToast ("Only the recipe or cookbook placeholder can be updated." ), userID )
92
+ w .WriteHeader (http .StatusBadRequest )
93
+ return
94
+ }
95
+
96
+ r .Body = http .MaxBytesReader (w , r .Body , 1 << 24 )
97
+
98
+ err := r .ParseMultipartForm (1 << 24 )
99
+ if err != nil {
100
+ msg := "Could not parse the form."
101
+ slog .Error (msg , userIDAttr , "error" , err )
102
+ s .Brokers .SendToast (models .NewErrorFormToast (msg ), userID )
103
+ w .WriteHeader (http .StatusBadRequest )
104
+ return
105
+ }
106
+
107
+ images , ok := r .MultipartForm .File ["images" ]
108
+ if ! ok {
109
+ msg := "Could not retrieve the image from the form."
110
+ slog .Error (msg , userIDAttr , "error" , err )
111
+ s .Brokers .SendToast (models .NewErrorFormToast (msg ), userID )
112
+ w .WriteHeader (http .StatusBadRequest )
113
+ return
114
+ }
115
+
116
+ if len (images ) == 0 {
117
+ msg := "No image has been uploaded."
118
+ slog .Error (msg , userIDAttr , "error" , err )
119
+ s .Brokers .SendToast (models .NewErrorFormToast (msg ), userID )
120
+ w .WriteHeader (http .StatusBadRequest )
121
+ return
122
+ }
123
+
124
+ f , err := images [0 ].Open ()
125
+ if err != nil {
126
+ msg := "Could not open the image from the form."
127
+ slog .Error (msg , "error" , err , userIDAttr )
128
+ s .Brokers .SendToast (models .NewErrorFormToast (msg ), userID )
129
+ w .WriteHeader (http .StatusBadRequest )
130
+ return
131
+ }
132
+ defer f .Close ()
133
+
134
+ imageUUID , err := s .Files .UploadImage (f )
135
+ if err != nil {
136
+ msg := "Error uploading image."
137
+ slog .Error (msg , "error" , err , userIDAttr )
138
+ s .Brokers .SendToast (models .NewErrorFilesToast (msg ), userID )
139
+ w .WriteHeader (http .StatusInternalServerError )
140
+ return
141
+ }
142
+ imageUUIDAttr := slog .String ("imageUUID" , imageUUID .String ())
143
+
144
+ from := filepath .Join (app .ImagesDir , imageUUID .String ()+ app .ImageExt )
145
+ to := filepath .Join (app .ImagesDir , "Placeholders" , "placeholder." + name + app .ImageExt )
146
+ err = os .Rename (from , to )
147
+ if err != nil {
148
+ msg := "Error moving compressed placeholder image."
149
+ slog .Error (msg , "error" , err , imageUUIDAttr , userIDAttr )
150
+ s .Brokers .SendToast (models .NewErrorFilesToast (msg ), userID )
151
+ w .WriteHeader (http .StatusInternalServerError )
152
+ return
153
+ }
154
+ })
155
+ }
156
+
157
+ func (s * Server ) restorePlaceholderPostHandler () http.HandlerFunc {
158
+ return func (w http.ResponseWriter , r * http.Request ) {
159
+ name := "recipe"
160
+ path := "static/img/recipes/placeholder.webp"
161
+ if r .FormValue ("name" ) == "cookbook" {
162
+ name = "cookbook"
163
+ path = "static/img/cookbooks-new/placeholder.webp"
164
+ }
165
+
166
+ userID := getUserID (r )
167
+ userIDAttr := slog .Int64 ("userID" , userID )
168
+
169
+ openFile , err := web .StaticFS .Open (path )
170
+ if err != nil {
171
+ msg := "Error opening the public file."
172
+ s .Brokers .SendToast (models .NewErrorGeneralToast (msg ), userID )
173
+ slog .Error (msg , "error" , err , userIDAttr )
174
+ return
175
+ }
176
+ defer openFile .Close ()
177
+
178
+ f , err := os .Create (filepath .Join (app .ImagesDir , "Placeholders" , "placeholder." + name + ".webp" ))
179
+ if err != nil {
180
+ msg := "Error creating placeholder file."
181
+ s .Brokers .SendToast (models .NewErrorGeneralToast (msg ), userID )
182
+ slog .Error (msg , "error" , err , userIDAttr )
183
+ w .WriteHeader (http .StatusInternalServerError )
184
+ return
185
+ }
186
+ defer f .Close ()
187
+
188
+ io .Copy (f , openFile )
189
+ }
190
+ }
191
+
83
192
func (s * Server ) userInitialsHandler () http.HandlerFunc {
84
193
return func (w http.ResponseWriter , r * http.Request ) {
85
194
_ , _ = w .Write ([]byte (s .Repository .UserInitials (getUserID (r ))))
0 commit comments