forked from upstream/filebrowser
+10








3c02eedca6
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>
89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package activity
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
activitydb "github.com/gtsteffaniak/filebrowser/backend/internal/database/activity"
|
|
)
|
|
|
|
var activityOptionalExportColumns = map[string]struct{}{
|
|
"source": {},
|
|
"path": {},
|
|
"shareHash": {},
|
|
"tokenName": {},
|
|
}
|
|
|
|
func ParseExportRows(raw string) ([]string, error) {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return nil, nil
|
|
}
|
|
parts := strings.Split(raw, ",")
|
|
out := make([]string, 0, len(parts))
|
|
seen := make(map[string]struct{}, len(parts))
|
|
for _, part := range parts {
|
|
key := strings.TrimSpace(part)
|
|
if key == "" {
|
|
continue
|
|
}
|
|
if _, ok := activityOptionalExportColumns[key]; !ok {
|
|
return nil, fmt.Errorf("invalid rows column: %s (allowed: source, path, shareHash, tokenName)", key)
|
|
}
|
|
if _, dup := seen[key]; dup {
|
|
continue
|
|
}
|
|
seen[key] = struct{}{}
|
|
out = append(out, key)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func ExportHeader(includeDetails bool, optional []string) []string {
|
|
header := []string{"id", "createdAt", "username", "eventType"}
|
|
header = append(header, optional...)
|
|
header = append(header, "ipAddress")
|
|
if includeDetails {
|
|
header = append(header, "details")
|
|
}
|
|
return header
|
|
}
|
|
|
|
func SanitizeCSVCell(v string) string {
|
|
if v == "" {
|
|
return v
|
|
}
|
|
switch v[0] {
|
|
case '=', '+', '-', '@':
|
|
return "'" + v
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
func ExportRowValues(item activitydb.FrontendEntry, optional []string, includeDetails bool, detailsJSON string) []string {
|
|
row := []string{
|
|
fmt.Sprintf("%d", item.ID),
|
|
fmt.Sprintf("%d", item.CreatedAt),
|
|
SanitizeCSVCell(item.Username),
|
|
string(item.EventType),
|
|
}
|
|
for _, col := range optional {
|
|
switch col {
|
|
case "source":
|
|
row = append(row, SanitizeCSVCell(item.Source))
|
|
case "path":
|
|
row = append(row, SanitizeCSVCell(item.Path))
|
|
case "shareHash":
|
|
row = append(row, SanitizeCSVCell(item.ShareHash))
|
|
case "tokenName":
|
|
row = append(row, SanitizeCSVCell(item.TokenName))
|
|
}
|
|
}
|
|
row = append(row, SanitizeCSVCell(item.IPAddress))
|
|
if includeDetails {
|
|
row = append(row, SanitizeCSVCell(detailsJSON))
|
|
}
|
|
return row
|
|
}
|