Update storage.go (#10)

This commit is contained in:
Kai Gohegan
2024-12-24 22:59:33 +00:00
committed by GitHub
parent 4ff00859a3
commit 810c9d705e

View File

@@ -10,6 +10,7 @@ type TorrentStorage struct {
torrents map[string]*Torrent torrents map[string]*Torrent
mu sync.RWMutex mu sync.RWMutex
order []string order []string
filename string // Added to store the filename for persistence
} }
func loadTorrentsFromJSON(filename string) (map[string]*Torrent, error) { func loadTorrentsFromJSON(filename string) (map[string]*Torrent, error) {
@@ -25,7 +26,7 @@ func loadTorrentsFromJSON(filename string) (map[string]*Torrent, error) {
} }
func NewTorrentStorage(filename string) *TorrentStorage { func NewTorrentStorage(filename string) *TorrentStorage {
// Open the json file and read the data // Open the JSON file and read the data
torrents, err := loadTorrentsFromJSON(filename) torrents, err := loadTorrentsFromJSON(filename)
if err != nil { if err != nil {
torrents = make(map[string]*Torrent) torrents = make(map[string]*Torrent)
@@ -38,6 +39,7 @@ func NewTorrentStorage(filename string) *TorrentStorage {
return &TorrentStorage{ return &TorrentStorage{
torrents: torrents, torrents: torrents,
order: order, order: order,
filename: filename,
} }
} }
@@ -46,6 +48,7 @@ func (ts *TorrentStorage) Add(torrent *Torrent) {
defer ts.mu.Unlock() defer ts.mu.Unlock()
ts.torrents[torrent.Hash] = torrent ts.torrents[torrent.Hash] = torrent
ts.order = append(ts.order, torrent.Hash) ts.order = append(ts.order, torrent.Hash)
ts.saveToFile()
} }
func (ts *TorrentStorage) AddOrUpdate(torrent *Torrent) { func (ts *TorrentStorage) AddOrUpdate(torrent *Torrent) {
@@ -55,55 +58,14 @@ func (ts *TorrentStorage) AddOrUpdate(torrent *Torrent) {
ts.order = append(ts.order, torrent.Hash) ts.order = append(ts.order, torrent.Hash)
} }
ts.torrents[torrent.Hash] = torrent ts.torrents[torrent.Hash] = torrent
} ts.saveToFile()
func (ts *TorrentStorage) GetByID(id string) *Torrent {
ts.mu.RLock()
defer ts.mu.RUnlock()
for _, torrent := range ts.torrents {
if torrent.ID == id {
return torrent
}
}
return nil
}
func (ts *TorrentStorage) Get(hash string) *Torrent {
ts.mu.RLock()
defer ts.mu.RUnlock()
return ts.torrents[hash]
}
func (ts *TorrentStorage) GetAll(category string, filter string, hashes []string) []*Torrent {
ts.mu.RLock()
defer ts.mu.RUnlock()
torrents := make([]*Torrent, 0)
for _, id := range ts.order {
torrent := ts.torrents[id]
if category != "" && torrent.Category != category {
continue
}
if filter != "" && torrent.State != filter {
continue
}
torrents = append(torrents, torrent)
}
if len(hashes) > 0 {
filtered := make([]*Torrent, 0, len(torrents))
for _, hash := range hashes {
if torrent := ts.torrents[hash]; torrent != nil {
filtered = append(filtered, torrent)
}
}
torrents = filtered
}
return torrents
} }
func (ts *TorrentStorage) Update(torrent *Torrent) { func (ts *TorrentStorage) Update(torrent *Torrent) {
ts.mu.Lock() ts.mu.Lock()
defer ts.mu.Unlock() defer ts.mu.Unlock()
ts.torrents[torrent.Hash] = torrent ts.torrents[torrent.Hash] = torrent
ts.saveToFile()
} }
func (ts *TorrentStorage) Delete(hash string) { func (ts *TorrentStorage) Delete(hash string) {
@@ -127,14 +89,20 @@ func (ts *TorrentStorage) Delete(hash string) {
return return
} }
} }
ts.saveToFile()
} }
func (ts *TorrentStorage) Save(filename string) error { func (ts *TorrentStorage) Save() error {
ts.mu.RLock() ts.mu.RLock()
defer ts.mu.RUnlock() defer ts.mu.RUnlock()
data, err := json.Marshal(ts.torrents) return ts.saveToFile()
}
// saveToFile is a helper function to write the current state to the JSON file
func (ts *TorrentStorage) saveToFile() error {
data, err := json.MarshalIndent(ts.torrents, "", " ")
if err != nil { if err != nil {
return err return err
} }
return os.WriteFile(filename, data, 0644) return os.WriteFile(ts.filename, data, 0644)
} }