use antigravity to rewrite all your code to migrate from flat files to relational database. YOLO
All checks were successful
pedestrian-simulator / build (push) Successful in 58s
All checks were successful
pedestrian-simulator / build (push) Successful in 58s
This commit is contained in:
351
server/kml.go
351
server/kml.go
@@ -10,9 +10,8 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -45,95 +44,26 @@ type KMLMetadata struct {
|
||||
Votes int `json:"votes"` // Net votes (calculated from voting system)
|
||||
}
|
||||
|
||||
// Global vote tracking: kmlID -> userID -> vote (+1, -1, or 0)
|
||||
type VoteRegistry struct {
|
||||
Votes map[string]map[string]int `json:"votes"` // kmlID -> (userID -> vote)
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
var voteRegistry *VoteRegistry
|
||||
|
||||
// InitVoteRegistry loads the vote registry from disk
|
||||
func InitVoteRegistry() {
|
||||
voteRegistry = &VoteRegistry{
|
||||
Votes: make(map[string]map[string]int),
|
||||
}
|
||||
voteRegistry.Load()
|
||||
}
|
||||
|
||||
func (vr *VoteRegistry) Load() error {
|
||||
vr.mu.Lock()
|
||||
defer vr.mu.Unlock()
|
||||
|
||||
data, err := os.ReadFile("data/kml_votes.json")
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(data, vr)
|
||||
}
|
||||
|
||||
func (vr *VoteRegistry) Save() error {
|
||||
vr.mu.RLock()
|
||||
defer vr.mu.RUnlock()
|
||||
return vr.saveUnlocked()
|
||||
}
|
||||
|
||||
func (vr *VoteRegistry) saveUnlocked() error {
|
||||
if err := os.MkdirAll("data", 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(vr, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile("data/kml_votes.json", data, 0644)
|
||||
}
|
||||
|
||||
// GetVote return the vote of a user for a KML file (-1, 0, +1)
|
||||
func (vr *VoteRegistry) GetVote(kmlID, userID string) int {
|
||||
vr.mu.RLock()
|
||||
defer vr.mu.RUnlock()
|
||||
|
||||
if userVotes, exists := vr.Votes[kmlID]; exists {
|
||||
return userVotes[userID]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// SetVote sets a user's vote for a KML file
|
||||
func (vr *VoteRegistry) SetVote(kmlID, userID string, vote int) error {
|
||||
vr.mu.Lock()
|
||||
defer vr.mu.Unlock()
|
||||
|
||||
if vr.Votes[kmlID] == nil {
|
||||
vr.Votes[kmlID] = make(map[string]int)
|
||||
}
|
||||
|
||||
// SetVote sets a user's vote for a KML file in the database
|
||||
func SetVote(kmlID int, userID string, vote int) error {
|
||||
if vote == 0 {
|
||||
delete(vr.Votes[kmlID], userID)
|
||||
} else {
|
||||
vr.Votes[kmlID][userID] = vote
|
||||
_, err := db.Exec("DELETE FROM kml_votes WHERE kml_id = ? AND user_id = ?", kmlID, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
return vr.saveUnlocked()
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO kml_votes (kml_id, user_id, vote_value)
|
||||
VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE vote_value = ?
|
||||
`, kmlID, userID, vote, vote)
|
||||
return err
|
||||
}
|
||||
|
||||
// CalculateNetVotes calculates net votes for a KML file
|
||||
func (vr *VoteRegistry) CalculateNetVotes(kmlID string) int {
|
||||
vr.mu.RLock()
|
||||
defer vr.mu.RUnlock()
|
||||
|
||||
total := 0
|
||||
if userVotes, exists := vr.Votes[kmlID]; exists {
|
||||
for _, vote := range userVotes {
|
||||
total += vote
|
||||
}
|
||||
// CalculateNetVotes calculates net votes for a KML file from the database
|
||||
func CalculateNetVotes(kmlID int) int {
|
||||
var total int
|
||||
err := db.QueryRow("SELECT COALESCE(SUM(vote_value), 0) FROM kml_votes WHERE kml_id = ?", kmlID).Scan(&total)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return total
|
||||
}
|
||||
@@ -265,28 +195,14 @@ func HandleKMLUpload(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get user info
|
||||
user, _ := userRegistry.GetUser(userID)
|
||||
displayName := userID
|
||||
if user != nil {
|
||||
displayName = user.DisplayName
|
||||
}
|
||||
|
||||
// Create metadata
|
||||
metadata := KMLMetadata{
|
||||
Filename: handler.Filename,
|
||||
UserID: userID,
|
||||
DisplayName: displayName,
|
||||
Distance: distance,
|
||||
IsPublic: false, // Private by default
|
||||
UploadedAt: time.Now(),
|
||||
Votes: 0,
|
||||
}
|
||||
|
||||
metaPath := filepath.Join(kmlDir, handler.Filename+".meta.json")
|
||||
metaData, _ := json.MarshalIndent(metadata, "", " ")
|
||||
if err := os.WriteFile(metaPath, metaData, 0644); err != nil {
|
||||
http.Error(w, "Failed to save metadata", http.StatusInternalServerError)
|
||||
// Save metadata to database
|
||||
_, err = db.Exec(`
|
||||
INSERT INTO kml_metadata (filename, user_id, distance, is_public, uploaded_at)
|
||||
VALUES (?, ?, ?, ?, NOW())
|
||||
ON DUPLICATE KEY UPDATE distance = ?, uploaded_at = NOW()
|
||||
`, handler.Filename, userID, distance, false, distance)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to save metadata: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -298,7 +214,7 @@ func HandleKMLUpload(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// HandleKMLList lists KML files (user's own + public files)
|
||||
// HandleKMLList lists KML files with pagination and sorting
|
||||
func HandleKMLList(w http.ResponseWriter, r *http.Request) {
|
||||
userID, ok := getUserID(r.Context())
|
||||
if !ok {
|
||||
@@ -306,89 +222,88 @@ func HandleKMLList(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var allFiles []KMLMetadata
|
||||
// Parse pagination parameters
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
offset := (page - 1) * limit
|
||||
|
||||
// Walk through all user directories
|
||||
usersDir := "data/users"
|
||||
entries, err := os.ReadDir(usersDir)
|
||||
sortBy := r.URL.Query().Get("sort_by")
|
||||
order := r.URL.Query().Get("order")
|
||||
if order != "ASC" {
|
||||
order = "DESC"
|
||||
}
|
||||
|
||||
// 1. Get my files
|
||||
myFiles, err := queryKMLMetadata(`
|
||||
SELECT m.filename, m.user_id, u.display_name, m.distance, m.is_public, m.uploaded_at,
|
||||
(SELECT COALESCE(SUM(v.vote_value), 0) FROM kml_votes v WHERE v.kml_id = m.id) as votes
|
||||
FROM kml_metadata m
|
||||
JOIN users u ON m.user_id = u.fitbit_user_id
|
||||
WHERE m.user_id = ?
|
||||
ORDER BY m.uploaded_at DESC
|
||||
`, userID)
|
||||
if err != nil {
|
||||
// No users yet
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"my_files": []KMLMetadata{},
|
||||
"public_files": []KMLMetadata{},
|
||||
})
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
ownerID := entry.Name()
|
||||
kmlDir := filepath.Join(usersDir, ownerID, "kml")
|
||||
|
||||
kmlFiles, err := os.ReadDir(kmlDir)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, kmlFile := range kmlFiles {
|
||||
if !strings.HasSuffix(kmlFile.Name(), ".meta.json") {
|
||||
continue
|
||||
}
|
||||
|
||||
metaPath := filepath.Join(kmlDir, kmlFile.Name())
|
||||
data, err := os.ReadFile(metaPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var meta KMLMetadata
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate current votes
|
||||
kmlID := fmt.Sprintf("%s/%s", ownerID, meta.Filename)
|
||||
meta.Votes = voteRegistry.CalculateNetVotes(kmlID)
|
||||
|
||||
// Include if: 1) owned by current user, OR 2) public
|
||||
if ownerID == userID || meta.IsPublic {
|
||||
allFiles = append(allFiles, meta)
|
||||
}
|
||||
}
|
||||
// 2. Get public files (with pagination and sorting)
|
||||
sortClause := "votes"
|
||||
switch sortBy {
|
||||
case "date":
|
||||
sortClause = "m.uploaded_at"
|
||||
case "distance":
|
||||
sortClause = "m.distance"
|
||||
}
|
||||
|
||||
// Separate into own files and public files
|
||||
var myFiles, publicFiles []KMLMetadata
|
||||
for _, file := range allFiles {
|
||||
if file.UserID == userID {
|
||||
myFiles = append(myFiles, file)
|
||||
}
|
||||
if file.IsPublic {
|
||||
publicFiles = append(publicFiles, file)
|
||||
}
|
||||
publicFiles, err := queryKMLMetadata(fmt.Sprintf(`
|
||||
SELECT m.filename, m.user_id, u.display_name, m.distance, m.is_public, m.uploaded_at,
|
||||
(SELECT COALESCE(SUM(v.vote_value), 0) FROM kml_votes v WHERE v.kml_id = m.id) as votes
|
||||
FROM kml_metadata m
|
||||
JOIN users u ON m.user_id = u.fitbit_user_id
|
||||
WHERE m.is_public = 1
|
||||
ORDER BY %s %s
|
||||
LIMIT ? OFFSET ?
|
||||
`, sortClause, order), limit, offset)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Sort public files by votes (highest first)
|
||||
sort.Slice(publicFiles, func(i, j int) bool {
|
||||
return publicFiles[i].Votes > publicFiles[j].Votes
|
||||
})
|
||||
|
||||
// Sort my files by upload date (newest first)
|
||||
sort.Slice(myFiles, func(i, j int) bool {
|
||||
return myFiles[i].UploadedAt.After(myFiles[j].UploadedAt)
|
||||
})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"my_files": myFiles,
|
||||
"public_files": publicFiles,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
})
|
||||
}
|
||||
|
||||
func queryKMLMetadata(query string, args ...interface{}) ([]KMLMetadata, error) {
|
||||
rows, err := db.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var files []KMLMetadata
|
||||
for rows.Next() {
|
||||
var m KMLMetadata
|
||||
err := rows.Scan(&m.Filename, &m.UserID, &m.DisplayName, &m.Distance, &m.IsPublic, &m.UploadedAt, &m.Votes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files = append(files, m)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// HandleKMLPrivacyToggle toggles the privacy setting of a KML file
|
||||
func HandleKMLPrivacyToggle(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
@@ -410,39 +325,22 @@ func HandleKMLPrivacyToggle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
metaPath := fmt.Sprintf("data/users/%s/kml/%s.meta.json", userID, req.Filename)
|
||||
data, err := os.ReadFile(metaPath)
|
||||
if err != nil {
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var meta KMLMetadata
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
http.Error(w, "Failed to parse metadata", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify ownership
|
||||
if meta.UserID != userID {
|
||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
// Toggle privacy
|
||||
meta.IsPublic = !meta.IsPublic
|
||||
|
||||
// Save updated metadata
|
||||
newData, _ := json.MarshalIndent(meta, "", " ")
|
||||
if err := os.WriteFile(metaPath, newData, 0644); err != nil {
|
||||
// Toggle privacy in database
|
||||
if _, err := db.Exec("UPDATE kml_metadata SET is_public = NOT is_public WHERE filename = ? AND user_id = ?", req.Filename, userID); err != nil {
|
||||
http.Error(w, "Failed to update metadata", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var isPublic bool
|
||||
if err := db.QueryRow("SELECT is_public FROM kml_metadata WHERE filename = ? AND user_id = ?", req.Filename, userID).Scan(&isPublic); err != nil {
|
||||
http.Error(w, "Failed to fetch updated metadata", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"success": true,
|
||||
"is_public": meta.IsPublic,
|
||||
"is_public": isPublic,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -475,16 +373,22 @@ func HandleKMLVote(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
kmlID := fmt.Sprintf("%s/%s", req.OwnerID, req.Filename)
|
||||
// Get KML ID
|
||||
var kmlID int
|
||||
err := db.QueryRow("SELECT id FROM kml_metadata WHERE user_id = ? AND filename = ?", req.OwnerID, req.Filename).Scan(&kmlID)
|
||||
if err != nil {
|
||||
http.Error(w, "KML not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the vote
|
||||
if err := voteRegistry.SetVote(kmlID, userID, req.Vote); err != nil {
|
||||
if err := SetVote(kmlID, userID, req.Vote); err != nil {
|
||||
http.Error(w, "Failed to save vote", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Calculate new net votes
|
||||
netVotes := voteRegistry.CalculateNetVotes(kmlID)
|
||||
netVotes := CalculateNetVotes(kmlID)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
@@ -514,30 +418,15 @@ func HandleKMLDelete(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify ownership by reading metadata
|
||||
metaPath := fmt.Sprintf("data/users/%s/kml/%s.meta.json", userID, req.Filename)
|
||||
data, err := os.ReadFile(metaPath)
|
||||
if err != nil {
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
// Verify ownership and delete metadata from database
|
||||
if _, err := db.Exec("DELETE FROM kml_metadata WHERE filename = ? AND user_id = ?", req.Filename, userID); err != nil {
|
||||
http.Error(w, "Failed to delete metadata or file not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var meta KMLMetadata
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
http.Error(w, "Failed to parse metadata", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify ownership
|
||||
if meta.UserID != userID {
|
||||
http.Error(w, "Forbidden - you can only delete your own files", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete KML file and metadata
|
||||
// Delete KML file
|
||||
kmlPath := fmt.Sprintf("data/users/%s/kml/%s", userID, req.Filename)
|
||||
os.Remove(kmlPath)
|
||||
os.Remove(metaPath)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
@@ -563,20 +452,14 @@ func HandleKMLDownload(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Verify permission: ownerID == userID OR file is public
|
||||
if ownerID != userID {
|
||||
metaPath := fmt.Sprintf("data/users/%s/kml/%s.meta.json", ownerID, filename)
|
||||
data, err := os.ReadFile(metaPath)
|
||||
var isPublic bool
|
||||
err := db.QueryRow("SELECT is_public FROM kml_metadata WHERE user_id = ? AND filename = ?", ownerID, filename).Scan(&isPublic)
|
||||
if err != nil {
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var meta KMLMetadata
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
http.Error(w, "Error reading metadata", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if !meta.IsPublic {
|
||||
if !isPublic {
|
||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user