- feat(library): walk each root, plan the changes, read tags with N parallel workers and write from a single goroutine - feat(library): detect renames by size and mtime only when the match is unambiguous; mark vanished files missing instead of deleting them - fix-safe: an unreadable root, an unreadable subdirectory or an interrupted scan never marks files as missing; symlinks are skipped - feat(library): event hub that fans scan progress out to subscribers without ever blocking the scan (ready for a websocket client) - feat(library): TagLib (WASM, no CGO) reader for MP3 and FLAC tags and audio properties, with title and artist fallbacks - feat(storage): library repository for SQLite and PostgreSQL - feat(api): POST/GET /api/v1/library/scan to start a scan and read its status - feat(config): library.scan_workers
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package library
|
|
|
|
import "sync"
|
|
|
|
// Hub fans scan events out to any number of subscribers, for example one
|
|
// websocket connection each. Publishing never blocks: a subscriber whose
|
|
// buffer is full simply misses events.
|
|
type Hub struct {
|
|
mu sync.Mutex
|
|
next int
|
|
subs map[int]chan Event
|
|
}
|
|
|
|
// NewHub creates an empty hub.
|
|
func NewHub() *Hub { return &Hub{subs: map[int]chan Event{}} }
|
|
|
|
// Subscribe returns a channel of events and a function that stops the
|
|
// subscription and closes the channel. The function is safe to call twice.
|
|
func (h *Hub) Subscribe(buffer int) (<-chan Event, func()) {
|
|
if buffer < 1 {
|
|
buffer = 1
|
|
}
|
|
ch := make(chan Event, buffer)
|
|
|
|
h.mu.Lock()
|
|
id := h.next
|
|
h.next++
|
|
h.subs[id] = ch
|
|
h.mu.Unlock()
|
|
|
|
var once sync.Once
|
|
cancel := func() {
|
|
once.Do(func() {
|
|
h.mu.Lock()
|
|
delete(h.subs, id)
|
|
close(ch)
|
|
h.mu.Unlock()
|
|
})
|
|
}
|
|
return ch, cancel
|
|
}
|
|
|
|
// Publish delivers e to every subscriber that has room for it.
|
|
func (h *Hub) Publish(e Event) {
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
for _, ch := range h.subs {
|
|
select {
|
|
case ch <- e:
|
|
default: // slow subscriber: drop instead of stalling the scan
|
|
}
|
|
}
|
|
}
|