play functionality for every versions

This commit is contained in:
2026-01-25 16:18:35 +01:00
parent ceeb8d53fb
commit f79ff46b0c
5 changed files with 132 additions and 11 deletions
+104 -4
View File
@@ -21,7 +21,7 @@ func NewPlayController(softwareService domain.SoftwareServiceInterface) *PlayCon
}
}
func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
func (c *PlayController) PlayV1(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Name not provided", http.StatusBadRequest)
@@ -38,6 +38,58 @@ func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
return
}
var webPlayableRelease *domain.Release
for _, release := range software.Releases {
if release.WebPlayable {
webPlayableRelease = &release
break
}
}
if webPlayableRelease == nil {
http.Error(w, "No web-playable version found for this software", http.StatusNotFound)
return
}
http.Redirect(w, r, fmt.Sprintf("/play/%s/%s", name, webPlayableRelease.Version), http.StatusFound)
}
func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Name not provided", http.StatusBadRequest)
return
}
version := chi.URLParam(r, "version")
if version == "" {
http.Error(w, "Version not provided", http.StatusBadRequest)
return
}
software, err := c.softwareService.GetByNameWithReleases(name)
if err != nil {
if err == domain.ErrSoftwareNotFound {
http.Error(w, "Software not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var webPlayableRelease *domain.Release
for _, release := range software.Releases {
if release.Version == version && release.WebPlayable {
webPlayableRelease = &release
break
}
}
if webPlayableRelease == nil {
http.Error(w, "No web-playable version found for this software", http.StatusNotFound)
return
}
tmpl, err := template_utils.GetTemplate("play_controller_play", "http/views/shared/layout.html", "http/views/play/play.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -45,11 +97,12 @@ func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
}
tmpl.Execute(w, map[string]interface{}{
"Software": software,
"Software": software,
"WebPlayableRelease": webPlayableRelease,
})
}
func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
func (c *PlayController) ServeContentV1(w http.ResponseWriter, r *http.Request) {
contentsPath := os.Getenv("GAMES_DIR")
name := chi.URLParam(r, "name")
if name == "" {
@@ -57,7 +110,26 @@ func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
return
}
htmlBaseDir := filepath.Join(contentsPath, "html", name)
software, err := c.softwareService.GetByNameWithReleases(name)
if err != nil {
http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound)
return
}
var webPlayableRelease *domain.Release
for _, release := range software.Releases {
if release.WebPlayable {
webPlayableRelease = &release
break
}
}
if webPlayableRelease == nil {
http.Error(w, "No web-playable version found for this software", http.StatusNotFound)
return
}
htmlBaseDir := filepath.Join(contentsPath, "html", name, webPlayableRelease.Version)
if _, err := os.Stat(htmlBaseDir); os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound)
@@ -70,3 +142,31 @@ func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
fs := http.StripPrefix(fmt.Sprintf("/play/%s/content", name), http.FileServer(http.Dir(htmlBaseDir)))
fs.ServeHTTP(w, r)
}
func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
contentsPath := os.Getenv("GAMES_DIR")
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Name not provided", http.StatusBadRequest)
return
}
version := chi.URLParam(r, "version")
if version == "" {
http.Error(w, "Version not provided", http.StatusBadRequest)
return
}
htmlBaseDir := filepath.Join(contentsPath, "html", name, version)
if _, err := os.Stat(htmlBaseDir); os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, fmt.Sprintf("Error accessing content for '%s': %v", name, err), http.StatusInternalServerError)
return
}
fs := http.StripPrefix(fmt.Sprintf("/play/%s/%s/content", name, version), http.FileServer(http.Dir(htmlBaseDir)))
fs.ServeHTTP(w, r)
}