vor temaplte
This commit is contained in:
@@ -257,6 +257,10 @@ func fileRoutes(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store)
|
||||
}
|
||||
it, err := store.Rename(r.Context(), in.ID, in.Name)
|
||||
if err != nil {
|
||||
if errors.Is(err, filesvc.ErrForbidden) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "only owner may modify"})
|
||||
return
|
||||
}
|
||||
status := http.StatusBadRequest
|
||||
if errors.Is(err, filesvc.ErrNotFound) {
|
||||
status = http.StatusNotFound
|
||||
@@ -283,6 +287,10 @@ func fileRoutes(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store)
|
||||
it, err := store.Delete(r.Context(), in.ID)
|
||||
_ = blobs.Delete(r.Context(), string(in.ID))
|
||||
if err != nil {
|
||||
if errors.Is(err, filesvc.ErrForbidden) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "only owner may modify"})
|
||||
return
|
||||
}
|
||||
status := http.StatusBadRequest
|
||||
if errors.Is(err, filesvc.ErrNotFound) {
|
||||
status = http.StatusNotFound
|
||||
@@ -363,13 +371,23 @@ func apiFiles(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store, m
|
||||
}
|
||||
|
||||
// 3) remote holen & cachen
|
||||
it1, _ := store.Get(r.Context(), filesvc.ID(id))
|
||||
peers := meshNode.PeerList()
|
||||
ttl := 2 * time.Minute
|
||||
if cfg := meshNode.Config(); cfg.PeerTTL > 0 {
|
||||
ttl = cfg.PeerTTL
|
||||
}
|
||||
if !isOwnerActive(it1.Owner, peers, ttl) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
rrc, _, _, _, err := meshNode.FetchBlobAny(r.Context(), id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
defer rrc.Close()
|
||||
if _, err := blobs.Save(r.Context(), id, it.Name, rrc); err != nil {
|
||||
if _, err := blobs.Save(r.Context(), id, it1.Name, rrc); err != nil {
|
||||
http.Error(w, "cache failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -381,7 +399,7 @@ func apiFiles(mux *http.ServeMux, store filesvc.MeshStore, blobs blobfs.Store, m
|
||||
return
|
||||
}
|
||||
defer lrc.Close()
|
||||
serveBlob(w, r, lrc, meta, it.Name)
|
||||
serveBlob(w, r, lrc, meta, it1.Name)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -395,6 +413,7 @@ func toMeshSnapshot(s filesvc.Snapshot) mesh.Snapshot {
|
||||
Name: it.Name,
|
||||
UpdatedAt: it.UpdatedAt,
|
||||
Deleted: it.Deleted,
|
||||
Owner: it.Owner,
|
||||
})
|
||||
}
|
||||
return out
|
||||
@@ -408,18 +427,45 @@ func fromMeshSnapshot(ms mesh.Snapshot) filesvc.Snapshot {
|
||||
Name: it.Name,
|
||||
UpdatedAt: it.UpdatedAt,
|
||||
Deleted: it.Deleted,
|
||||
Owner: it.Owner,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// isOwnerActive prüft, ob der Owner in der Peer-Liste als "aktiv" gilt.
|
||||
func isOwnerActive(owner string, peers []mesh.Peer, ttl time.Duration) bool {
|
||||
owner = strings.TrimSpace(owner)
|
||||
if owner == "" {
|
||||
return true
|
||||
}
|
||||
cutoff := time.Now().Add(-ttl)
|
||||
for _, p := range peers {
|
||||
if strings.TrimSpace(p.URL) != owner {
|
||||
continue
|
||||
}
|
||||
// Self ist per Definition aktiv
|
||||
if p.Self {
|
||||
return true
|
||||
}
|
||||
// ohne LastSeen: als inaktiv behandeln
|
||||
if p.LastSeen.IsZero() {
|
||||
return false
|
||||
}
|
||||
return p.LastSeen.After(cutoff)
|
||||
}
|
||||
// Owner ist nicht mal in der Liste: inaktiv
|
||||
return false
|
||||
}
|
||||
|
||||
/*** main ***/
|
||||
|
||||
func main() {
|
||||
cfg := loadConfig()
|
||||
|
||||
// Domain-Store (mesh-fähig)
|
||||
st := filesvc.NewMemStore()
|
||||
nodeID := strings.TrimSpace(cfg.Mesh.AdvertURL)
|
||||
st := filesvc.NewMemStore(nodeID)
|
||||
|
||||
// Mesh starten
|
||||
//mcfg := mesh.FromEnv()
|
||||
@@ -463,6 +509,47 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(1 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
// aktive Owner bestimmen
|
||||
peers := mnode.PeerList()
|
||||
ttl := 2 * time.Minute
|
||||
if cfg := mnode.Config(); cfg.PeerTTL > 0 {
|
||||
ttl = cfg.PeerTTL
|
||||
}
|
||||
cutoff := time.Now().Add(-ttl)
|
||||
|
||||
active := map[string]bool{}
|
||||
for _, p := range peers {
|
||||
if p.Self {
|
||||
active[p.URL] = true
|
||||
continue
|
||||
}
|
||||
if !p.LastSeen.IsZero() && p.LastSeen.After(cutoff) {
|
||||
active[p.URL] = true
|
||||
}
|
||||
}
|
||||
|
||||
// alle Items durchgehen; Blobs von Offline-Ownern löschen
|
||||
var next filesvc.ID
|
||||
for {
|
||||
items, nextOut, _ := st.List(context.Background(), next, 1000)
|
||||
for _, it := range items {
|
||||
if it.Owner == "" || active[it.Owner] {
|
||||
continue
|
||||
}
|
||||
_ = blobs.Delete(context.Background(), it.ID)
|
||||
}
|
||||
if nextOut == "" {
|
||||
break
|
||||
}
|
||||
next = nextOut
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Root-Mux
|
||||
root := http.NewServeMux()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user