Skip to content

Commit

Permalink
Merge pull request #1015 from JustaPenguin/real-penalty-download-logs
Browse files Browse the repository at this point in the history
add download logs functionality to realpenalty options
  • Loading branch information
cj123 authored Sep 16, 2020
2 parents 7699e0e + f1e423c commit 65b2dab
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ <h1 class="text-center">Real Penalty Options</h1>
<button class="btn btn-success float-right" type="submit">Save</button>

<div class="clearfix"></div>

<hr>

<h3>Download Logs</h3>

<p>You can download all of your Real Penalty logs in zip format here.</p>

<a class="btn btn-info" href="/realpenalty/logs">Download Logs</a>
</form>
{{ else }}
<p>Real Penalty is not installed. Please follow the instructions below to install Real Penalty, or contact your Server Administrator:</p>
Expand Down
55 changes: 55 additions & 0 deletions plugin_realpenalty.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 65b2dab

Please sign in to comment.