diff --git a/cmd/unified/main.go b/cmd/unified/main.go index d62ff49..aa278a7 100644 --- a/cmd/unified/main.go +++ b/cmd/unified/main.go @@ -203,13 +203,8 @@ func fileRoutes(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store) mux.HandleFunc("/api/v1/items", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - nextStr := r.URL.Query().Get("next") - var next filesvc.ID - if nextStr != "" { - if n, err := strconv.ParseInt(nextStr, 10, 64); err == nil { - next = filesvc.ID(n) - } - } + nextStr := strings.TrimSpace(r.URL.Query().Get("next")) + next := filesvc.ID(nextStr) items, nextOut, err := store.List(r.Context(), next, 100) if err != nil { writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) @@ -275,7 +270,7 @@ func fileRoutes(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store) return } it, err := store.Delete(r.Context(), in.ID) - _ = blobs.Delete(r.Context(), int64(in.ID)) + _ = blobs.Delete(r.Context(), string(in.ID)) if err != nil { status := http.StatusBadRequest if errors.Is(err, filesvc.ErrNotFound) { @@ -316,7 +311,7 @@ func apiFiles(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store, m writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()}) return } - meta, err := blobs.Save(r.Context(), int64(it.ID), name, fh) + meta, err := blobs.Save(r.Context(), string(it.ID), name, fh) if err != nil { _, _ = store.Delete(r.Context(), it.ID) writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) @@ -336,8 +331,8 @@ func apiFiles(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store, m http.NotFound(w, r) return } - id, err := strconv.ParseInt(parts[0], 10, 64) - if err != nil || id <= 0 { + id := parts[0] + if strings.TrimSpace(id) == "" { http.NotFound(w, r) return } @@ -385,7 +380,7 @@ func toMeshSnapshot(s filesvc.Snapshot) mesh.Snapshot { out := mesh.Snapshot{Items: make([]mesh.Item, 0, len(s.Items))} for _, it := range s.Items { out.Items = append(out.Items, mesh.Item{ - ID: int64(it.ID), + ID: it.ID, Name: it.Name, UpdatedAt: it.UpdatedAt, Deleted: it.Deleted, @@ -398,7 +393,7 @@ func fromMeshSnapshot(ms mesh.Snapshot) filesvc.Snapshot { out := filesvc.Snapshot{Items: make([]filesvc.File, 0, len(ms.Items))} for _, it := range ms.Items { out.Items = append(out.Items, filesvc.File{ - ID: filesvc.ID(it.ID), + ID: it.ID, Name: it.Name, UpdatedAt: it.UpdatedAt, Deleted: it.Deleted, @@ -430,7 +425,7 @@ func main() { ApplyRemote: func(ctx context.Context, s mesh.Snapshot) error { return st.ApplyRemote(ctx, fromMeshSnapshot(s)) }, - BlobOpen: func(ctx context.Context, id int64) (io.ReadCloser, string, string, int64, error) { + BlobOpen: func(ctx context.Context, id string) (io.ReadCloser, string, string, int64, error) { //5588 it, err := st.Get(ctx, filesvc.ID(id)) if err != nil || it.Deleted { return nil, "", "", 0, fmt.Errorf("not found") @@ -528,8 +523,8 @@ func registerPublicDownloads(mux *http.ServeMux, store filesvc.MeshStore, blobs http.NotFound(w, r) return } - id, err := strconv.ParseInt(idStr, 10, 64) - if err != nil || id <= 0 { + id := idStr + if strings.TrimSpace(id) == "" { http.NotFound(w, r) return } diff --git a/internal/admin/admin.go b/internal/admin/admin.go index b8460c3..25600da 100644 --- a/internal/admin/admin.go +++ b/internal/admin/admin.go @@ -56,17 +56,11 @@ func Register(mux *http.ServeMux, d Deps) { // Partials mux.HandleFunc("/admin/items", func(w http.ResponseWriter, r *http.Request) { - nextQ := strings.TrimSpace(r.URL.Query().Get("next")) - var nextID filesvc.ID - if nextQ != "" { - if n, err := strconv.ParseInt(nextQ, 10, 64); err == nil { - nextID = filesvc.ID(n) - } - } + nextID := filesvc.ID(strings.TrimSpace(r.URL.Query().Get("next"))) items, nextOut, _ := d.Store.List(r.Context(), nextID, 100) type row struct { - ID int64 + ID string Name string UpdatedAt int64 HasBlob bool @@ -74,9 +68,9 @@ func Register(mux *http.ServeMux, d Deps) { } rows := make([]row, 0, len(items)) for _, it := range items { - meta, ok, _ := d.Blob.Stat(r.Context(), int64(it.ID)) + meta, ok, _ := d.Blob.Stat(r.Context(), it.ID) rows = append(rows, row{ - ID: int64(it.ID), + ID: it.ID, Name: it.Name, UpdatedAt: it.UpdatedAt, HasBlob: ok, @@ -119,7 +113,7 @@ func Register(mux *http.ServeMux, d Deps) { } // 2) Blob speichern - if _, err := d.Blob.Save(r.Context(), int64(it.ID), name, fh); err != nil { + if _, err := d.Blob.Save(r.Context(), (it.ID), name, fh); err != nil { // zurückrollen (Tombstone) _, _ = d.Store.Delete(r.Context(), it.ID) http.Error(w, "save failed: "+err.Error(), http.StatusInternalServerError) @@ -137,8 +131,8 @@ func Register(mux *http.ServeMux, d Deps) { http.NotFound(w, r) return } - id, err := strconv.ParseInt(parts[0], 10, 64) - if err != nil { + id := parts[0] + if strings.TrimSpace(id) == "" { http.NotFound(w, r) return } @@ -208,9 +202,9 @@ func Register(mux *http.ServeMux, d Deps) { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } - idStr := r.FormValue("id") + id := strings.TrimSpace(r.FormValue("id")) newName := strings.TrimSpace(r.FormValue("name")) - if id, err := strconv.ParseInt(idStr, 10, 64); err == nil && newName != "" { + if id != "" && newName != "" { _, _ = d.Store.Rename(r.Context(), filesvc.ID(id), newName) _ = d.Mesh.SyncNow(r.Context()) } @@ -223,10 +217,10 @@ func Register(mux *http.ServeMux, d Deps) { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } - id64, err := strconv.ParseInt(r.FormValue("id"), 10, 64) - if err == nil { - _, _ = d.Store.Delete(r.Context(), filesvc.ID(id64)) - _ = d.Blob.Delete(r.Context(), id64) // Blob wirklich löschen + id := strings.TrimSpace(r.FormValue("id")) + if id != "" { + _, _ = d.Store.Delete(r.Context(), filesvc.ID(id)) + _ = d.Blob.Delete(r.Context(), id) _ = d.Mesh.SyncNow(r.Context()) } renderItemsPartial(w, r, d) @@ -264,27 +258,21 @@ func BasicAuth(user, pass string, next http.Handler) http.Handler { // rebuild & render items partial for HTMX swaps func renderItemsPartial(w http.ResponseWriter, r *http.Request, d Deps) { type row struct { - ID int64 + ID string Name string UpdatedAt int64 HasBlob bool Size int64 } - nextQ := strings.TrimSpace(r.URL.Query().Get("next")) - var nextID filesvc.ID - if nextQ != "" { - if n, err := strconv.ParseInt(nextQ, 10, 64); err == nil { - nextID = filesvc.ID(n) - } - } + nextID := filesvc.ID(strings.TrimSpace(r.URL.Query().Get("next"))) items, nextOut, _ := d.Store.List(r.Context(), nextID, 100) rows := make([]row, 0, len(items)) for _, it := range items { - meta, ok, _ := d.Blob.Stat(r.Context(), int64(it.ID)) + meta, ok, _ := d.Blob.Stat(r.Context(), (it.ID)) rows = append(rows, row{ - ID: int64(it.ID), + ID: (it.ID), Name: it.Name, UpdatedAt: it.UpdatedAt, HasBlob: ok, diff --git a/internal/blobfs/blobfs.go b/internal/blobfs/blobfs.go index ee5bf7e..69747ce 100644 --- a/internal/blobfs/blobfs.go +++ b/internal/blobfs/blobfs.go @@ -6,7 +6,6 @@ import ( "encoding/hex" "encoding/json" "errors" - "fmt" "io" "mime" "net/http" @@ -23,22 +22,35 @@ type Meta struct { } type Store interface { - Save(ctx context.Context, id int64, filename string, r io.Reader) (Meta, error) - Open(ctx context.Context, id int64) (io.ReadSeekCloser, Meta, error) - Stat(ctx context.Context, id int64) (Meta, bool, error) - Delete(ctx context.Context, id int64) error + Save(ctx context.Context, id string, filename string, r io.Reader) (Meta, error) + Open(ctx context.Context, id string) (io.ReadSeekCloser, Meta, error) + Stat(ctx context.Context, id string) (Meta, bool, error) + Delete(ctx context.Context, id string) error } type FS struct{ root string } func New(root string) *FS { return &FS{root: root} } -func (fs *FS) dir(id int64) string { return filepath.Join(fs.root, "files", fmt.Sprintf("%d", id)) } -func (fs *FS) metaPath(id int64) string { return filepath.Join(fs.dir(id), "meta.json") } -func (fs *FS) blobPath(id int64, name string) string { +func (fs *FS) dir(id string) string { return filepath.Join(fs.root, "files", sanitizeID(id)) } +func (fs *FS) metaPath(id string) string { return filepath.Join(fs.dir(id), "meta.json") } +func (fs *FS) blobPath(id string, name string) string { return filepath.Join(fs.dir(id), "blob"+safeExt(name)) } +func sanitizeID(id string) string { + // nur 0-9a-zA-Z- zulassen; Rest mit '_' ersetzen + b := make([]rune, 0, len(id)) + for _, r := range id { + if (r >= '0' && r <= '9') || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == '-' { + b = append(b, r) + } else { + b = append(b, '_') + } + } + return string(b) +} + func safeExt(name string) string { ext := filepath.Ext(name) if len(ext) > 16 { // unrealistisch lange Exts beschneiden @@ -47,7 +59,7 @@ func safeExt(name string) string { return ext } -func (fs *FS) Save(_ context.Context, id int64, filename string, r io.Reader) (Meta, error) { +func (fs *FS) Save(_ context.Context, id string, filename string, r io.Reader) (Meta, error) { if strings.TrimSpace(filename) == "" { return Meta{}, errors.New("filename required") } @@ -107,7 +119,7 @@ func (fs *FS) Save(_ context.Context, id int64, filename string, r io.Reader) (M return meta, nil } -func (fs *FS) Open(_ context.Context, id int64) (io.ReadSeekCloser, Meta, error) { +func (fs *FS) Open(_ context.Context, id string) (io.ReadSeekCloser, Meta, error) { meta, ok, err := fs.Stat(context.Background(), id) if err != nil { return nil, Meta{}, err @@ -119,7 +131,7 @@ func (fs *FS) Open(_ context.Context, id int64) (io.ReadSeekCloser, Meta, error) return f, meta, err } -func (fs *FS) Stat(_ context.Context, id int64) (Meta, bool, error) { +func (fs *FS) Stat(_ context.Context, id string) (Meta, bool, error) { b, err := os.ReadFile(fs.metaPath(id)) if err != nil { if os.IsNotExist(err) { @@ -139,7 +151,7 @@ func (fs *FS) Stat(_ context.Context, id int64) (Meta, bool, error) { return m, true, nil } -func (fs *FS) Delete(_ context.Context, id int64) error { +func (fs *FS) Delete(_ context.Context, id string) error { return os.RemoveAll(fs.dir(id)) } diff --git a/internal/filesvc/memstore.go b/internal/filesvc/memstore.go index 5aad781..2e0d40e 100644 --- a/internal/filesvc/memstore.go +++ b/internal/filesvc/memstore.go @@ -11,7 +11,6 @@ import ( type MemStore struct { mu sync.Mutex items map[ID]File - next ID // optionales Eventing subs []chan ChangeEvent @@ -20,7 +19,6 @@ type MemStore struct { func NewMemStore() *MemStore { return &MemStore{ items: make(map[ID]File), - next: 1, } } @@ -39,39 +37,54 @@ func (m *MemStore) Get(_ context.Context, id ID) (File, error) { func (m *MemStore) List(_ context.Context, next ID, limit int) ([]File, ID, error) { m.mu.Lock() defer m.mu.Unlock() + if limit <= 0 || limit > 1000 { limit = 100 } - ids := make([]ID, 0, len(m.items)) - for id := range m.items { - ids = append(ids, id) + + // sortiere deterministisch nach UpdatedAt, dann ID + all := make([]File, 0, len(m.items)) + for _, v := range m.items { + all = append(all, v) } - slices.Sort(ids) + slices.SortFunc(all, func(a, b File) int { + if a.UpdatedAt == b.UpdatedAt { + if a.ID == b.ID { + return 0 + } + if a.ID < b.ID { + return -1 + } + return 1 + } + if a.UpdatedAt < b.UpdatedAt { + return -1 + } + return 1 + }) start := 0 - if next > 0 { - for i, id := range ids { - if id >= next { + if next != "" { + for i, it := range all { + if it.ID >= next { start = i break } } } end := start + limit - if end > len(ids) { - end = len(ids) + if end > len(all) { + end = len(all) } - out := make([]File, 0, end-start) - for _, id := range ids[start:end] { - it := m.items[id] + for _, it := range all[start:end] { if !it.Deleted { out = append(out, it) } } var nextOut ID - if end < len(ids) { - nextOut = ids[end] + if end < len(all) { + nextOut = all[end].ID } return out, nextOut, nil } @@ -85,9 +98,12 @@ func (m *MemStore) Create(_ context.Context, name string) (File, error) { m.mu.Lock() defer m.mu.Unlock() now := time.Now().UnixNano() - it := File{ID: m.next, Name: name, UpdatedAt: now} + uid, err := NewUUIDv4() + if err != nil { + return File{}, err + } + it := File{ID: uid, Name: name, UpdatedAt: now} m.items[it.ID] = it - m.next++ m.emit(it) return it, nil } @@ -147,9 +163,6 @@ func (m *MemStore) ApplyRemote(_ context.Context, s Snapshot) error { li, ok := m.items[ri.ID] if !ok || ri.UpdatedAt > li.UpdatedAt { m.items[ri.ID] = ri - if ri.ID >= m.next { - m.next = ri.ID + 1 - } m.emitLocked(ri) } } diff --git a/internal/filesvc/store.go b/internal/filesvc/store.go index c55afa1..6eeb5c3 100644 --- a/internal/filesvc/store.go +++ b/internal/filesvc/store.go @@ -2,13 +2,15 @@ package filesvc import ( "context" + "crypto/rand" "errors" + "fmt" "time" ) /*** Domain ***/ -type ID = int64 +type ID = string type File struct { ID ID `json:"id"` @@ -76,3 +78,13 @@ type MeshStore interface { Replicable Watchable // optional – kann Noop sein } + +func NewUUIDv4() (string, error) { + b := make([]byte, 16) + if _, err := rand.Read(b); err != nil { + return "", err + } + b[6] = (b[6] & 0x0f) | 0x40 // Version 4 + b[8] = (b[8] & 0x3f) | 0x80 // Variant RFC4122 + return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]), nil +} diff --git a/internal/mesh/mesh.go b/internal/mesh/mesh.go index a798c61..2977b16 100644 --- a/internal/mesh/mesh.go +++ b/internal/mesh/mesh.go @@ -41,7 +41,7 @@ type Peer struct { } type Item struct { - ID int64 `json:"id"` + ID string `json:"id"` Name string `json:"name"` UpdatedAt int64 `json:"updatedAt"` Deleted bool `json:"deleted"` // <— NEU: Tombstone für Deletes @@ -55,7 +55,7 @@ type Snapshot struct { type Callbacks struct { GetSnapshot func(ctx context.Context) (Snapshot, error) ApplyRemote func(ctx context.Context, s Snapshot) error - BlobOpen func(ctx context.Context, id int64) (io.ReadCloser, string, string, int64, error) + BlobOpen func(ctx context.Context, id string) (io.ReadCloser, string, string, int64, error) } /*** Node ***/ @@ -447,11 +447,11 @@ func (n *Node) SyncNow(ctx context.Context) error { /*** Utilities ***/ // OwnerHint is a simple, optional mapping to distribute responsibility. -func OwnerHint(id int64, peers []string) int { +func OwnerHint(id string, peers []string) int { if len(peers) == 0 { return 0 } - h := crc32.ChecksumIEEE([]byte(string(rune(id)))) + h := crc32.ChecksumIEEE([]byte(id)) return int(h % uint32(len(peers))) } @@ -502,7 +502,7 @@ func (n *Node) blobHandler(w http.ResponseWriter, r *http.Request) { return } var req struct { - ID int64 `json:"id"` + ID string `json:"id"` } if err := json.Unmarshal(body, &req); err != nil { http.Error(w, "bad json", http.StatusBadRequest) @@ -530,9 +530,9 @@ func (n *Node) blobHandler(w http.ResponseWriter, r *http.Request) { } // interner Helper: signierter Blob-Request an einen Peer -func (n *Node) sendBlobRequest(url string, id int64) (*http.Response, error) { +func (n *Node) sendBlobRequest(url string, id string) (*http.Response, error) { b, _ := json.Marshal(struct { - ID int64 `json:"id"` + ID string `json:"id"` }{ID: id}) req, _ := http.NewRequest(http.MethodPost, strings.TrimRight(url, "/")+"/mesh/blob", bytes.NewReader(b)) req.Header.Set("X-Mesh-Sig", n.sign(b)) @@ -540,7 +540,7 @@ func (n *Node) sendBlobRequest(url string, id int64) (*http.Response, error) { } // Öffentliche Methode: versuche Blob bei irgendeinem Peer zu holen -func (n *Node) FetchBlobAny(ctx context.Context, id int64) (io.ReadCloser, string, string, int64, error) { +func (n *Node) FetchBlobAny(ctx context.Context, id string) (io.ReadCloser, string, string, int64, error) { n.mu.RLock() targets := make([]string, 0, len(n.peers)) for url := range n.peers { @@ -574,5 +574,5 @@ func (n *Node) FetchBlobAny(ctx context.Context, id int64) (io.ReadCloser, strin io.Copy(io.Discard, resp.Body) resp.Body.Close() } - return nil, "", "", 0, fmt.Errorf("blob %d not found on peers", id) + return nil, "", "", 0, fmt.Errorf("blob %s not found on peers", id) }