-
Notifications
You must be signed in to change notification settings - Fork 0
/
poster.go
315 lines (266 loc) · 7.37 KB
/
poster.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
package main
import (
"github.com/djherbis/times"
"github.com/writeas/go-writeas/v2"
"log/slog"
"os"
"path"
"regexp"
"slices"
"strings"
"time"
)
const AllowedFileTimestampSkew = 2 * time.Second
var ObsiSyncFilePattern = regexp.MustCompile(`\d\d\d\d-\d\d-\d\d-.*\.md`)
type LocalImage struct {
fullPath string
relPath string
size int64
mtime time.Time
}
type LocalPost struct {
fname string
datePart, slug string
images []LocalImage
ctime, mtime time.Time
content string
title string
}
type PostSynchronizer struct {
imageSyncer ImageSyncer
client *writeas.Client
rootDir string
collAlias string
posts map[string]LocalPost
}
func NewPostSynchronizer(imageSyncer ImageSyncer, client *writeas.Client, rootDir, collAlias string) *PostSynchronizer {
return &PostSynchronizer{
imageSyncer: imageSyncer,
client: client,
rootDir: rootDir,
collAlias: collAlias,
posts: make(map[string]LocalPost),
}
}
func (p *PostSynchronizer) FindFiles() error {
dir, err := os.ReadDir(p.rootDir)
if err != nil {
return err
}
slices.SortFunc(dir, func(a, b os.DirEntry) int {
return strings.Compare(a.Name(), b.Name())
})
for _, d := range dir {
fname := d.Name()
if !ObsiSyncFilePattern.MatchString(fname) || d.IsDir() {
continue
}
finfo, err := d.Info()
if err != nil {
return err
}
// Extract the slug
parts := strings.SplitN(fname, "-", 4)
datePart := strings.Join(parts[0:3], "-")
slug := strings.TrimSuffix(parts[3], ".md")
content, err := os.ReadFile(path.Join(p.rootDir, fname))
if err != nil {
return err
}
images, title, err := GatherPostImagesAndTitle(p.rootDir, content)
if err != nil {
return err
}
stat, err := times.Stat(path.Join(p.rootDir, fname))
if err != nil {
return err
}
lp := LocalPost{
fname: fname,
datePart: datePart,
slug: slug,
images: images,
mtime: finfo.ModTime(),
ctime: stat.BirthTime(),
content: string(content),
title: title,
}
p.posts[slug] = lp
}
return nil
}
func (p *PostSynchronizer) UploadLocalImages() (map[string]string, error) {
urlMap := make(map[string]string)
for _, curPost := range p.posts {
for _, i := range curPost.images {
imgUrl, err := p.imageSyncer.EnsureLocalImageIsUploaded(i)
if err != nil {
return nil, err
}
urlMap[i.relPath] = imgUrl
}
}
return urlMap, nil
}
func (p *PostSynchronizer) LoadRemotePosts() ([]writeas.Post, error) {
var res []writeas.Post
page := uint64(1)
for {
slog.Default().Info("Fetching a page", slog.Uint64("page", page))
posts, err := ReqWithRetries[*[]writeas.Post](func() (*[]writeas.Post, error) {
return GetCollectionPostsPaginated(p.client, p.collAlias, page)
})
if err != nil {
return nil, err
}
if len(*posts) == 0 {
break
}
res = append(res, *posts...)
page++
}
return res, nil
}
func (p *PostSynchronizer) UpdateOrCreateLocalPosts(remotePosts []writeas.Post) error {
for _, curPost := range remotePosts {
// Find the local file?
localPost, ok := p.posts[curPost.Slug]
if ok {
// Check the modification time
timeDiff := localPost.mtime.Sub(curPost.Updated)
if timeDiff < -AllowedFileTimestampSkew {
// The file is substantially newer than the server's post
slog.Default().Info("Post has been updated on the server, syncing locally",
slog.String("slug", curPost.Slug))
err := p.createOrUpdateLocalFile(curPost, &localPost)
if err != nil {
return err
}
}
} else {
slog.Default().Info("New remote post", slog.String("slug", curPost.Slug))
err := p.createOrUpdateLocalFile(curPost, nil)
if err != nil {
return err
}
}
}
return nil
}
func (p *PostSynchronizer) createOrUpdateLocalFile(post writeas.Post, local *LocalPost) error {
datePart := post.Created.UTC().Format("2006-01-02")
if local != nil {
// Override the remote post's creation date, it might be different from the local one
datePart = local.datePart
}
fname := path.Join(p.rootDir, datePart+"-"+post.Slug+".md")
file, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
linkFixMap, err := ParsePostAndDownloadReferencedImages(p.imageSyncer, post.Content, datePart, post.Slug)
if err != nil {
return err
}
fixedContent := post.Content
for oldLnk, newLnk := range linkFixMap {
fixedContent = strings.ReplaceAll(fixedContent, "("+oldLnk+")", "("+newLnk+")")
}
// Fixup the final "discuss" link: <a href=\"....\">Discuss...</a>
re := regexp.MustCompile(`\n\n<a href=".*">Discuss...</a> $`)
fixedContent = string(re.ReplaceAll([]byte(fixedContent), []byte("")))
// We excluded the title during the upload, re-add it
if strings.TrimSpace(post.Title) != "" {
fixedContent = "# " + post.Title + "\n" + fixedContent
}
_, err = file.WriteString(fixedContent)
if err != nil {
return err
}
err = os.Chtimes(fname, time.Time{}, post.Updated)
if err != nil {
return err
}
return nil
}
func (p *PostSynchronizer) UpdateOrCreateRemotePosts(remotePosts []writeas.Post,
imageUrlMap map[string]string) error {
remotes := make(map[string]writeas.Post)
for _, post := range remotePosts {
remotes[post.Slug] = post
}
for _, localPost := range p.posts {
// Do we have the remote post?
remote, ok := remotes[localPost.slug]
if ok {
timeDiff := localPost.mtime.Sub(remote.Updated)
if timeDiff > AllowedFileTimestampSkew {
slog.Default().Info("File has been updated locally, updating on the server",
slog.String("slug", localPost.slug))
err := p.uploadLocalPostToServer(localPost, &remote, imageUrlMap)
if err != nil {
return err
}
} else {
slog.Default().Info("Up-to-date file", slog.String("slug", localPost.slug))
}
} else {
slog.Default().Info("Uploading new local post", slog.String("slug", localPost.slug))
err := p.uploadLocalPostToServer(localPost, nil, imageUrlMap)
if err != nil {
return err
}
}
}
return nil
}
func (p *PostSynchronizer) uploadLocalPostToServer(local LocalPost, remote *writeas.Post,
imageUrlMap map[string]string) error {
// First, fixup the URLs
content := local.content
for relPath, imgUrl := range imageUrlMap {
content = strings.ReplaceAll(content, "("+relPath+")", "("+imgUrl+")")
}
// Remove the title
if local.title != "" {
content = strings.Replace(content, "# "+local.title+"\n", "", 1)
}
if remote != nil {
_, err := ReqWithRetries[*writeas.Post](func() (*writeas.Post, error) {
return p.client.UpdatePost(remote.ID, "", &writeas.PostParams{
ID: remote.ID,
Updated: &local.mtime,
Content: content,
Title: local.title,
})
})
if err != nil {
return err
}
} else {
// The timestamp is basically useless in the current WriteAs API, so we just use the slug part
// for the date, to preserve the post order. We use the UTC date at noon.
ctime, err := time.Parse("2006-01-02T15:04:05", local.datePart+"T12:00:00")
if err != nil {
return err
}
_, err = ReqWithRetries[*writeas.Post](func() (*writeas.Post, error) {
return p.client.CreatePost(&writeas.PostParams{
Collection: p.collAlias,
Slug: local.slug,
Created: &ctime,
Content: content,
Title: local.title,
})
})
if err != nil {
return err
}
//err = p.setPostCtime(*newPost, local.title, ctime)
//if err != nil {
// return err
//}
}
return nil
}