Skip to content

Instantly share code, notes, and snippets.

@spotlightishere
Last active July 24, 2020 00:45
Show Gist options
  • Save spotlightishere/9cc55febe29094a417a946e42dd00583 to your computer and use it in GitHub Desktop.
Save spotlightishere/9cc55febe29094a417a946e42dd00583 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/png"
"log"
"net/http"
"strconv"
)
var global image.Image
func main() {
http.HandleFunc("/", primaryHandler)
http.HandleFunc("/img", imageHandler)
log.Println(http.ListenAndServeTLS("[::1]:443", "cert.pem", "key.pem", http.DefaultServeMux))
}
func primaryHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Write([]byte(`
<!DOCTYPE html>
<html>
<head>
<title>puuuush</title>
</head>
<body>
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
`))
case "POST":
file, _, _ := r.FormFile("fileToUpload")
// Interpret image
img, _ := png.Decode(file)
log.Println(img.Bounds())
global = img
pusher, ok := w.(http.Pusher)
if !ok {
w.WriteHeader(500)
w.Write([]byte("lol"))
return
}
body := `
<!DOCTYPE html>
<html>
<head>
<title>puuuush</title>
<style>
body {
line-height: 0px;
font-size: 0px;
}
</style>
</head>
<body>
`
// Run through the y axis so we can create a new line on images.
maxX := img.Bounds().Max.X
maxY := img.Bounds().Max.Y
for y := 0; y < maxY; y++ {
// Register all X positions possible for this area.
for x := 0; x < maxX; x++ {
url := fmt.Sprintf("/img?x=%d&y=%d", x, y)
body += fmt.Sprintf("<img src='%s'>", url)
if err := pusher.Push(url, nil); err != nil {
log.Printf("Failed to push: %v", err)
}
}
// And now, a newline.
body += "<br>"
log.Println(y)
}
// And we're done.
body += `
</body>
</html>
`
log.Print("Sending...")
w.Write([]byte(body))
default:
w.Write([]byte("what the fuck are you doing?"))
}
return
}
func imageHandler(w http.ResponseWriter, r *http.Request) {
queries := r.URL.Query()
xS := queries["x"][0]
x, _ := strconv.Atoi(xS)
yS := queries["y"][0]
y, _ := strconv.Atoi(yS)
// I am so sorry.
img := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{1, 1}})
img.Set(0, 0, global.At(x, y))
// I truly am sorry.
w.Header().Set("Content-Type", "image/png")
png.Encode(w, img)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment