Skip to content

Commit 8610a05

Browse files
committed
added a seperate media endpoint for images
1 parent 14859a4 commit 8610a05

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

quiz/sheets.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package quiz
22

33
import (
4+
"bytes"
5+
"crypto/sha256"
46
"encoding/json"
57
"fmt"
8+
"image"
9+
_ "image/jpeg"
10+
"image/png"
611
"io"
712
"math"
813
"net/http"
@@ -114,7 +119,7 @@ func getQuestionFromRow(row *sheets.RowData) (qq *Question, err error) {
114119
continue
115120
}
116121
cellContent := getContentFromCell(cell)
117-
if cellContent == (DisplayableContent{}) {
122+
if cellContent.Text == "" {
118123
continue
119124
}
120125

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

140145
// validation
141-
if qq.Question == (DisplayableContent{}) {
146+
if qq.Question.Text == "" {
142147
if len(qq.Correct) == 0 && len(qq.Wrong) == 0 {
143148
return nil, nil
144149
}
@@ -224,16 +229,26 @@ func parseCellFormula(formula, parameter string) (content DisplayableContent) {
224229
log.Printf("Error: get image from url '%s': %v", url, err)
225230
return
226231
}
227-
data, err := io.ReadAll(resp.Body)
232+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
233+
data, _ := io.ReadAll(resp.Body)
234+
log.Printf("Error: could not get image from url: got '%s': %s", resp.Status, string(data))
235+
return
236+
}
237+
238+
img, imgFormat, err := image.Decode(resp.Body)
228239
if err != nil {
229-
log.Printf("Error: reading image response: %v", err)
240+
log.Printf("Error: decoding image from '%s': %v", url, err)
230241
return
231242
}
232-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
233-
log.Printf("Error: could not get image from url: got '%s': %s", resp.Status, string(data))
243+
hash := sha256.New()
244+
imgData := &bytes.Buffer{}
245+
err = png.Encode(io.MultiWriter(hash, imgData), img)
246+
if err != nil {
247+
log.Printf("Error: encoding image (%s to png): %v", imgFormat, err)
234248
return
235249
}
236-
content.Text = string(data)
250+
content.Text = fmt.Sprintf("%x", hash.Sum(nil))
251+
content.Media = imgData.Bytes()
237252
}
238253
return
239254
}

quiz/types.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package quiz
22

33
import (
44
logger "log"
5+
"math"
56
"math/rand"
67
"time"
78
)
@@ -72,8 +73,9 @@ type Question struct {
7273
}
7374

7475
type DisplayableContent struct {
75-
Type ContentType `json:"type"`
76-
Text string `json:"text"`
76+
Type ContentType `json:"type"`
77+
Text string `json:"text"`
78+
Media []byte `json:"-"`
7779
}
7880

7981
type ContentType uint8
@@ -254,12 +256,9 @@ func (q Question) ToRound() Round {
254256
rand.Shuffle(len(q.Wrong), func(i, j int) {
255257
q.Wrong[i], q.Wrong[j] = q.Wrong[j], q.Wrong[i]
256258
})
257-
for _, a := range q.Wrong[:3] {
258-
answers = append(answers, a)
259-
}
260-
} else {
261-
answers = append(answers, q.Wrong...)
262259
}
260+
num_wrong := int(math.Min(float64(cap(q.Wrong)), 3))
261+
answers = append(answers, q.Wrong[:num_wrong]...)
263262

264263
var correct int
265264
rand.Shuffle(len(answers), func(i, j int) {

webserver/handle.go

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/google/uuid"
14+
"github.com/gorilla/mux"
1415
"github.com/kesuaheli/twitchgo"
1516
"github.com/spf13/viper"
1617
)
@@ -300,6 +301,34 @@ func getRound(w http.ResponseWriter, r *http.Request) {
300301

301302
w.Write(b)
302303
}
304+
func getRoundMedia(w http.ResponseWriter, r *http.Request) {
305+
c, ok := isAuthorized(r)
306+
if !ok {
307+
w.WriteHeader(http.StatusUnauthorized)
308+
return
309+
}
310+
311+
if c.Game == nil {
312+
w.WriteHeader(http.StatusBadRequest)
313+
return
314+
}
315+
316+
if c.Game.Current == 0 {
317+
http.Error(w, "no active round", http.StatusNotFound)
318+
return
319+
}
320+
321+
media := mux.Vars(r)["media"]
322+
round := *c.Game.Rounds[c.Game.Current-1]
323+
for _, answer := range round.Answers {
324+
if answer.Type == quiz.CONTENTTEXT || answer.Text != media {
325+
continue
326+
}
327+
w.Write(answer.Media)
328+
return
329+
}
330+
http.Error(w, "media not found", http.StatusNotFound)
331+
}
303332

304333
func nextRound(w http.ResponseWriter, r *http.Request) {
305334
c, ok := isAuthorized(r)

webserver/webserver.go

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func initHandler() http.Handler {
7272
r.HandleFunc("/game", handleGame)
7373
r.HandleFunc("/vote/streamer", handleStreamerVote).Methods(http.MethodPost)
7474
r.HandleFunc("/round", getRound).Methods(http.MethodGet)
75+
r.HandleFunc("/round/media/{media}", getRoundMedia).Methods(http.MethodGet)
7576
r.HandleFunc("/round/next", nextRound).Methods(http.MethodPost)
7677

7778
return r

0 commit comments

Comments
 (0)