Added cleanup logic.

This commit is contained in:
2025-08-07 22:25:02 +02:00
parent afccf236fc
commit 716c6730b5
9 changed files with 115 additions and 16 deletions

10
.env
View File

@@ -1,3 +1,13 @@
# FITRA environment variables
# Port to run the server on
PORT=8080 PORT=8080
# Directory for uploaded files
UPLOAD_DIR=./uploads UPLOAD_DIR=./uploads
# Base URL for file downloads
BASE_URL=http://localhost:8080 BASE_URL=http://localhost:8080
# How long to keep uploaded files before deletion (examples: 1d, 12h, 30m)
DELETE_FILES_AFTER=1d

View File

@@ -1,4 +1,13 @@
# FITRA environment variables
# Port to run the server on
PORT=8080 PORT=8080
# Directory for uploaded files
UPLOAD_DIR=./uploads UPLOAD_DIR=./uploads
MAX_FILE_SIZE=10MB
# Base URL for file downloads
BASE_URL=http://localhost:8080 BASE_URL=http://localhost:8080
# How long to keep uploaded files before deletion (examples: 1d, 12h, 30m)
DELETE_FILES_AFTER=1d

1
.gitignore vendored
View File

@@ -206,3 +206,4 @@ $RECYCLE.BIN/
# Application # Application
uploads/** uploads/**
.env

View File

@@ -1,4 +0,0 @@
This project is FITRA's backend, FITRA stands for file transfer.
GoLang is used for the backend.
This app is made for developers who needs to upload files via HTTP requests in CLI.

80
application/clean.go Normal file
View File

@@ -0,0 +1,80 @@
package application
import (
"fmt"
"log"
"os"
"time"
)
func ParseDuration(s string) (time.Duration, error) {
if s == "" {
return 24 * time.Hour, nil // Default to 1 day
}
return time.ParseDuration(s)
}
func StartCleanupRoutine() {
deleteAfterStr := os.Getenv("DELETE_FILES_AFTER")
deleteAfter, err := ParseDuration(deleteAfterStr)
if err != nil {
log.Printf("Invalid DELETE_FILES_AFTER value '%s', using default 1 day: %v", deleteAfterStr, err)
deleteAfter = 24 * time.Hour
}
log.Printf("File cleanup routine started, deleting files older than %v", deleteAfter)
ticker := time.NewTicker(time.Hour) // Check every hour
defer ticker.Stop()
for {
select {
case <-ticker.C:
CleanupOldFiles(deleteAfter)
}
}
}
func CleanupOldFiles(maxAge time.Duration) {
uploadsDir := os.Getenv("UPLOAD_DIR")
if uploadsDir == "" {
uploadsDir = "./uploads"
}
entries, err := os.ReadDir(uploadsDir)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("Error reading uploads directory: %v", err)
}
return
}
cutoffTime := time.Now().Add(-maxAge)
deletedCount := 0
for _, entry := range entries {
if !entry.IsDir() {
continue
}
dirPath := fmt.Sprintf("%s/%s", uploadsDir, entry.Name())
info, err := entry.Info()
if err != nil {
log.Printf("Error getting info for directory %s: %v", dirPath, err)
continue
}
if info.ModTime().Before(cutoffTime) {
if err := os.RemoveAll(dirPath); err != nil {
log.Printf("Error deleting directory %s: %v", dirPath, err)
} else {
deletedCount++
log.Printf("Deleted old directory: %s", dirPath)
}
}
}
if deletedCount > 0 {
log.Printf("Cleanup completed: deleted %d old directories", deletedCount)
}
}

View File

@@ -1,4 +1,4 @@
package endpoints package controllers
import ( import (
"net/http" "net/http"

View File

@@ -1,4 +1,4 @@
package endpoints package controllers
import ( import (
"net/http" "net/http"

View File

@@ -1,4 +1,4 @@
package endpoints package controllers
import ( import (
"crypto/rand" "crypto/rand"

11
main.go
View File

@@ -1,7 +1,8 @@
package main package main
import ( import (
"fitra-backend/endpoints" "fitra-backend/application"
"fitra-backend/controllers"
"fmt" "fmt"
"os" "os"
@@ -9,12 +10,14 @@ import (
) )
func main() { func main() {
go application.StartCleanupRoutine()
r := gin.Default() r := gin.Default()
r.LoadHTMLGlob("views/*") r.LoadHTMLGlob("views/*")
r.GET("/", endpoints.HandleIndex) r.GET("/", controllers.HandleIndex)
r.POST("/upload", endpoints.HandleFileUpload) r.POST("/upload", controllers.HandleFileUpload)
r.GET("/uploads/:fileID/:filename", endpoints.HandleFileDownload) r.GET("/uploads/:fileID/:filename", controllers.HandleFileDownload)
r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) }) r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) })
if port := os.Getenv("PORT"); port != "" { if port := os.Getenv("PORT"); port != "" {