Files
filebrowser/backend/internal/web/analytics.go
T
+10 3c02eedca6 Beta/v2.0.0 (#2746)
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: Gabriel Górski <me@glaeqen.eu>
Co-authored-by: Joel Godfrey <82655889+BaccanoMob@users.noreply.github.com>
Co-authored-by: Shane Chiu <71911511+pwn2ooown@users.noreply.github.com>
Co-authored-by: carloda <35763669+carloda@users.noreply.github.com>
Co-authored-by: Taufik Hidayat <taufikdev88@gmail.com>
Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
Co-authored-by: SIDDHU SINGH <siddhu3116@gmail.com>
Co-authored-by: Gaurav Kalal <117557391+Gorupa@users.noreply.github.com>
Co-authored-by: Nebula <40148908+nebula-it@users.noreply.github.com>
Co-authored-by: Juan Soler <juansoler@gmail.com>
Co-authored-by: codacy-production[bot] <61871480+codacy-production[bot]@users.noreply.github.com>
Co-authored-by: Anatolii Prylutskyi <anpryl@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Graham Steffaniak <graham@debian-lake.ghome.net>
Co-authored-by: Neil <kylelzzz02@icloud.com>
Co-authored-by: Graham <info@quantumx-apps.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 16:24:38 -05:00

72 lines
2.2 KiB
Go

package web
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gtsteffaniak/filebrowser/backend/internal/analytics"
"github.com/gtsteffaniak/filebrowser/backend/internal/state"
"github.com/gtsteffaniak/go-logger/logger"
)
type analyticsStatusResponse struct {
Enabled bool `json:"enabled"`
Ready bool `json:"ready"`
PublishSupported bool `json:"publishSupported"`
}
type analyticsUpdateRequest struct {
Enabled bool `json:"enabled"`
}
func settingsAnalyticsGetHandler(w http.ResponseWriter, r *http.Request, d *Context) (int, error) {
return RenderJSON(w, r, analyticsStatusResponse{
Enabled: state.IsAnalyticsEnabled(),
Ready: analytics.Ready(),
PublishSupported: analytics.PublishSupported(),
})
}
func settingsAnalyticsUpdateHandler(w http.ResponseWriter, r *http.Request, d *Context) (int, error) {
var body analyticsUpdateRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
return http.StatusBadRequest, fmt.Errorf("failed to decode body: %w", err)
}
defer r.Body.Close()
if body.Enabled && !analytics.PublishSupported() {
return http.StatusForbidden, fmt.Errorf("deployment analytics publishing is not available in this build")
}
if err := state.SetAnalyticsEnabled(body.Enabled); err != nil {
logger.Errorf("failed to update analytics setting: %v", err)
return http.StatusInternalServerError, fmt.Errorf("failed to update analytics setting")
}
if !body.Enabled {
analytics.NotifyAnalyticsDisabled()
analytics.InvalidateCache()
} else {
analytics.NotifyAnalyticsEnabled()
}
return RenderJSON(w, r, analyticsStatusResponse{
Enabled: state.IsAnalyticsEnabled(),
Ready: analytics.Ready(),
PublishSupported: analytics.PublishSupported(),
})
}
func settingsAnalyticsPreviewHandler(w http.ResponseWriter, r *http.Request, d *Context) (int, error) {
preview, err := analytics.PreviewSnapshot()
if err != nil {
return http.StatusServiceUnavailable, fmt.Errorf("failed to build analytics preview: %w", err)
}
if !json.Valid(preview) {
return http.StatusInternalServerError, fmt.Errorf("failed to format analytics preview")
}
return RenderJSON(w, r, json.RawMessage(preview))
}