Skip to content

Commit ab9abb9

Browse files
committed
Add local caching of team logos to prevent invalid URLs
1 parent d9bf800 commit ab9abb9

File tree

6 files changed

+84
-29
lines changed

6 files changed

+84
-29
lines changed

.DS_Store

2 KB
Binary file not shown.

igl.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"io/ioutil"
8+
"log"
79
"net/http"
10+
"os"
11+
"path/filepath"
12+
"strings"
813
)
914

10-
// const CircuitAPIURL = "https://indy-gaming-league-api.herokuapp.com/api/circuits?data={%22motherCircuitId%22:%225e4b290a420594ace7e97726%22,%22live%22:false,%22active%22:true,%22hidden%22:false,%20%22game%22:%22KILLER%20QUEEN%20BLACK%22}"
11-
// const CircuitAPIURL = "https://indy-gaming-league-api.herokuapp.com/api/circuits?data=%7B%22live%22:true,%22hidden%22:false,%22game%22:%22KILLER%20QUEEN%20BLACK%22%7D"
1215
const CircuitAPIURL = "https://indy-gaming-league-api.herokuapp.com/api/circuits?data=%7B%22motherCircuitId%22:%225ec8428a2cbb1c4439c29e6c%22,%22live%22:false,%22active%22:true,%22hidden%22:false,%22game%22:%22KILLER%20QUEEN%20BLACK%22%7D"
1316

