From 5d88b9089274888f493aea29da36d5238bc25237 Mon Sep 17 00:00:00 2001 From: Howl Date: Mon, 28 Nov 2016 19:10:02 +0100 Subject: [PATCH] remove hanayo profile backgrounds when donor expires --- cron.go | 35 ++++++++++++++++++----------- hanayo.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 hanayo.go diff --git a/cron.go b/cron.go index 26db200..1afb905 100644 --- a/cron.go +++ b/cron.go @@ -14,25 +14,27 @@ import ( ) type config struct { - DSN string - RippleDir string `description:"The ripple folder (e.g. /var/www/ripple, NOT /var/www/ripple/osu.ppy.sh). Write the directory relatively to where the ripple-cron-go executable is placed."` + DSN string + RippleDir string `description:"The ripple folder (e.g. /var/www/ripple, NOT /var/www/ripple/osu.ppy.sh). Write the directory relatively to where the ripple-cron-go executable is placed."` + HanayoFolder string CalculateAccuracy bool CacheRankedScore bool CacheTotalHits bool CacheLevel bool - DeleteOldPasswordResets bool - CleanReplays bool - DeleteReplayCache bool - BuildLeaderboards bool - CalculatePP bool - FixScoreDuplicates bool `description:"might take a VERY long time"` - CalculateOverallAccuracy bool - FixCompletedScores bool `description:"Set to completed = 2 all scores on beatmaps that aren't ranked."` - UnrankScoresOnInvalidBeatmaps bool `description:"Set to completed = 2 all scores on beatmaps that are not in the database."` - RemoveDonorOnExpired bool - FixMultipleCompletedScores bool `description:"Set completed=2 if multiple completed=3 scores for same beatmap and user are present."` + DeleteOldPasswordResets bool + CleanReplays bool + DeleteReplayCache bool + BuildLeaderboards bool + CalculatePP bool + FixScoreDuplicates bool `description:"might take a VERY long time"` + CalculateOverallAccuracy bool + FixCompletedScores bool `description:"Set to completed = 2 all scores on beatmaps that aren't ranked."` + UnrankScoresOnInvalidBeatmaps bool `description:"Set to completed = 2 all scores on beatmaps that are not in the database."` + RemoveDonorOnExpired bool + FixMultipleCompletedScores bool `description:"Set completed=2 if multiple completed=3 scores for same beatmap and user are present."` + ClearExpiredProfileBackgrounds bool Workers int `description:"The number of goroutines which should execute queries. Increasing it may make cron faster, depending on your system."` } @@ -185,6 +187,13 @@ func main() { go opFixMultipleCompletedScores() color.Green(" ok!") } + if c.ClearExpiredProfileBackgrounds { + fmt.Print("Removing profile backgrounds of expired donors...") + wg.Add(1) + go opClearExpiredProfileBackgrounds() + color.Green(" ok!") + } + wg.Wait() color.Green("Data elaboration has been terminated.") color.Green("Execution time: %.4fs", time.Now().Sub(timeAtStart).Seconds()) diff --git a/hanayo.go b/hanayo.go new file mode 100644 index 0000000..487c49e --- /dev/null +++ b/hanayo.go @@ -0,0 +1,67 @@ +package main + +import ( + "io/ioutil" + + "os" + + "github.com/fatih/color" +) + +func opClearExpiredProfileBackgrounds() { + defer wg.Done() + + if c.HanayoFolder == "" { + color.Red("> ClearExpiredProfileBackgrounds: HanayoFolder is empty. ignoring") + return + } + + // get all the backgrounds + elementsRaw, err := ioutil.ReadDir(c.HanayoFolder + "/static/profbackgrounds") + if err != nil { + color.Red("> ClearExpiredProfileBackgrounds: failed to get profile backgrounds: %v", err) + return + } + + // convert to []string + elements := make([]string, len(elementsRaw)) + for i, v := range elementsRaw { + if v.Name() == ".keep" { + continue + } + elements[i] = v.Name() + } + + // get profile backgrounds in db + const q = "SELECT uid FROM profile_backgrounds WHERE type = 2" + inDB, err := db.Query(q) + if err != nil { + queryError(err, q) + } + + // remove from elements every background that does actually exist in the database + for inDB.Next() { + var i string + err := inDB.Scan(&i) + if err != nil { + queryError(err, q) + return + } + for pos, e := range elements { + if e == i+".jpg" { + // remove from elements if exists + elements[pos] = elements[len(elements)-1] + elements = elements[:len(elements)-1] + break + } + } + } + + // remove all elements still left + for _, e := range elements { + err := os.Remove(c.HanayoFolder + "/static/profbackgrounds/" + e) + if err != nil { + color.Red("> ClearExpiredProfileBackgrounds: failed to delete a background: %v", err) + } + } +} -- GitLab