-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmicropub.go
520 lines (481 loc) · 14.7 KB
/
micropub.go
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package main
import (
"cmp"
"crypto/sha256"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
urlpkg "net/url"
"path/filepath"
"reflect"
"regexp"
"slices"
"strings"
"github.com/samber/lo"
"github.com/spf13/cast"
"go.goblog.app/app/pkgs/bodylimit"
"go.hacdias.com/indielib/micropub"
"gopkg.in/yaml.v3"
)
func (a *goBlog) getMicropubImplementation() *micropubImplementation {
if a.mpImpl == nil {
a.mpImpl = µpubImplementation{a: a}
}
return a.mpImpl
}
const (
micropubPath = "/micropub"
micropubMediaSubPath = "/media"
)
type micropubImplementation struct {
a *goBlog
h http.Handler
mh http.Handler
}
func (s *micropubImplementation) getHandler() http.Handler {
if s.h == nil {
s.h = micropub.NewHandler(
s,
micropub.WithMediaEndpoint(s.a.getFullAddress(micropubPath+micropubMediaSubPath)),
micropub.WithGetCategories(s.getCategories),
micropub.WithGetChannels(s.getChannels),
micropub.WithGetVisibility(s.getVisibility),
)
}
return s.h
}
func (s *micropubImplementation) getCategories() []string {
allCategories := []string{}
for blog := range s.a.cfg.Blogs {
values, _ := s.a.db.allTaxonomyValues(blog, s.a.cfg.Micropub.CategoryParam)
allCategories = append(allCategories, values...)
}
return lo.Uniq(allCategories)
}
func (s *micropubImplementation) getChannels() []micropub.Channel {
allChannels := []micropub.Channel{}
for b, bc := range s.a.cfg.Blogs {
allChannels = append(allChannels, micropub.Channel{
Name: fmt.Sprintf("%s: %s", b, bc.Title),
UID: b,
})
for s, sc := range bc.Sections {
allChannels = append(allChannels, micropub.Channel{
Name: fmt.Sprintf("%s/%s: %s", b, s, sc.Name),
UID: fmt.Sprintf("%s/%s", b, s),
})
}
}
return allChannels
}
func (s *micropubImplementation) getVisibility() []string {
return []string{string(visibilityPrivate), string(visibilityUnlisted), string(visibilityPublic)}
}
func (s *micropubImplementation) getMediaHandler() http.Handler {
if s.mh == nil {
s.mh = micropub.NewMediaHandler(
s.UploadMedia,
s.HasScope,
micropub.WithMaxMemory(0),
micropub.WithMaxMediaSize(30*bodylimit.MB),
)
}
return s.mh
}
func (s *micropubImplementation) HasScope(r *http.Request, scope string) bool {
return strings.Contains(r.Context().Value(indieAuthScope).(string), scope)
}
func (s *micropubImplementation) Source(urlStr string) (map[string]any, error) {
url, err := urlpkg.Parse(urlStr)
if err != nil {
return nil, fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
p, err := s.a.getPost(url.Path)
if err != nil {
return nil, fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
return s.a.postToMfMap(p), nil
}
func (s *micropubImplementation) SourceMany(limit, offset int) ([]map[string]any, error) {
posts, err := s.a.getPosts(&postsRequestConfig{
limit: limit,
offset: offset,
})
if err != nil {
return nil, fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
list := []map[string]any{}
for _, p := range posts {
list = append(list, s.a.postToMfMap(p))
}
return list, nil
}
func (s *micropubImplementation) Create(req *micropub.Request) (string, error) {
if req.Type != "h-entry" {
return "", fmt.Errorf("%w: only h-entry supported", micropub.ErrNotImplemented)
}
entry := &post{}
entry.Parameters = map[string][]string{}
allValues := lo.Assign(req.Properties, req.Commands)
// Parameters with special care
for photoNo, photo := range allValues["photo"] {
pp := s.a.cfg.Micropub.PhotoParam
pdp := s.a.cfg.Micropub.PhotoDescriptionParam
if photoLink, isPhotoLink := photo.(string); isPhotoLink {
entry.Parameters[pp] = append(entry.Parameters[pp], photoLink)
if len(allValues["photo-alt"]) > photoNo && allValues["photo-alt"][photoNo] != nil {
entry.Parameters[pdp] = append(entry.Parameters[pdp], cast.ToString(allValues["photo-alt"][photoNo]))
} else {
entry.Parameters[pdp] = append(entry.Parameters[pdp], "")
}
} else if photoObject, isPhotoObject := photo.(map[string]any); isPhotoObject {
entry.Parameters[pp] = append(entry.Parameters[pp], cast.ToString(photoObject["value"]))
entry.Parameters[pdp] = append(entry.Parameters[pdp], cast.ToString(photoObject["alt"]))
}
}
delete(allValues, "photo")
delete(allValues, "photo-alt")
delete(allValues, "file") // Micropublish.net fix
// Rest of parameters
for key, values := range allValues {
values := cast.ToStringSlice(values)
if len(values) == 0 {
continue
}
switch key {
case "content":
entry.Content = values[0]
case "published":
entry.Published = values[0]
case "updated":
entry.Updated = values[0]
case "slug":
entry.Slug = values[0]
case "channel":
entry.setChannel(values[0])
case "post-status":
entry.Status = micropubStatus(values[0])
case "visibility":
entry.Visibility = micropubVisibility(values[0])
default:
entry.Parameters[s.mapToParameterName(key)] = values
}
}
if err := s.a.extractParamsFromContent(entry); err != nil {
return "", fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
if err := s.a.createPost(entry); err != nil {
return "", fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
return s.a.fullPostURL(entry), nil
}
func (s *micropubImplementation) Update(req *micropub.Request) (string, error) {
// Get editor options
editorOptions := req.Updates.Replace["goblog-editor"]
if editorOptions == nil {
editorOptions = []any{}
}
// Get post
url, err := urlpkg.Parse(req.URL)
if err != nil {
return "", fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
postPath := cmp.Or(url.Path, "/")
entry, err := s.a.getPost(postPath)
if err != nil {
return "", fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
// Check if post is marked as deleted
if entry.Deleted() {
return "", fmt.Errorf("%w: post is marked as deleted, undelete it first", micropub.ErrBadRequest)
}
// Update post
oldPath := entry.Path
oldStatus := entry.Status
oldVisibility := entry.Visibility
if entry.Parameters == nil {
entry.Parameters = map[string][]string{}
}
// Update properties
properties := s.a.postMfProperties(entry, false)
properties, err = micropubUpdateMfProperties(properties, req.Updates)
if err != nil {
return "", fmt.Errorf("failed to update properties: %w", err)
}
s.updatePostPropertiesFromMf(entry, properties)
err = s.a.extractParamsFromContent(entry)
if err != nil {
return "", fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
err = s.a.replacePost(entry, oldPath, oldStatus, oldVisibility, slices.Contains(editorOptions, "noupdated"))
if err != nil {
return "", fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
return s.a.fullPostURL(entry), nil
}
func (s *micropubImplementation) Delete(urlStr string) error {
url, err := urlpkg.Parse(urlStr)
if err != nil {
return fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
if err := s.a.deletePost(url.Path); err != nil {
return fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
return nil
}
func (s *micropubImplementation) Undelete(urlStr string) error {
url, err := urlpkg.Parse(urlStr)
if err != nil {
return fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
if err := s.a.undeletePost(url.Path); err != nil {
return fmt.Errorf("%w: %w", micropub.ErrBadRequest, err)
}
return nil
}
func (s *micropubImplementation) UploadMedia(file multipart.File, header *multipart.FileHeader) (string, error) {
// Generate sha256 hash for file
hash := sha256.New()
_, err := io.Copy(hash, file)
if err != nil {
return "", fmt.Errorf("%w: failed to get file hash", micropub.ErrBadRequest)
}
// Get file extension
fileExtension := filepath.Ext(header.Filename)
if fileExtension == "" {
// Find correct file extension if original filename does not contain one
mimeType := header.Header.Get(contentType)
if len(mimeType) > 0 {
allExtensions, _ := mime.ExtensionsByType(mimeType)
if len(allExtensions) > 0 {
fileExtension = allExtensions[0]
}
}
}
// Generate the file name
fileName := fmt.Sprintf("%x%s", hash.Sum(nil), fileExtension)
// Save file
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return "", fmt.Errorf("%w: failed to read multipart file", micropub.ErrBadRequest)
}
location, err := s.a.saveMediaFile(fileName, file)
if err != nil {
return "", fmt.Errorf("%w: failed to save original file", micropub.ErrBadRequest)
}
// Try to compress file (only when not in private mode)
if !s.a.isPrivate() {
compressedLocation, compressionErr := s.a.compressMediaFile(location)
if compressionErr != nil {
return "", fmt.Errorf("%w: failed to compress file: %w", micropub.ErrBadRequest, compressionErr)
}
// Overwrite location
if compressedLocation != "" {
location = compressedLocation
}
}
return location, nil
}
func (s *micropubImplementation) mapToParameterName(key string) string {
switch key {
case "name":
return "title"
case "category":
return s.a.cfg.Micropub.CategoryParam
case "in-reply-to":
return s.a.cfg.Micropub.ReplyParam
case "like-of":
return s.a.cfg.Micropub.LikeParam
case "bookmark-of":
return s.a.cfg.Micropub.BookmarkParam
case "audio":
return s.a.cfg.Micropub.AudioParam
case "location":
return s.a.cfg.Micropub.LocationParam
default:
return key
}
}
func (a *goBlog) extractParamsFromContent(p *post) error {
// Ensure parameters map is initialized
if p.Parameters == nil {
p.Parameters = map[string][]string{}
}
// Normalize line endings in content
p.Content = regexp.MustCompile("\r\n").ReplaceAllString(p.Content, "\n")
// Check for frontmatter
if split := strings.Split(p.Content, "---\n"); len(split) >= 3 && strings.TrimSpace(split[0]) == "" {
// Extract frontmatter
fm := split[1]
meta := map[string]any{}
if err := yaml.Unmarshal([]byte(fm), &meta); err != nil {
return err
}
// Copy frontmatter to parameters
for key, value := range meta {
// For parameters starting with "+", use existing parameters and just append
// For other parameters, create a new slice
if !strings.HasPrefix(key, "+") {
p.Parameters[key] = []string{}
} else {
key = strings.TrimPrefix(key, "+")
}
// Append to existing parameters
if a, ok := value.([]any); ok {
for _, ae := range a {
p.Parameters[key] = append(p.Parameters[key], cast.ToString(ae))
}
} else {
p.Parameters[key] = append(p.Parameters[key], cast.ToString(value))
}
}
// Remove frontmatter from content
p.Content = strings.Join(split[2:], "---\n")
}
// Extract specific parameters
extractParam := func(paramName string, field any) {
if values, ok := p.Parameters[paramName]; len(values) == 1 && ok {
if stringPointer, ok := field.(*string); ok {
*stringPointer = values[0]
} else if stringFunc, ok := field.(func(string)); ok {
stringFunc(values[0])
}
delete(p.Parameters, paramName)
}
}
extractParam("blog", &p.Blog)
extractParam("path", &p.Path)
extractParam("section", &p.Section)
extractParam("slug", &p.Slug)
extractParam("published", &p.Published)
extractParam("updated", &p.Updated)
extractParam("status", func(status string) { p.Status = postStatus(status) })
extractParam("visibility", func(visibility string) { p.Visibility = postVisibility(visibility) })
extractParam("priority", func(priority string) { p.Priority = cast.ToInt(priority) })
// Add images not in content
images, imageAlts := p.Parameters[a.cfg.Micropub.PhotoParam], p.Parameters[a.cfg.Micropub.PhotoDescriptionParam]
useAlts := len(images) == len(imageAlts)
for i, image := range images {
if !strings.Contains(p.Content, image) {
if useAlts && imageAlts[i] != "" {
p.Content += fmt.Sprintf("\n\n", imageAlts[i], image, imageAlts[i])
} else {
p.Content += fmt.Sprintf("\n\n", image)
}
}
}
return nil
}
func micropubStatus(status string) postStatus {
switch status {
case "draft":
return statusDraft
default:
return statusPublished
}
}
func micropubVisibility(visibility string) postVisibility {
switch visibility {
case "unlisted":
return visibilityUnlisted
case "private":
return visibilityPrivate
default:
return visibilityPublic
}
}
func micropubUpdateMfProperties(properties map[string][]any, req micropub.RequestUpdate) (map[string][]any, error) {
if req.Replace != nil {
delete(req.Replace, "goblog-editor")
for key, value := range req.Replace {
properties[key] = value
}
}
if req.Add != nil {
for key, value := range req.Add {
if _, ok := properties[key]; !ok {
properties[key] = []any{}
}
properties[key] = append(properties[key], value...)
}
}
if req.Delete != nil {
if reflect.TypeOf(req.Delete).Kind() == reflect.Slice {
toDelete, ok := req.Delete.([]any)
if !ok {
return nil, errors.New("invalid delete array")
}
for _, key := range toDelete {
delete(properties, cast.ToString(key))
}
} else {
toDelete, ok := req.Delete.(map[string]any)
if !ok {
return nil, fmt.Errorf("invalid delete object: expected map[string]any, got: %s", reflect.TypeOf(req.Delete))
}
for key, v := range toDelete {
value, ok := v.([]any)
if !ok {
// Wrong type, ignore
continue
}
if _, ok := properties[key]; !ok {
// Parameter not present, ignore delete
continue
}
properties[key] = lo.Filter(properties[key], func(ss any, _ int) bool {
for _, s := range value {
if s == ss {
return false
}
}
return true
})
}
}
}
return properties, nil
}
func (s *micropubImplementation) updatePostPropertiesFromMf(p *post, properties map[string][]any) {
if properties == nil || p == nil {
return
}
// Ignore the following properties
delete(properties, "url")
delete(properties, "photo")
delete(properties, "photo-alt")
// Helper function
getFirstStringFromArray := func(arr any) string {
if strArr, ok := arr.([]any); ok && len(strArr) > 0 {
if str, ok := strArr[0].(string); ok {
return str
}
}
return ""
}
// Set other properties
p.Content = getFirstStringFromArray(properties["content"])
delete(properties, "content")
p.Published = getFirstStringFromArray(properties["published"])
delete(properties, "published")
p.Updated = getFirstStringFromArray(properties["updated"])
delete(properties, "updated")
p.Slug = getFirstStringFromArray(properties["mp-slug"])
delete(properties, "mp-slug")
p.setChannel(getFirstStringFromArray(properties["mp-channel"]))
delete(properties, "mp-channel")
p.Visibility = postVisibility(cmp.Or(getFirstStringFromArray(properties["visibility"]), string(p.Visibility)))
delete(properties, "visibility")
if newStatusString := getFirstStringFromArray(properties["post-status"]); newStatusString != "" {
if newStatus := postStatus(newStatusString); newStatus == statusPublished || newStatus == statusDraft {
p.Status = newStatus
}
}
delete(properties, "post-status")
for key, value := range properties {
p.Parameters[s.mapToParameterName(key)] = cast.ToStringSlice(value)
}
}