~erock/pico

811452c1b778c062d82bd77753e85472b7e871a2 — Eric Bower 19 days ago 2cf902e
refactor(imgs): simplified logic in imgHandler
2 files changed, 13 insertions(+), 56 deletions(-)

M imgs/api.go
D imgs/html/read.page.tmpl
M imgs/api.go => imgs/api.go +13 -18
@@ 219,8 219,10 @@ func imgHandler(w http.ResponseWriter, h *ImgHandler) {
		return
	}

	contentType := post.MimeType
	fname := post.Filename
	if h.Optimized {
		contentType = "image/webp"
		fname = fmt.Sprintf("%s.webp", shared.SanitizeFileExt(post.Filename))
	}



@@ 232,25 234,18 @@ func imgHandler(w http.ResponseWriter, h *ImgHandler) {
	}
	defer contents.Close()

	if h.Optimized {
		w.Header().Add("Content-Type", "image/webp")
		if h.Img.Width != 0 || h.Img.Height != 0 {
			err := h.Img.Process(w, contents)
			if err != nil {
				h.Logger.Error(err)
			}
		} else {
			_, err := io.Copy(w, contents)
			if err != nil {
				h.Logger.Error(err)
			}
		}
	w.Header().Add("Content-Type", contentType)

	resizeImg := h.Img.Width != 0 || h.Img.Height != 0

	if h.Optimized && resizeImg {
		err = h.Img.Process(w, contents)
	} else {
		w.Header().Add("Content-Type", post.MimeType)
		_, err := io.Copy(w, contents)
		if err != nil {
			h.Logger.Error(err)
		}
		_, err = io.Copy(w, contents)
	}

	if err != nil {
		h.Logger.Error(err)
	}
}


D imgs/html/read.page.tmpl => imgs/html/read.page.tmpl +0 -38
@@ 1,38 0,0 @@
{{template "base" .}}

{{define "title"}}discover prose -- {{.Site.Domain}}{{end}}

{{define "meta"}}
<meta name="description" content="discover interesting posts" />
{{end}}

{{define "attrs"}}{{end}}

{{define "body"}}
<header class="text-center">
    <h1 class="text-2xl font-bold">read</h1>
    <p class="text-lg">recent posts</p>
    <p class="text-lg"><a href="/rss">rss</a></p>
    <hr />
</header>
<main>
    <div class="my">
        {{if .PrevPage}}<a href="{{.PrevPage}}">prev</a>{{else}}<span class="text-grey">prev</span>{{end}}
        {{if .NextPage}}<a href="{{.NextPage}}">next</a>{{else}}<span class="text-grey">next</span>{{end}}
    </div>
    {{range .Posts}}
    <article>
        <div class="flex items-center">
            <time datetime="{{.PublishAtISO}}" class="font-italic text-sm post-date">{{.PublishAt}}</time>
            <div class="flex-1">
                <div class="inline"><a href="{{.URL}}">{{.Title}}</a></div>
                <address class="text-sm inline">
                    <a href="{{.BlogURL}}" class="link-grey">({{.Username}})</a>
                </address>
            </div>
        </div>
    </article>
    {{end}}
</main>
{{template "marketing-footer" .}}
{{end}}