Added tracking for uploading and downloading files

This commit is contained in:
2025-08-17 00:47:10 +02:00
parent 91fb6c62aa
commit 496f9140a1
8 changed files with 65 additions and 1 deletions

View File

@@ -14,3 +14,6 @@ DELETE_FILES_AFTER=1d
# Maximum storage capacity in GB
MAX_STORAGE_GB=10
# Rybbit API Key
ANALYTICS_KEY=

View File

@@ -6,6 +6,7 @@ import (
"path/filepath"
"github.com/gin-gonic/gin"
"fitra-backend/utils"
)
func HandleFileDownload(c *gin.Context) {
@@ -23,6 +24,9 @@ func HandleFileDownload(c *gin.Context) {
return
}
// Send analytics event
utils.SendAnalyticsEvent("file_download", "/api/download/"+fileID+"/"+filename, "File Download", c.GetHeader("User-Agent"), c.ClientIP())
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename)

View File

@@ -10,6 +10,7 @@ import (
"strconv"
"github.com/gin-gonic/gin"
"fitra-backend/utils"
)
func generateID() (string, error) {
@@ -99,6 +100,9 @@ func HandleFileUpload(c *gin.Context) {
baseURL = "http://localhost:8080"
}
// Send analytics event
utils.SendAnalyticsEvent("file_upload", "/api/upload", "File Upload", c.GetHeader("User-Agent"), c.ClientIP())
c.JSON(http.StatusOK, gin.H{
"message": "File uploaded successfully",
"id": fileID,

Binary file not shown.

Binary file not shown.

BIN
dist/fitra-linux-amd64 vendored

Binary file not shown.

BIN
dist/fitra-linux-arm64 vendored

Binary file not shown.

53
utils/analytics.go Normal file
View File

@@ -0,0 +1,53 @@
package utils
import (
"bytes"
"encoding/json"
"net/http"
"os"
)
type AnalyticsEvent struct {
APIKey string `json:"api_key"`
SiteID string `json:"site_id"`
Type string `json:"type"`
Pathname string `json:"pathname"`
Hostname string `json:"hostname"`
PageTitle string `json:"page_title"`
UserAgent string `json:"user_agent"`
IPAddress string `json:"ip_address"`
}
func SendAnalyticsEvent(eventType, pathname, pageTitle, userAgent, ipAddress string) error {
apiKey := os.Getenv("ANALYTICS_KEY")
if apiKey == "" {
return nil // Skip if no API key configured
}
event := AnalyticsEvent{
APIKey: apiKey,
SiteID: "fitra-backend",
Type: eventType,
Pathname: pathname,
Hostname: "a.adhd.sh",
PageTitle: pageTitle,
UserAgent: userAgent,
IPAddress: ipAddress,
}
jsonData, err := json.Marshal(event)
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://app.rybbit.io/api/track", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
_, err = client.Do(req)
return err
}