Commit 5d88b908 authored by Howl's avatar Howl
Browse files

remove hanayo profile backgrounds when donor expires

parent fd518a03
Loading
Loading
Loading
Loading
+22 −13
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ 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."`
	HanayoFolder string

	CalculateAccuracy bool
	CacheRankedScore  bool
@@ -33,6 +34,7 @@ type config struct {
	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())

hanayo.go

0 → 100644
+67 −0
Original line number Diff line number Diff line
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)
		}
	}
}