diff --git a/CHANGELOG.md b/CHANGELOG.md index f63215966..fa018503f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Added: * New lua plugin file (race-control.lua) containing onChat function which is called whenever a chat message is sent in-game, from the live timings page or from a lua script * (Multiserver) Events across all servers will now be shown on the Calendar, events on other servers will be marked as so. * Events using "Any Available Car" now split the car types evenly between the number of entrants. +* Added a "Download Logs" button to the Real Penalty Options page. You can use this to view logs from your previous Real Penalty sessions, including the penalties applied by the tool. Fixed: diff --git a/cmd/server-manager/views/pages/server/realpenalty-options.html b/cmd/server-manager/views/pages/server/realpenalty-options.html index eba44b9fd..37d570f23 100644 --- a/cmd/server-manager/views/pages/server/realpenalty-options.html +++ b/cmd/server-manager/views/pages/server/realpenalty-options.html @@ -26,6 +26,14 @@
You can download all of your Real Penalty logs in zip format here.
+ + Download Logs {{ else }}Real Penalty is not installed. Please follow the instructions below to install Real Penalty, or contact your Server Administrator:
diff --git a/plugin_realpenalty.go b/plugin_realpenalty.go index 4f58230fa..332940b7d 100644 --- a/plugin_realpenalty.go +++ b/plugin_realpenalty.go @@ -1,11 +1,16 @@ package servermanager import ( + "archive/zip" + "fmt" "html/template" + "io" + "io/ioutil" "net/http" "os" "path/filepath" "runtime" + "time" "github.com/sirupsen/logrus" ) @@ -108,3 +113,53 @@ func (rph *RealPenaltyHandler) options(w http.ResponseWriter, r *http.Request) { RealPenaltySupportedVersion: RealPenaltySupportedVersion, }) } + +func (rph *RealPenaltyHandler) downloadLogs(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment;filename="realpenalty_logs_%s.zip"`, time.Now().Format("2006-01-02_15_04"))) + w.Header().Set("Content-Type", "application/zip") + + if err := rph.buildLogZip(w); err != nil { + logrus.WithError(err).Errorf("Could not create real penalty log zip") + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } +} + +func (rph *RealPenaltyHandler) buildLogZip(w io.Writer) error { + z := zip.NewWriter(w) + defer z.Close() + + logFiles, err := ioutil.ReadDir(filepath.Join(RealPenaltyFolderPath(), "logs")) + + if err != nil { + return err + } + + for _, file := range logFiles { + if err := rph.writeRealPenaltyLogFileToZip(z, file); err != nil { + return err + } + } + + return nil +} + +func (rph *RealPenaltyHandler) writeRealPenaltyLogFileToZip(z *zip.Writer, info os.FileInfo) error { + zf, err := z.Create(info.Name()) + + if err != nil { + return err + } + + f, err := os.Open(filepath.Join(filepath.Join(RealPenaltyFolderPath(), "logs"), info.Name())) + + if err != nil { + return err + } + + defer f.Close() + + _, err = io.Copy(zf, f) + + return err +} diff --git a/router.go b/router.go index 192c7199f..198a52b7b 100644 --- a/router.go +++ b/router.go @@ -348,6 +348,7 @@ func Router( r.HandleFunc("/stracker/options", strackerHandler.options) r.HandleFunc("/kissmyrank/options", kissMyRankHandler.options) r.HandleFunc("/realpenalty/options", realPenaltyHandler.options) + r.HandleFunc("/realpenalty/logs", realPenaltyHandler.downloadLogs) }) FileServer(r, "/static", fs, false)