neko/internal/api/room/screen.go
Miroslav Šedivý 794728f4a8 add nolint.
2021-01-25 17:36:43 +01:00

119 lines
2.6 KiB
Go

package room
import (
"bytes"
"image/jpeg"
"strconv"
"net/http"
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
"demodesk/neko/internal/utils"
)
type ScreenConfigurationPayload struct {
Width int `json:"width"`
Height int `json:"height"`
Rate int16 `json:"rate"`
}
func (h *RoomHandler) screenConfiguration(w http.ResponseWriter, r *http.Request) {
size := h.desktop.GetScreenSize()
if size == nil {
utils.HttpInternalServerError(w, "Unable to get screen configuration.")
return
}
utils.HttpSuccess(w, ScreenConfigurationPayload{
Width: size.Width,
Height: size.Height,
Rate: size.Rate,
})
}
func (h *RoomHandler) screenConfigurationChange(w http.ResponseWriter, r *http.Request) {
data := &ScreenConfigurationPayload{}
if !utils.HttpJsonRequest(w, r, data) {
return
}
if err := h.desktop.SetScreenSize(types.ScreenSize{
Width: data.Width,
Height: data.Height,
Rate: data.Rate,
}); err != nil {
utils.HttpUnprocessableEntity(w, err)
return
}
h.sessions.Broadcast(
message.ScreenSize{
Event: event.SCREEN_UPDATED,
Width: data.Width,
Height: data.Height,
Rate: data.Rate,
}, nil)
utils.HttpSuccess(w, data)
}
func (h *RoomHandler) screenConfigurationsList(w http.ResponseWriter, r *http.Request) {
list := []ScreenConfigurationPayload{}
ScreenConfigurations := h.desktop.ScreenConfigurations()
for _, size := range ScreenConfigurations {
for _, fps := range size.Rates {
list = append(list, ScreenConfigurationPayload{
Width: size.Width,
Height: size.Height,
Rate: fps,
})
}
}
utils.HttpSuccess(w, list)
}
func (h *RoomHandler) screenShotGet(w http.ResponseWriter, r *http.Request) {
quality, err := strconv.Atoi(r.URL.Query().Get("quality"))
if err != nil {
quality = 90
}
img := h.desktop.GetScreenshotImage()
out := new(bytes.Buffer)
if err := jpeg.Encode(out, img, &jpeg.Options{
Quality: quality,
}); err != nil {
utils.HttpInternalServerError(w, err)
return
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
//nolint
w.Write(out.Bytes())
}
func (h *RoomHandler) screenCastGet(w http.ResponseWriter, r *http.Request) {
screencast := h.capture.Screencast()
if !screencast.Enabled() {
utils.HttpBadRequest(w, "Screencast pipeline is not enabled.")
return
}
bytes, err := screencast.Image()
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
//nolint
w.Write(bytes)
}