~evanj/cms

ref: 4aa45cc85249c542c4d80aed47e25c86145bcc3c cms/internal/c/content/content.go -rw-r--r-- 9.0 KiB
4aa45cc8Evan M Jones Feat(mysql): Moving to mysql. Getting ready for alpha release (just for 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package content

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/http"
	"strings"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/content"
	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/m/valuetype"
	"git.sr.ht/~evanj/cms/internal/s/db"
	"git.sr.ht/~evanj/cms/internal/s/tmpl"
)

var (
	contentHTML = tmpl.MustParse("html/content.html")
)

type Content struct {
	*c.Controller
	log *log.Logger
	db  dber
	e3  e3er
}

type dber interface {
	UserGet(username, password string) (user.User, error)
	UserGetFromToken(token string) (user.User, error)
	SpaceGet(user user.User, spaceID string) (space.Space, error)
	ContentTypeGet(space space.Space, contenttypeID string) (contenttype.ContentType, error)
	ContentNew(space space.Space, ct contenttype.ContentType, params []db.ContentNewParam) (content.Content, error)
	ContentGet(space space.Space, ct contenttype.ContentType, contentID string) (content.Content, error)
	ContentUpdate(space space.Space, ct contenttype.ContentType, content content.Content, params []db.ContentUpdateParam) (content.Content, error)
	ContentDelete(space space.Space, ct contenttype.ContentType, content content.Content) error
	ContentPerContentType(space space.Space, ct contenttype.ContentType, page int) ([]content.Content, error)
}

type e3er interface {
	Upload(ctx context.Context, public bool, filename string, file io.Reader) (string, error)
}

func New(log *log.Logger, db dber, e3 e3er) *Content {
	return &Content{
		c.New(log, db),
		log,
		db,
		e3,
	}
}

func (c *Content) tree(w http.ResponseWriter, r *http.Request, spaceID, contenttypeID, contentID string) (user.User, space.Space, contenttype.ContentType, content.Content, error) {
	user, err := c.GetCookieUser(w, r)
	if err != nil {
		return nil, nil, nil, nil, fmt.Errorf("must be logged in")
	}

	space, err := c.db.SpaceGet(user, spaceID)
	if err != nil {
		return nil, nil, nil, nil, fmt.Errorf("failed to find required space")
	}

	ct, err := c.db.ContentTypeGet(space, contenttypeID)
	if err != nil {
		return nil, nil, nil, nil, fmt.Errorf("failed to find required contenttype")
	}

	content, err := c.db.ContentGet(space, ct, contentID)
	if err != nil {
		return nil, nil, nil, nil, fmt.Errorf("failed to find required content")
	}

	return user, space, ct, content, nil
}

func (c *Content) create(w http.ResponseWriter, r *http.Request) {
	spaceID := r.FormValue("space")
	contenttypeID := r.FormValue("contenttype")

	user, err := c.GetCookieUser(w, r)
	if err != nil {
		c.Error(w, r, http.StatusBadRequest, "must be logged in to create content")
		return
	}

	space, err := c.db.SpaceGet(user, spaceID)
	if err != nil {
		c.Error(w, r, http.StatusInternalServerError, "failed to find required space")
		return
	}

	ct, err := c.db.ContentTypeGet(space, contenttypeID)
	if err != nil {
		c.Error(w, r, http.StatusInternalServerError, "failed to find required contenttype")
		return
	}

	var params []db.ContentNewParam
	for key := range r.Form {
		if key == "space" || key == "contenttype" {
			continue
		}

		parts := strings.Split(key, "-")
		if len(parts) < 2 {
			c.Error(w, r, http.StatusInternalServerError, "invalid name field for value")
			return
		}

		typ, name, val := parts[0], parts[1], r.FormValue(key)
		if typ == valuetype.File {
			continue
		}

		params = append(params, db.ContentNewParam{
			Type:  typ,
			Name:  name,
			Value: val,
		})
	}

	// TODO: Upload concurrently.
	_, _, _ = r.FormFile("") // Dummy read, loads internal r.MulitpartForm state.
	for key := range r.MultipartForm.File {
		file, header, err := r.FormFile(key)
		if err != nil {
			c.Error(w, r, http.StatusInternalServerError, "failed to retreive file")
			return
		}

		// TODO: Change public to false
		url, err := c.e3.Upload(r.Context(), true, header.Filename, file)
		if err != nil {
			c.log.Println("failed to upload file", err)
			c.Error(w, r, http.StatusInternalServerError, "failed to upload file")
			return
		}

		parts := strings.Split(key, "-")
		if len(parts) < 2 {
			c.Error(w, r, http.StatusInternalServerError, "invalid name field for value")
			return
		}

		typ, name := parts[0], parts[1]

		params = append(params, db.ContentNewParam{
			Type:  typ,
			Name:  name,
			Value: url,
		})
	}

	content, err := c.db.ContentNew(space, ct, params)
	if err != nil {
		c.log.Println(err)
		c.Error(w, r, http.StatusInternalServerError, err.Error())
		return
	}

	url := fmt.Sprintf("/content/%s/%s/%s", space.ID(), ct.ID(), content.ID())
	c.log.Println("successfully created new content for user", user.Name(), "in space", space.Name(), "for contenttype", ct.Name(), "redirecting to", url)
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func (c *Content) serve(w http.ResponseWriter, r *http.Request, spaceID, contenttypeID, contentID string) {
	user, err := c.GetCookieUser(w, r)
	if err != nil {
		c.Error(w, r, http.StatusBadRequest, "must be logged in")
		return
	}

	space, err := c.db.SpaceGet(user, spaceID)
	if err != nil {
		c.Error(w, r, http.StatusInternalServerError, "failed to find required space")
		return
	}

	ct, err := c.db.ContentTypeGet(space, contenttypeID)
	if err != nil {
		c.Error(w, r, http.StatusInternalServerError, "failed to find required contenttype")
		return
	}

	content, err := c.db.ContentGet(space, ct, contentID)
	if err != nil {
		c.log.Println(err)
		c.Error(w, r, http.StatusInternalServerError, "failed to find desired content")
		return
	}

	c.HTML(w, r, contentHTML, map[string]interface{}{
		"User":        user,
		"Space":       space,
		"ContentType": ct,
		"Content":     content,
	})
}

func (c *Content) update(w http.ResponseWriter, r *http.Request) {
	spaceID := r.FormValue("space")
	contenttypeID := r.FormValue("contenttype")
	contentID := r.FormValue("content")

	user, space, ct, content, err := c.tree(w, r, spaceID, contenttypeID, contentID)
	if err != nil {
		c.log.Println(err)
		c.Error(w, r, http.StatusInternalServerError, err.Error())
		return
	}

	var params []db.ContentUpdateParam
	for key := range r.Form {
		if key == "space" || key == "contenttype" || key == "content" {
			continue
		}

		parts := strings.Split(key, "-")
		if len(parts) < 2 {
			c.Error(w, r, http.StatusInternalServerError, "invalid name field for value")
			return
		}

		typ, id, val := parts[0], parts[1], r.FormValue(key)
		if typ == valuetype.File && strings.TrimSpace(val) == "" {
			// Don't update empty file fields.
			continue
		}

		params = append(params, db.ContentUpdateParam{
			ID:    id,
			Type:  typ,
			Value: val,
		})
	}

	// TODO: Upload concurrently.
	_, _, _ = r.FormFile("") // Dummy read, loads internal r.MulitpartForm state.
	for key := range r.MultipartForm.File {
		file, header, err := r.FormFile(key)
		if err != nil {
			c.Error(w, r, http.StatusInternalServerError, "failed to retreive file")
			return
		}

		// TODO: Change public to false
		url, err := c.e3.Upload(r.Context(), true, header.Filename, file)
		if err != nil {
			c.log.Println("failed to upload file", err)
			c.Error(w, r, http.StatusInternalServerError, "failed to upload file")
			return
		}

		parts := strings.Split(key, "-")
		if len(parts) < 2 {
			c.Error(w, r, http.StatusInternalServerError, "invalid name field for value")
			return
		}

		_, id := parts[0], parts[1]

		params = append(params, db.ContentUpdateParam{
			ID:    id,
			Type:  valuetype.File,
			Value: url,
		})
	}

	content, err = c.db.ContentUpdate(space, ct, content, params)
	if err != nil {
		c.Error(w, r, http.StatusInternalServerError, "failed to find update content")
		return
	}

	url := fmt.Sprintf("/content/%s/%s/%s", space.ID(), ct.ID(), content.ID())
	c.log.Println("successfully updated content for user", user.Name(), "in space", space.Name(), "for contenttype", ct.Name(), "redirecting to", url)
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func (c *Content) delete(w http.ResponseWriter, r *http.Request) {
	spaceID := r.FormValue("space")
	contenttypeID := r.FormValue("contenttype")
	contentID := r.FormValue("content")

	user, space, ct, content, err := c.tree(w, r, spaceID, contenttypeID, contentID)
	if err != nil {
		c.log.Println(err)
		c.Error(w, r, http.StatusInternalServerError, err.Error())
		return
	}

	if err := c.db.ContentDelete(space, ct, content); err != nil {
		c.log.Println(err)
		c.Error(w, r, http.StatusInternalServerError, "failed to delete content")
		return
	}

	url := fmt.Sprintf("/contenttype/%s/%s", space.ID(), ct.ID())
	c.log.Println("successfully deleted content for user", user.Name(), "in space", space.Name(), "for contenttype", ct.Name(), "redirecting to", url)
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func (c *Content) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	switch r.URL.Path {
	case "/content/new":
		c.create(w, r)
		return
	case "/content/update":
		c.update(w, r)
		return
	case "/content/delete":
		c.delete(w, r)
		return
	}

	parts := strings.Split(r.URL.Path, "/")
	if len(parts) > 4 {
		c.serve(w, r, parts[2], parts[3], parts[4])
		return
	}

	http.NotFound(w, r)
}