- commit
- c774b03
- parent
- 70678e4
- author
- Eric Bower
- date
- 2022-08-23 00:00:44 +0000 UTC
refactor(imgs): remove markdown metadata uploader After further thought, using a second markdown file for the metadata of images was just a little too cumbersome. I also have a hunch that most people are not going to take the time to annotate their images into mini-blogs.
4 files changed,
+34,
-186
+3,
-10
1@@ -132,16 +132,9 @@ func (h *UploadImgHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
2 metadata.Post.PublishAt = post.PublishAt
3 }
4
5- if metadata.MimeType == mdMime {
6- err := h.writeMd(&metadata)
7- if err != nil {
8- return "", err
9- }
10- } else {
11- err := h.writeImg(&metadata)
12- if err != nil {
13- return "", err
14- }
15+ err = h.writeImg(&metadata)
16+ if err != nil {
17+ return "", err
18 }
19
20 url := h.Cfg.FullPostURL(
+24,
-14
1@@ -49,12 +49,7 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
2 ImgPath: fname,
3 }
4
5- if data.Cur != nil {
6- data.Text = data.Cur.Text
7- data.Title = data.Cur.Title
8- data.PublishAt = data.Cur.PublishAt
9- data.Description = data.Cur.Description
10- }
11+ data.Text = ""
12
13 return nil
14 }
15@@ -91,20 +86,35 @@ func (h *UploadImgHandler) writeImg(data *PostMetaData) error {
16 UserID: h.User.ID,
17 Space: h.Cfg.Space,
18
19- Data: data.Data,
20- Filename: data.Filename,
21- FileSize: data.FileSize,
22- Hidden: data.Hidden,
23- MimeType: data.MimeType,
24- PublishAt: data.PublishAt,
25- Shasum: data.Shasum,
26- Slug: data.Slug,
27+ Data: data.Data,
28+ Description: data.Description,
29+ Filename: data.Filename,
30+ FileSize: data.FileSize,
31+ Hidden: data.Hidden,
32+ MimeType: data.MimeType,
33+ PublishAt: data.PublishAt,
34+ Shasum: data.Shasum,
35+ Slug: data.Slug,
36+ Text: data.Text,
37+ Title: data.Title,
38 }
39 _, err := h.DBPool.InsertPost(&insertPost)
40 if err != nil {
41 h.Cfg.Logger.Errorf("error for %s: %v", data.Filename, err)
42 return fmt.Errorf("error for %s: %v", data.Filename, err)
43 }
44+
45+ if len(data.Tags) > 0 {
46+ h.Cfg.Logger.Infof(
47+ "Found (%s) post tags, replacing with old tags",
48+ strings.Join(data.Tags, ","),
49+ )
50+ err = h.DBPool.ReplaceTagsForPost(data.Tags, data.Post.ID)
51+ if err != nil {
52+ h.Cfg.Logger.Errorf("error for %s: %v", data.Filename, err)
53+ return fmt.Errorf("error for %s: %v", data.Filename, err)
54+ }
55+ }
56 } else {
57 if shared.Shasum(data.OrigText) == data.Cur.Shasum {
58 h.Cfg.Logger.Infof("(%s) found, but image is identical, skipping", data.Filename)
+0,
-155
1@@ -1,155 +0,0 @@
2-package uploadimgs
3-
4-import (
5- "fmt"
6- "strings"
7-
8- "git.sr.ht/~erock/pico/db"
9- "git.sr.ht/~erock/pico/shared"
10-)
11-
12-func (h *UploadImgHandler) validateMd(data *PostMetaData) (bool, error) {
13- if !shared.IsTextFile(data.Text) {
14- err := fmt.Errorf(
15- "WARNING: (%s) invalid file must be plain text (utf-8), skipping",
16- data.Filename,
17- )
18- return false, err
19- }
20-
21- if !shared.IsExtAllowed(data.Filename, []string{".md"}) {
22- err := fmt.Errorf(
23- "(%s) invalid file, format must be (.md), skipping",
24- data.Filename,
25- )
26- return false, err
27- }
28-
29- return true, nil
30-}
31-
32-func (h *UploadImgHandler) metaMd(data *PostMetaData) error {
33- parsedText, err := shared.ParseText(data.Text, "")
34- // we return nil here because we don't want the file upload to fail
35- if err != nil {
36- return nil
37- }
38-
39- if parsedText.Title == "" {
40- data.Title = shared.ToUpper(data.Slug)
41- } else {
42- data.Title = parsedText.Title
43- }
44-
45- data.Tags = parsedText.Tags
46- data.Description = parsedText.Description
47-
48- if parsedText.PublishAt != nil && !parsedText.PublishAt.IsZero() {
49- data.PublishAt = parsedText.MetaData.PublishAt
50- }
51-
52- if data.Cur != nil {
53- data.Filename = data.Cur.Filename
54- data.FileSize = data.Cur.FileSize
55- data.MimeType = data.Cur.MimeType
56- data.Data = data.Cur.Data
57- data.Shasum = data.Cur.Shasum
58- data.Slug = data.Cur.Slug
59- }
60-
61- if data.Description == "" {
62- data.Description = data.Title
63- }
64-
65- return nil
66-}
67-
68-func (h *UploadImgHandler) writeMd(data *PostMetaData) error {
69- valid, err := h.validateMd(data)
70- if !valid {
71- return err
72- }
73-
74- err = h.metaMd(data)
75- if err != nil {
76- return err
77- }
78-
79- if len(data.Text) == 0 {
80- err = h.removePost(data)
81- if err != nil {
82- return err
83- }
84- } else if data.Cur == nil {
85- h.Cfg.Logger.Infof("(%s) not found, adding record", data.Filename)
86- insertPost := db.Post{
87- UserID: h.User.ID,
88- Space: h.Cfg.Space,
89-
90- Data: data.Data,
91- Description: data.Description,
92- Filename: data.Filename,
93- FileSize: data.FileSize,
94- Hidden: data.Hidden,
95- MimeType: data.MimeType,
96- PublishAt: data.PublishAt,
97- Shasum: data.Shasum,
98- Slug: data.Slug,
99- Text: data.Text,
100- Title: data.Title,
101- }
102- post, err := h.DBPool.InsertPost(&insertPost)
103- if err != nil {
104- h.Cfg.Logger.Errorf("error for %s: %v", data.Filename, err)
105- return fmt.Errorf("error for %s: %v", data.Filename, err)
106- }
107-
108- if len(data.Tags) > 0 {
109- h.Cfg.Logger.Infof(
110- "Found (%s) post tags, replacing with old tags",
111- strings.Join(data.Tags, ","),
112- )
113- err = h.DBPool.ReplaceTagsForPost(data.Tags, post.ID)
114- if err != nil {
115- h.Cfg.Logger.Errorf("error for %s: %v", data.Filename, err)
116- return fmt.Errorf("error for %s: %v", data.Filename, err)
117- }
118- }
119- } else {
120- if data.Text == data.Cur.Text {
121- h.Cfg.Logger.Infof("(%s) found, but metadata is identical, skipping", data.Filename)
122- return nil
123- }
124-
125- h.Cfg.Logger.Infof("(%s) found, updating record", data.Filename)
126- updatePost := db.Post{
127- ID: data.Cur.ID,
128-
129- Data: data.Data,
130- FileSize: data.FileSize,
131- Description: data.Description,
132- PublishAt: data.PublishAt,
133- Slug: data.Slug,
134- Shasum: data.Shasum,
135- Text: data.Text,
136- Title: data.Title,
137- }
138- _, err = h.DBPool.UpdatePost(&updatePost)
139- if err != nil {
140- h.Cfg.Logger.Errorf("error for %s: %v", data.Filename, err)
141- return fmt.Errorf("error for %s: %v", data.Filename, err)
142- }
143-
144- h.Cfg.Logger.Infof(
145- "Found (%s) post tags, replacing with old tags",
146- strings.Join(data.Tags, ","),
147- )
148- err = h.DBPool.ReplaceTagsForPost(data.Tags, data.Cur.ID)
149- if err != nil {
150- h.Cfg.Logger.Errorf("error for %s: %v", data.Filename, err)
151- return fmt.Errorf("error for %s: %v", data.Filename, err)
152- }
153- }
154-
155- return nil
156-}
+7,
-7
1@@ -80,12 +80,12 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
2 return client.Upload(s, entry)
3 }
4
5- var text []byte
6+ var origText []byte
7 if b, err := io.ReadAll(entry.Reader); err == nil {
8- text = b
9+ origText = b
10 }
11
12- mimeType := http.DetectContentType(text)
13+ mimeType := http.DetectContentType(origText)
14 ext := path.Ext(filename)
15 // DetectContentType does not detect markdown
16 if ext == ".md" {
17@@ -94,14 +94,14 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
18
19 now := time.Now()
20 slug := shared.SanitizeFileExt(filename)
21- fileSize := binary.Size(text)
22- shasum := shared.Shasum(text)
23+ fileSize := binary.Size(origText)
24+ shasum := shared.Shasum(origText)
25
26 nextPost := db.Post{
27 Filename: filename,
28 Slug: slug,
29 PublishAt: &now,
30- Text: string(text),
31+ Text: string(origText),
32 MimeType: mimeType,
33 FileSize: fileSize,
34 Shasum: shasum,
35@@ -137,7 +137,7 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
36 }
37
38 // if the file is empty we remove it from our database
39- if len(text) == 0 {
40+ if len(origText) == 0 {
41 // skip empty files from being added to db
42 if post == nil {
43 logger.Infof("(%s) is empty, skipping record", filename)