Skip to content

Commit

Permalink
added a seperate media endpoint for images
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Nov 1, 2024
1 parent 14859a4 commit 8610a05
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
29 changes: 22 additions & 7 deletions quiz/sheets.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package quiz

import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"image"
_ "image/jpeg"
"image/png"
"io"
"math"
"net/http"
Expand Down Expand Up @@ -114,7 +119,7 @@ func getQuestionFromRow(row *sheets.RowData) (qq *Question, err error) {
continue
}
cellContent := getContentFromCell(cell)
if cellContent == (DisplayableContent{}) {
if cellContent.Text == "" {
continue
}

Expand All @@ -138,7 +143,7 @@ func getQuestionFromRow(row *sheets.RowData) (qq *Question, err error) {
}

// validation
if qq.Question == (DisplayableContent{}) {
if qq.Question.Text == "" {
if len(qq.Correct) == 0 && len(qq.Wrong) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -224,16 +229,26 @@ func parseCellFormula(formula, parameter string) (content DisplayableContent) {
log.Printf("Error: get image from url '%s': %v", url, err)
return
}
data, err := io.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
data, _ := io.ReadAll(resp.Body)
log.Printf("Error: could not get image from url: got '%s': %s", resp.Status, string(data))
return
}

img, imgFormat, err := image.Decode(resp.Body)
if err != nil {
log.Printf("Error: reading image response: %v", err)
log.Printf("Error: decoding image from '%s': %v", url, err)
return
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
log.Printf("Error: could not get image from url: got '%s': %s", resp.Status, string(data))
hash := sha256.New()
imgData := &bytes.Buffer{}
err = png.Encode(io.MultiWriter(hash, imgData), img)
if err != nil {
log.Printf("Error: encoding image (%s to png): %v", imgFormat, err)
return
}
content.Text = string(data)
content.Text = fmt.Sprintf("%x", hash.Sum(nil))
content.Media = imgData.Bytes()
}
return
}
13 changes: 6 additions & 7 deletions quiz/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package quiz

import (
logger "log"
"math"
"math/rand"
"time"
)
Expand Down Expand Up @@ -72,8 +73,9 @@ type Question struct {
}

type DisplayableContent struct {
Type ContentType `json:"type"`
Text string `json:"text"`
Type ContentType `json:"type"`
Text string `json:"text"`
Media []byte `json:"-"`
}

type ContentType uint8
Expand Down Expand Up @@ -254,12 +256,9 @@ func (q Question) ToRound() Round {
rand.Shuffle(len(q.Wrong), func(i, j int) {
q.Wrong[i], q.Wrong[j] = q.Wrong[j], q.Wrong[i]
})
for _, a := range q.Wrong[:3] {
answers = append(answers, a)
}
} else {
answers = append(answers, q.Wrong...)
}
num_wrong := int(math.Min(float64(cap(q.Wrong)), 3))
answers = append(answers, q.Wrong[:num_wrong]...)

var correct int
rand.Shuffle(len(answers), func(i, j int) {
Expand Down
29 changes: 29 additions & 0 deletions webserver/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/kesuaheli/twitchgo"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -300,6 +301,34 @@ func getRound(w http.ResponseWriter, r *http.Request) {

w.Write(b)
}
func getRoundMedia(w http.ResponseWriter, r *http.Request) {
c, ok := isAuthorized(r)
if !ok {
w.WriteHeader(http.StatusUnauthorized)
return
}

if c.Game == nil {
w.WriteHeader(http.StatusBadRequest)
return
}

if c.Game.Current == 0 {
http.Error(w, "no active round", http.StatusNotFound)
return
}

media := mux.Vars(r)["media"]
round := *c.Game.Rounds[c.Game.Current-1]
for _, answer := range round.Answers {
if answer.Type == quiz.CONTENTTEXT || answer.Text != media {
continue
}
w.Write(answer.Media)
return
}
http.Error(w, "media not found", http.StatusNotFound)
}

func nextRound(w http.ResponseWriter, r *http.Request) {
c, ok := isAuthorized(r)
Expand Down
1 change: 1 addition & 0 deletions webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func initHandler() http.Handler {
r.HandleFunc("/game", handleGame)
r.HandleFunc("/vote/streamer", handleStreamerVote).Methods(http.MethodPost)
r.HandleFunc("/round", getRound).Methods(http.MethodGet)
r.HandleFunc("/round/media/{media}", getRoundMedia).Methods(http.MethodGet)
r.HandleFunc("/round/next", nextRound).Methods(http.MethodPost)

return r
Expand Down

0 comments on commit 8610a05

Please sign in to comment.