Upkeep for data directory

This commit is contained in:
Ben Sarmiento
2024-05-21 06:50:46 +02:00
parent 57df90620e
commit 6c24d74f61
4 changed files with 67 additions and 9 deletions

View File

@@ -137,8 +137,17 @@ func (t *TorrentManager) GetPath(file *File) string {
/// torrent functions
func (t *TorrentManager) getTorrentFiles() mapset.Set[string] {
files, err := filepath.Glob("data/*.torrent_zurg")
if err != nil {
t.log.Warnf("Cannot get files in data directory: %v", err)
return nil
}
return mapset.NewSet[string](files...)
}
func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) {
filePath := "data/" + torrent.Hash + ".json"
filePath := "data/" + torrent.Hash + ".torrent_zurg"
file, err := os.Create(filePath)
if err != nil {
t.log.Warnf("Cannot create file %s: %v", filePath, err)
@@ -163,7 +172,7 @@ func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) {
}
func (t *TorrentManager) readTorrentFromFile(hash string) *Torrent {
filePath := "data/" + hash + ".json"
filePath := "data/" + hash + ".torrent_zurg"
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
@@ -190,7 +199,7 @@ func (t *TorrentManager) readTorrentFromFile(hash string) *Torrent {
}
func (t *TorrentManager) deleteTorrentFile(hash string) {
filePath := "data/" + hash + ".json"
filePath := "data/" + hash + ".torrent_zurg"
err := os.Remove(filePath)
if err != nil {
t.log.Warnf("Cannot delete file %s: %v", filePath, err)
@@ -201,8 +210,17 @@ func (t *TorrentManager) deleteTorrentFile(hash string) {
/// info functions
func (t *TorrentManager) getInfoFiles() mapset.Set[string] {
files, err := filepath.Glob("data/*.info_zurg")
if err != nil {
t.log.Warnf("Cannot get files in data directory: %v", err)
return nil
}
return mapset.NewSet[string](files...)
}
func (t *TorrentManager) writeInfoToFile(info *realdebrid.TorrentInfo) {
filePath := "data/" + info.ID + ".info"
filePath := "data/" + info.ID + ".info_zurg"
file, err := os.Create(filePath)
if err != nil {
t.log.Warnf("Cannot create info file %s: %v", filePath, err)
@@ -225,7 +243,7 @@ func (t *TorrentManager) writeInfoToFile(info *realdebrid.TorrentInfo) {
}
func (t *TorrentManager) readInfoFromFile(torrentID string) *realdebrid.TorrentInfo {
filePath := "data/" + torrentID + ".info"
filePath := "data/" + torrentID + ".info_zurg"
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
@@ -246,7 +264,7 @@ func (t *TorrentManager) readInfoFromFile(torrentID string) *realdebrid.TorrentI
}
func (t *TorrentManager) deleteInfoFile(torrentID string) {
filePath := "data/" + torrentID + ".info"
filePath := "data/" + torrentID + ".info_zurg"
err := os.Remove(filePath)
if err != nil {
t.log.Warnf("Cannot delete info file %s: %v", filePath, err)