1417
type IGLCircuit struct {
@@ -41,3 +44,45 @@ func GetIGLCircuits(c chan []IGLCircuit) {
4144

4245
c <- result.Circuits
4346
}
47+
48+
// DownloadFile will download a url to a local file. It's efficient because it will
49+
// write as it downloads and not load the whole file into memory.
50+
func DownloadFile(filepath string, url string) error {
51+
52+
// Get the data
53+
resp, err := http.Get(url)
54+
if err != nil {
55+
return err
56+
}
57+
defer resp.Body.Close()
58+
59+
// Create the file
60+
out, err := os.Create(filepath)
61+
if err != nil {
62+
return err
63+
}
64+
defer out.Close()
65+
66+
// Write the body to file
67+
_, err = io.Copy(out, resp.Body)
68+
return err
69+
}
70+
71+
// UpdateTeamLogo will Take Team as an input and download the logo locally
72+
// as a png file
73+
func UpdateTeamLogo(t *Team) {
74+
75+
if t.Img == "/avatar.png" {
76+
return
77+
}
78+
79+
log.Printf("Downloading Logo %s\n", t.Img)
80+
logoFile := fmt.Sprintf("%s.png", strings.Replace(t.Name, " ", "", -1))
81+
err := DownloadFile(filepath.Join(logoPath, logoFile), t.Img)
82+
if err != nil {
83+
log.Println("Could not save logo image")
84+
log.Println(err)
85+
return
86+
}
87+
t.Img = "http://localhost:8080/logo/" + logoFile
88+
}

main.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"net/http"
99
"os"
10+
"path/filepath"
1011
"strconv"
1112
"time"
1213

@@ -39,6 +40,7 @@ type Stats struct {
3940
var h Team = Team{"Blue Team", PlaceholderImage, 1, 1, Stats{1, 1, 1, 1}}
4041
var a Team = Team{"Gold Team", PlaceholderImage, 1, 1, Stats{1, 1, 1, 1}}
4142
var s Scoreboard = Scoreboard{&h, &a, 0, 0, 0, 0}
43+
var logoPath string
4244

4345
func setupLogs() {
4446
f, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
@@ -57,6 +59,9 @@ func main() {
5759
// Configure log output to file
5860
setupLogs()
5961

62+
// Create Directory For Logos
63+
SetupLogoDirectory()
64+
6065
// Setup HTTP handler and websocket handler
6166
StartHTTPServer()
6267

@@ -151,24 +156,30 @@ type IGLApiResponse struct {
151156
} `json:"data"`
152157
}
153158

154-
// OLD JSON parser
155-
// results := result["data"].([]interface{})
156-
// teams := []Team{}
157-
// for _, v := range results {
158-
// t := v.(map[string]interface{})["team"]
159-
// s := v.(map[string]interface{})["stats"]
160-
// team := Team{}
161-
// team.Name = t.(map[string]interface{})["formattedName"].(string)
162-
// team.Tier = int(t.(map[string]interface{})["tier"].(float64))
163-
// team.Div = int(t.(map[string]interface{})["div"].(float64))
164-
// if t.(map[string]interface{})["logo"] != nil {
165-
// team.Img = t.(map[string]interface{})["logo"].(string)
166-
// } else {
167-
// team.Img = KQBAvatarImage
168-
// }
169-
// team.Stats.GamesWon, _ = strconv.Atoi(s.(map[string]interface{})["Games Won"].(string))
170-
// team.Stats.GamesLost, _ = strconv.Atoi(s.(map[string]interface{})["Games Lost"].(string))
171-
// team.Stats.MatchesWon, _ = strconv.Atoi(s.(map[string]interface{})["Matches Won"].(string))
172-
// team.Stats.MatchesLost, _ = strconv.Atoi(s.(map[string]interface{})["Matches Lost"].(string))
173-
// teams = append(teams, team)
174-
// }
159+
// SetupLogoDirectory creates a directory to cache the local logo files
160+
func SetupLogoDirectory() {
161+
162+
// Get Current Working Directory
163+
path, err := os.Getwd()
164+
if err != nil {
165+
log.Println(err)
166+
}
167+
fmt.Println(path)
168+
169+
// Set Logo Path
170+
logoPath = filepath.Join(path, "logo")
171+
fmt.Println(logoPath)
172+
173+
// Create directory if it doesn't exist
174+
if _, err := os.Stat(logoPath); os.IsNotExist(err) {
175+
os.Mkdir(logoPath, 0755)
176+
}
177+
}
178+
179+
func tidyUp() {
180+
181+
// Remove logopath directory and files
182+
os.RemoveAll(logoPath)
183+
os.Remove("./output.log")
184+
log.Println("Exiting...")
185+
}

static/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
<article class="wrapper-left">
227227
<div class="one">
228228
<figure class="image is-64x64 home-outline">
229-
<img id="home-img" src="https://bulma.io/images/placeholders/128x128.png" alt="Image">
229+
<img id="home-img" src="https://bulma.io/images/placeholders/128x128.png" alt="http://localhost:8080/avatar.png">
230230
</figure>
231231
</div>
232232
<div class="two">
@@ -275,7 +275,7 @@
275275
</div>
276276
<div class="four">
277277
<figure class="image is-64x64 away-outline">
278-
<img id="away-img" src="https://bulma.io/images/placeholders/128x128.png" alt="Image">
278+
<img id="away-img" src="https://bulma.io/images/placeholders/128x128.png" alt="http://localhost:8080/avatar.png">
279279
</figure>
280280
</div>
281281
</article>

ui.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func IGLTeamSelection(w fyne.Window, apiUrl string) *fyne.Container {
112112
saveButton := widget.NewButton("Start Scoreboard", func() {
113113
log.Println("Saved.")
114114
s = Scoreboard{&blueTeam, &goldTeam, 0, 0, 0, 0}
115+
UpdateTeamLogo(&blueTeam)
116+
UpdateTeamLogo(&goldTeam)
115117
StartScoreboard(w)
116118
})
117119

@@ -304,7 +306,3 @@ func ProgressIndicator() *fyne.Container {
304306
container := fyne.NewContainerWithLayout(layout.NewVBoxLayout(), label, infinite)
305307
return container
306308
}
307-
308-
func tidyUp() {
309-
log.Println("Exiting...")
310-
}

websocket.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func StartHTTPServer() {
8282
http.HandleFunc("/echo", echo)
8383
box := packr.New("static", "./static")
8484
http.Handle("/", http.FileServer(box))
85+
http.Handle("/logo", http.FileServer(http.Dir(logoPath)))
8586
go func() {
8687
log.Fatal(http.ListenAndServe(":8080", nil))
8788
}()

0 commit comments

Comments
 (0)