mirror of https://github.com/JustKato/FreePad.git
+ Cleanup timer for old files
This commit is contained in:
parent
051a8a311c
commit
cf62661399
|
@ -16,5 +16,5 @@ API_BAN_LIMIT=300
|
||||||
# Wether or not to run it all in dev-mode
|
# Wether or not to run it all in dev-mode
|
||||||
DEV_MODE=0
|
DEV_MODE=0
|
||||||
|
|
||||||
# Maximum file storage age in minutes
|
# Maximum file storage age in minutes, set to -1 to disable
|
||||||
CLEANUP_MAX_AGE=1
|
CLEANUP_MAX_AGE=43200 # Default is a month
|
|
@ -1,22 +1,36 @@
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/JustKato/FreePad/lib/objects"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TaskManager() {
|
func TaskManager() {
|
||||||
|
|
||||||
// Run the migrations function
|
// Get the cleanup interval
|
||||||
go handleMigrations()
|
cleanupIntervalString, exists := os.LookupEnv("CLEANUP_MAX_AGE")
|
||||||
|
if !exists {
|
||||||
|
cleanupIntervalString = "-1"
|
||||||
|
}
|
||||||
|
|
||||||
for range time.Tick(time.Second * 5) {
|
if cleanupIntervalString == "-1" {
|
||||||
// fmt.Printf("%s\n", time.Now().Format("02/01/2006 03:04 PM"))
|
// Do not cleanup
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try and parse the string as an int
|
||||||
|
cleanupInterval, err := strconv.Atoi(cleanupIntervalString)
|
||||||
|
if err != nil {
|
||||||
|
cleanupInterval = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("[Task::Cleanup]: Task registered")
|
||||||
|
for range time.Tick(time.Minute * 5) {
|
||||||
|
objects.CleanupPosts(cleanupInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMigrations() {
|
|
||||||
time.AfterFunc(time.Second*30, func() {
|
|
||||||
// Run Migrations
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package objects
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
|
@ -94,3 +96,56 @@ func WritePost(p Post) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CleanupPosts(age int) {
|
||||||
|
// Initialize the files buffer
|
||||||
|
var files []string
|
||||||
|
|
||||||
|
// Get the base path
|
||||||
|
root := getStorageDirectory()
|
||||||
|
|
||||||
|
// Scan the directory
|
||||||
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||||
|
|
||||||
|
// Check that the file is not a dir
|
||||||
|
if !info.IsDir() {
|
||||||
|
files = append(files, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
// Check if we had any errors
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[Task::Cleanup]: Error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The timestamp where the file should be deleted
|
||||||
|
tooOldTime := time.Now()
|
||||||
|
|
||||||
|
// Go through all files and process them
|
||||||
|
for _, filePath := range files {
|
||||||
|
|
||||||
|
// Get last modified date
|
||||||
|
fileData, err := os.Stat(filePath)
|
||||||
|
if err == nil {
|
||||||
|
fileAge := fileData.ModTime()
|
||||||
|
|
||||||
|
// Check if the file is too old
|
||||||
|
if fileAge.Add(time.Duration(age)*time.Minute).Unix() < tooOldTime.Unix() {
|
||||||
|
fmt.Println("[Task::Cleanup]: Removing File", filePath)
|
||||||
|
|
||||||
|
// Remove the file
|
||||||
|
err = os.Remove(filePath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("[Task::Cleanup]: Failed to remove file", filePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fmt.Println("[Task::Cleanup]: Error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
3
main.go
3
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/JustKato/FreePad/lib/controllers"
|
||||||
"github.com/JustKato/FreePad/lib/routes"
|
"github.com/JustKato/FreePad/lib/routes"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
@ -19,7 +20,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the TaskManager
|
// Run the TaskManager
|
||||||
// go controllers.TaskManager()
|
go controllers.TaskManager()
|
||||||
|
|
||||||
// Initialize the router
|
// Initialize the router
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
Loading…
Reference in New Issue