Skip to content

Instantly share code, notes, and snippets.

@carlosmn
Forked from omgjlk/screenshot.go
Created January 4, 2019 19:22
Show Gist options
  • Save carlosmn/b7002ad50a34626d6870df3b76745ecf to your computer and use it in GitHub Desktop.
Save carlosmn/b7002ad50a34626d6870df3b76745ecf to your computer and use it in GitHub Desktop.
// Perform snapshot of domain console
func DomainVNCSnapshot(w http.ResponseWriter, r *http.Request) {
ruuid := chi.URLParam(r, "uuid")
errstr := "Failure retrieving VM details"
domobj, err := GetDomObj(ruuid)
if err != nil {
http.Error(w, errstr, http.StatusInternalServerError)
return
}
defer domobj.Free()
// A screenshot is streamed so set up a non-blocking stream to receive it
st, err := conn.NewStream(1)
if err != nil {
http.Error(w, "Error setting up image stream", http.StatusInternalServerError)
return
}
defer st.Free()
mime, err := domobj.Screenshot(st, 0, 0)
if err != nil {
http.Error(w, "Error taking screenshot", http.StatusInternalServerError)
return
}
// We took the screenshot so we can tell the client everything went well
// and what kind of image they can expect.
w.Header().Set("Content-Type", mime)
w.WriteHeader(http.SatusOk)
// Copy the screenshot data into the client's stream
st.RecvAll(func(st *Stream, data []byte) (int, error) {
return w.Write(data)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment