Added tracking for uploading and downloading files
This commit is contained in:
@@ -13,4 +13,7 @@ BASE_URL=http://localhost:8080
|
|||||||
DELETE_FILES_AFTER=1d
|
DELETE_FILES_AFTER=1d
|
||||||
|
|
||||||
# Maximum storage capacity in GB
|
# Maximum storage capacity in GB
|
||||||
MAX_STORAGE_GB=10
|
MAX_STORAGE_GB=10
|
||||||
|
|
||||||
|
# Rybbit API Key
|
||||||
|
ANALYTICS_KEY=
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"fitra-backend/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleFileDownload(c *gin.Context) {
|
func HandleFileDownload(c *gin.Context) {
|
||||||
@@ -23,6 +24,9 @@ func HandleFileDownload(c *gin.Context) {
|
|||||||
return
|
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-Description", "File Transfer")
|
||||||
c.Header("Content-Transfer-Encoding", "binary")
|
c.Header("Content-Transfer-Encoding", "binary")
|
||||||
c.Header("Content-Disposition", "attachment; filename="+filename)
|
c.Header("Content-Disposition", "attachment; filename="+filename)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"fitra-backend/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func generateID() (string, error) {
|
func generateID() (string, error) {
|
||||||
@@ -99,6 +100,9 @@ func HandleFileUpload(c *gin.Context) {
|
|||||||
baseURL = "http://localhost:8080"
|
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{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "File uploaded successfully",
|
"message": "File uploaded successfully",
|
||||||
"id": fileID,
|
"id": fileID,
|
||||||
|
|||||||
BIN
dist/fitra-darwin-amd64
vendored
BIN
dist/fitra-darwin-amd64
vendored
Binary file not shown.
BIN
dist/fitra-darwin-arm64
vendored
BIN
dist/fitra-darwin-arm64
vendored
Binary file not shown.
BIN
dist/fitra-linux-amd64
vendored
BIN
dist/fitra-linux-amd64
vendored
Binary file not shown.
BIN
dist/fitra-linux-arm64
vendored
BIN
dist/fitra-linux-arm64
vendored
Binary file not shown.
53
utils/analytics.go
Normal file
53
utils/analytics.go
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user