Added cleanup logic.
This commit is contained in:
12
.env
12
.env
@@ -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=http://localhost:8080
|
|
||||||
|
# Base URL for file downloads
|
||||||
|
BASE_URL=http://localhost:8080
|
||||||
|
|
||||||
|
# How long to keep uploaded files before deletion (examples: 1d, 12h, 30m)
|
||||||
|
DELETE_FILES_AFTER=1d
|
||||||
13
.env.example
13
.env.example
@@ -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=http://localhost:8080
|
# Base URL for file downloads
|
||||||
|
BASE_URL=http://localhost:8080
|
||||||
|
|
||||||
|
# How long to keep uploaded files before deletion (examples: 1d, 12h, 30m)
|
||||||
|
DELETE_FILES_AFTER=1d
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -205,4 +205,5 @@ $RECYCLE.BIN/
|
|||||||
|
|
||||||
|
|
||||||
# Application
|
# Application
|
||||||
uploads/**
|
uploads/**
|
||||||
|
.env
|
||||||
@@ -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
80
application/clean.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package endpoints
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package endpoints
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -13,4 +13,4 @@ func HandleIndex(c *gin.Context) {
|
|||||||
baseURL = "http://localhost:8080"
|
baseURL = "http://localhost:8080"
|
||||||
}
|
}
|
||||||
c.HTML(http.StatusOK, "index.html", gin.H{"BaseURL": baseURL})
|
c.HTML(http.StatusOK, "index.html", gin.H{"BaseURL": baseURL})
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package endpoints
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
11
main.go
11
main.go
@@ -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 != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user