~mendelmaleh/dummy

d54219c72a7c72e64b34faa979aaac8249f1a1eb — Mendel E 1 year, 28 days ago d0dd20f
Move tar and zip endpoints to archive.go
2 files changed, 122 insertions(+), 113 deletions(-)

A cmd/dummyserver/archive.go
M cmd/dummyserver/main.go
A cmd/dummyserver/archive.go => cmd/dummyserver/archive.go +122 -0
@@ 0,0 1,122 @@
package main

import (
	"archive/tar"
	"archive/zip"
	"bytes"
	"fmt"
	"image/png"
	"io"
	"net/http"

	"git.sr.ht/~mendelmaleh/dummy"
)

func Tar(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.tar\"")
	w.Header().Set("Content-Type", "application/tar")

	archive := tar.NewWriter(w)

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

		var buf bytes.Buffer
		png.Encode(&buf, dummy.Image(opt.Options))

		hdr := &tar.Header{
			Name: f.name,
			Mode: 0600,
			Size: int64(buf.Len()),
		}

		if err := archive.WriteHeader(hdr); err != nil {
			fmt.Fprintln(w, err)
			return
		}

		if _, err := io.Copy(archive, &buf); err != nil {
			fmt.Fprintln(w, err)
			return
		}
	}

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

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
	}

}

M cmd/dummyserver/main.go => cmd/dummyserver/main.go +0 -113
@@ 1,13 1,9 @@
package main

import (
	"archive/tar"
	"archive/zip"
	"bytes"
	"fmt"
	"html/template"
	"image/png"
	"io"
	"io/ioutil"
	"log"
	"net"


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

func Tar(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.tar\"")
	w.Header().Set("Content-Type", "application/tar")

	archive := tar.NewWriter(w)

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

		var buf bytes.Buffer
		png.Encode(&buf, dummy.Image(opt.Options))

		hdr := &tar.Header{
			Name: f.name,
			Mode: 0600,
			Size: int64(buf.Len()),
		}

		if err := archive.WriteHeader(hdr); err != nil {
			fmt.Fprintln(w, err)
			return
		}

		if _, err := io.Copy(archive, &buf); err != nil {
			fmt.Fprintln(w, err)
			return
		}
	}

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

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 {