Created
January 5, 2017 05:28
-
-
Save groob/7af72b4d288afe7854d0ea1865aef183 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"image" | |
"image/draw" | |
"image/jpeg" | |
"image/png" | |
"io" | |
"log" | |
"net/http" | |
) | |
func handleUpload(w http.ResponseWriter, r *http.Request) { | |
err := r.ParseMultipartForm(100000) | |
if err != nil { | |
respondErr(err, w) | |
return | |
} | |
file, header, err := r.FormFile("wallpaper") | |
if err != nil { | |
respondErr(err, w) | |
return | |
} | |
defer file.Close() | |
ct := header.Header.Get("Content-Type") | |
img, err := convert(file, ct) | |
if err != nil { | |
respondErr(err, w) | |
return | |
} | |
w.Header().Set("Content-Disposition", "attachment; filename=com.apple.desktop.admin.png") | |
w.Header().Set("Content-Type", "image/png") | |
if err := png.Encode(w, img); err != nil { | |
respondErr(err, w) | |
return | |
} | |
} | |
type decodeFunc func(io.Reader) (image.Image, error) | |
func convert(file io.Reader, contentType string) (image.Image, error) { | |
var decode decodeFunc | |
switch contentType { | |
case "image/jpeg": | |
decode = jpeg.Decode | |
case "image/png": | |
decode = png.Decode | |
default: | |
return nil, fmt.Errorf("unrecognized image format: %s", contentType) | |
} | |
src, err := decode(file) | |
if err != nil { | |
return nil, err | |
} | |
img := image.NewRGBA(src.Bounds()) | |
draw.Draw(img, img.Bounds(), src, image.ZP, draw.Src) | |
// lower the alpha of one of the pixels by 1, to ensure | |
// that alpha is set | |
t := img.RGBAAt(0, 0) | |
t.A = t.A - 1 | |
img.Set(0, 0, t) | |
return img, nil | |
} | |
func respondErr(err error, w http.ResponseWriter) { | |
log.Println(err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
func index(w http.ResponseWriter, r *http.Request) { | |
uploadTmpl := ` | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Mac LoginWindow image.</title> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Convert LoginWindow Picture</h1> | |
<p>submit either a jpeg or a png. It will be converted to a png file with alpha channel set.</p> | |
<form class="form-upload" method="post" action="/upload" enctype="multipart/form-data"> | |
<fieldset> | |
<input type="file" name="wallpaper" id="wallpaper"> | |
<input type="submit" name="submit" value="Submit"> | |
</fieldset> | |
</form> | |
</body> | |
</html>` | |
w.Write([]byte(uploadTmpl)) | |
} | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/upload", handleUpload) | |
mux.HandleFunc("/", index) | |
http.ListenAndServe(":8080", mux) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment