~mendelmaleh/dummy

e97e60ace8d05a4e36b13f6f970e054759dd823a — Mendel E 1 year, 30 days ago 04d85b9
Add zip endpoint
1 files changed, 57 insertions(+), 0 deletions(-)

M cmd/dummyserver/main.go
M cmd/dummyserver/main.go => cmd/dummyserver/main.go +57 -0
@@ 1,6 1,7 @@
package main

import (
	"archive/zip"
	"fmt"
	"html/template"
	"image/png"


@@ 97,6 98,12 @@ func main() {
		},
	)

	http.HandleFunc(config.Dummy.Pattern+"zip/",
		func(w http.ResponseWriter, r *http.Request) {
			Zip(w, r, Options{dopt.Copy()})
		},
	)

	http.HandleFunc(config.Dummy.Pattern+"web/",
		func(w http.ResponseWriter, r *http.Request) {
			Web(w, r, Options{dopt.Copy()}, config, tmpl)


@@ 131,6 138,56 @@ func Dummy(w http.ResponseWriter, r *http.Request, opt Options) {
	// fmt.Fprintf(w, "%+v\n", dopt)
}

func Zip(w http.ResponseWriter, r *http.Request, opt Options) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	err = opt.FromForm(r.Form)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	files := []struct {
		name string
		size int
	}{
		{"favicon-16x16", 16},
		{"favicon-32x32", 32},
		{"apple-touch-icon", 180},
		{"android-chrome-192x192", 192},
		{"android-chrome-512x512", 512},
	}

	w.Header().Set("Content-Disposition", "attachment; filename=\"favicons.zip\"")
	w.Header().Set("Content-Type", "application/zip")

	archive := zip.NewWriter(w)

	for _, f := range files {
		opt.Width, opt.Height = f.size, f.size
		opt.DPI = float64(f.size) / 128 * 72

		file, err := archive.Create(f.name + ".png")
		if err != nil {
			fmt.Fprintln(w, err)
			return
		}

		png.Encode(file, dummy.Image(opt.Options))
	}

	err = archive.Close()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

}

func Web(w http.ResponseWriter, r *http.Request, opt Options, config Config, tmpl *template.Template) {
	err := r.ParseForm()
	if err != nil {