From 2d97ec49a4912c61191bc6ffc7f63b5c2f4c2975 Mon Sep 17 00:00:00 2001 From: Nils Jakobi Date: Tue, 17 Nov 2020 20:28:06 +0100 Subject: [PATCH] now working on windows, included more reward types --- go.mod | 1 - rewards.go | 24 +++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 2fbbc3b..9d21502 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module git.nils.zone/nils/goCake go 1.15 require ( - git.nils.zone/nils/prettify v0.0.4 github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.0.3 diff --git a/rewards.go b/rewards.go index 93e4a6c..c6a11ee 100644 --- a/rewards.go +++ b/rewards.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "net/http" + "runtime" "strconv" "strings" "time" @@ -29,6 +30,8 @@ type rewards struct { LapisDFI map[time.Month]string Lapis map[time.Month]string Referral map[time.Month]string + Airdrop map[time.Month]string + Swapped map[time.Month]string } // Rewards needs a comment @@ -115,6 +118,7 @@ func getOtherColors(color string, factor float64) string { func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) { // Maximum upload of 10 MB files r.ParseMultipartForm(10 << 20) + defer r.Body.Close() // Get handler for filename, size and headers file, handler, err := r.FormFile("csvFile") @@ -125,7 +129,8 @@ func uploadFile(w http.ResponseWriter, r *http.Request) ([]line, error) { defer file.Close() // check if we got a csv file - if handler.Header["Content-Type"][0] == "text/csv" { + switch handler.Header["Content-Type"][0] { + case "text/csv", "application/vnd.ms-excel": // accept this gift and read content of file fileContents, err := ioutil.ReadAll(file) if err != nil { @@ -181,6 +186,8 @@ func monthlyRewardOverview(lines []line) rewards { lapisDFI := make(map[time.Month]float64) lapis := make(map[time.Month]float64) referral := make(map[time.Month]float64) + airdrop := make(map[time.Month]float64) + swapped := make(map[time.Month]float64) // loop through all lines for i := range lines { @@ -196,6 +203,10 @@ func monthlyRewardOverview(lines []line) rewards { lapis[lines[i].Date.Month()] += lines[i].Amount case "Referral reward": referral[lines[i].Date.Month()] += lines[i].Amount + case "Bonus/Airdrop": + airdrop[lines[i].Date.Month()] += lines[i].Amount + case "Swapped in": + swapped[lines[i].Date.Month()] += lines[i].Amount } } @@ -209,7 +220,6 @@ func monthlyRewardOverview(lines []line) rewards { Lapis: fillSums(lapis, precision), Referral: fillSums(referral, precision), } - return r } @@ -232,7 +242,15 @@ func fillSums(s map[time.Month]float64, precision int) map[time.Month]string { func readUploadedFile(fileContents []byte) ([]line, error) { var lines []line - csvLines := strings.Split(string(fileContents), "\n") + var csvLines []string + + // newlines need to be handled differently on windows + switch runtime.GOOS { + case "windows": + csvLines = strings.Split(string(fileContents), "\r\n") + default: + csvLines = strings.Split(string(fileContents), "\n") + } // loop through all lines (except headers) for _, csvLine := range csvLines[1:] {