Skip to content

Commit

Permalink
Add /latest/ URLs and add links to latest to view and download
Browse files Browse the repository at this point in the history
  • Loading branch information
JonEllis committed Feb 11, 2022
1 parent 07cba3d commit c9ea2e5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.0.9
- Add `/latest/<game name>` URL to allow refreshing the viewer to reload the latest save.
- Update interface to list latest download and view URLs.

## 0.0.8
- Add version printing with `-v` or `--version`.

Expand Down
43 changes: 40 additions & 3 deletions satisfactory-viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ type Save struct {
Filename string
DownloadUrl string
ViewUrl string
FullUrl string
Type string
Timestamp time.Time
SaveTime string
Filesize string
}

type Game struct {
Name string
Saves []Save
Name string
Saves []Save
LatestDownloadUrl string
LatestViewUrl string
}

type ListData struct {
Expand Down Expand Up @@ -112,6 +115,7 @@ func run(cmd *cobra.Command, args []string) error {
}

http.HandleFunc("/", index)
http.HandleFunc("/latest/", latest)

saveServer := http.FileServer(http.Dir(savePath))
http.Handle("/saves/", http.StripPrefix("/saves", saveServer))
Expand Down Expand Up @@ -141,6 +145,30 @@ func index(w http.ResponseWriter, req *http.Request) {
listPage.Execute(w, listData)
}

func latest(w http.ResponseWriter, req *http.Request) {
name := strings.TrimPrefix(req.URL.Path, "/latest/")
if (name == "") {
w.WriteHeader(404)
fmt.Fprintln(w, "404 Page Not Found")
return
}

games := getGameData(req.Host)
for _, game := range games {
if game.Name != name {
continue
}

w.Header().Set("Location", game.Saves[0].FullUrl)
w.WriteHeader(302)
return
}

w.WriteHeader(404)
fmt.Fprintln(w, "404 Page Not Found")
return
}

func getGameData(httpHost string) []Game {
var gameMap = make(map[string]Game)

Expand All @@ -166,7 +194,15 @@ func getGameData(httpHost string) []Game {

game, found := gameMap[gameName]
if !found {
game = Game{Name: gameName, Saves: []Save{}}
latestUrl := fmt.Sprintf("%s://%s/latest/%s", "https", httpHost, gameName)
latestViewUrl := fmt.Sprintf("https://satisfactory-calculator.com/en/interactive-map?url=%s", latestUrl)

game = Game{
Name: gameName,
LatestDownloadUrl: latestUrl,
LatestViewUrl: latestViewUrl,
Saves: []Save{},
}
}

downloadUri := fmt.Sprintf("/saves/%s", fileName)
Expand All @@ -176,6 +212,7 @@ func getGameData(httpHost string) []Game {
Filename: fileName,
DownloadUrl: downloadUri,
ViewUrl: fmt.Sprintf("https://satisfactory-calculator.com/en/interactive-map?url=%s", fullUrl),
FullUrl: fullUrl,
Type: saveType,
Timestamp: timestamp,
SaveTime: saveTime,
Expand Down
73 changes: 50 additions & 23 deletions templates/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@
width: 100%;
}

table.latest {
background-color: #ccc;
}
table.latest .btn {
background: #eee;
}
table.latest .btn:hover {
background: #ddd;
}

th {
text-align: left;
padding: 5px;
}

td {
Expand Down Expand Up @@ -57,32 +68,48 @@ <h1>Satisfactory Saves</h1>
{{ range .Games }}
<h2>{{ .Name }}</h2>

<table class="latest">
<tbody>
<tr>
<th>
Latest
</th>
</tr>
<tr>
<td>
<a class="btn" href="{{ .LatestDownloadUrl }}">Download</a>
<a class="btn" href="{{ .LatestViewUrl }}">View</a>
</td>
</tr>
</tbody>
</table>

{{ range .Saves }}
<table>
<tbody>
<tr>
<th>Filename:</th>
<td>{{ .Filename }}</td>
</tr>
<tr>
<th>Type:</th>
<td>{{ .Type }}</td>
</tr>
<tr>
<th>Date:</th>
<td>{{ .SaveTime }}</td>
</tr>
<tr>
<th>Size:</th>
<td>{{ .Filesize }}</td>
</tr>
<tr>
<th></th>
<td>
<a class="btn" href="{{ .DownloadUrl }}">Download</a>
<a class="btn" href="{{ .ViewUrl }}">View</a>
</td>
</tr>
<tr>
<th>Filename:</th>
<td>{{ .Filename }}</td>
</tr>
<tr>
<th>Type:</th>
<td>{{ .Type }}</td>
</tr>
<tr>
<th>Date:</th>
<td>{{ .SaveTime }}</td>
</tr>
<tr>
<th>Size:</th>
<td>{{ .Filesize }}</td>
</tr>
<tr>
<th></th>
<td>
<a class="btn" href="{{ .DownloadUrl }}">Download</a>
<a class="btn" href="{{ .ViewUrl }}">View</a>
</td>
</tr>
</tbody>
</table>
{{ end }}
Expand Down

0 comments on commit c9ea2e5

Please sign in to comment.