repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
811452c
parent
2cf902e
author
Eric Bower
date
2022-09-07 01:23:54 +0000 UTC
refactor(imgs): simplified logic in imgHandler
2 files changed,  +13, -56
M imgs/api.go
+13, -18
 1@@ -219,8 +219,10 @@ func imgHandler(w http.ResponseWriter, h *ImgHandler) {
 2 		return
 3 	}
 4 
 5+	contentType := post.MimeType
 6 	fname := post.Filename
 7 	if h.Optimized {
 8+		contentType = "image/webp"
 9 		fname = fmt.Sprintf("%s.webp", shared.SanitizeFileExt(post.Filename))
10 	}
11 
12@@ -232,25 +234,18 @@ func imgHandler(w http.ResponseWriter, h *ImgHandler) {
13 	}
14 	defer contents.Close()
15 
16-	if h.Optimized {
17-		w.Header().Add("Content-Type", "image/webp")
18-		if h.Img.Width != 0 || h.Img.Height != 0 {
19-			err := h.Img.Process(w, contents)
20-			if err != nil {
21-				h.Logger.Error(err)
22-			}
23-		} else {
24-			_, err := io.Copy(w, contents)
25-			if err != nil {
26-				h.Logger.Error(err)
27-			}
28-		}
29+	w.Header().Add("Content-Type", contentType)
30+
31+	resizeImg := h.Img.Width != 0 || h.Img.Height != 0
32+
33+	if h.Optimized && resizeImg {
34+		err = h.Img.Process(w, contents)
35 	} else {
36-		w.Header().Add("Content-Type", post.MimeType)
37-		_, err := io.Copy(w, contents)
38-		if err != nil {
39-			h.Logger.Error(err)
40-		}
41+		_, err = io.Copy(w, contents)
42+	}
43+
44+	if err != nil {
45+		h.Logger.Error(err)
46 	}
47 }
48 
D imgs/html/read.page.tmpl
+0, -38
 1@@ -1,38 +0,0 @@
 2-{{template "base" .}}
 3-
 4-{{define "title"}}discover prose -- {{.Site.Domain}}{{end}}
 5-
 6-{{define "meta"}}
 7-<meta name="description" content="discover interesting posts" />
 8-{{end}}
 9-
10-{{define "attrs"}}{{end}}
11-
12-{{define "body"}}
13-<header class="text-center">
14-    <h1 class="text-2xl font-bold">read</h1>
15-    <p class="text-lg">recent posts</p>
16-    <p class="text-lg"><a href="/rss">rss</a></p>
17-    <hr />
18-</header>
19-<main>
20-    <div class="my">
21-        {{if .PrevPage}}<a href="{{.PrevPage}}">prev</a>{{else}}<span class="text-grey">prev</span>{{end}}
22-        {{if .NextPage}}<a href="{{.NextPage}}">next</a>{{else}}<span class="text-grey">next</span>{{end}}
23-    </div>
24-    {{range .Posts}}
25-    <article>
26-        <div class="flex items-center">
27-            <time datetime="{{.PublishAtISO}}" class="font-italic text-sm post-date">{{.PublishAt}}</time>
28-            <div class="flex-1">
29-                <div class="inline"><a href="{{.URL}}">{{.Title}}</a></div>
30-                <address class="text-sm inline">
31-                    <a href="{{.BlogURL}}" class="link-grey">({{.Username}})</a>
32-                </address>
33-            </div>
34-        </div>
35-    </article>
36-    {{end}}
37-</main>
38-{{template "marketing-footer" .}}
39-{{end}}