updated filepaths for multiplatform support (#56)

- Migrate from Go's path to path/filepath to support multi-OS support
This commit is contained in:
somesuchnonsense
2025-04-26 14:59:23 -05:00
committed by GitHub
parent bce51ecd4f
commit 6e2d1e1a7f
5 changed files with 74 additions and 61 deletions

View File

@@ -5,6 +5,14 @@ import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/go-co-op/gocron/v2"
"github.com/goccy/go-json"
"github.com/puzpuzpuz/xsync/v3"
@@ -13,13 +21,6 @@ import (
"github.com/sirrobot01/decypharr/internal/logger"
"github.com/sirrobot01/decypharr/internal/utils"
"github.com/sirrobot01/decypharr/pkg/debrid/types"
"os"
path "path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
type WebDavFolderNaming string
@@ -115,8 +116,8 @@ func New(dc config.Debrid, client types.Client) *Cache {
autoExpiresLinksAfter = 48 * time.Hour
}
return &Cache{
dir: path.Join(cfg.Path, "cache", dc.Name), // path to save cache files
torrents: xsync.NewMapOf[string, string](),
dir: filepath.Join(cfg.Path, "cache", dc.Name), // path to save cache files
torrents: xsync.NewMapOf[string, *CachedTorrent](),
torrentsNames: xsync.NewMapOf[string, *CachedTorrent](),
invalidDownloadLinks: xsync.NewMapOf[string, string](),
client: client,
@@ -178,7 +179,7 @@ func (c *Cache) load() (map[string]*CachedTorrent, error) {
// Get only json files
var jsonFiles []os.DirEntry
for _, file := range files {
if !file.IsDir() && path.Ext(file.Name()) == ".json" {
if !file.IsDir() && filepath.Ext(file.Name()) == ".json" {
jsonFiles = append(jsonFiles, file)
}
}
@@ -206,7 +207,7 @@ func (c *Cache) load() (map[string]*CachedTorrent, error) {
}
fileName := file.Name()
filePath := path.Join(c.dir, fileName)
filePath := filepath.Join(c.dir, fileName)
data, err := os.ReadFile(filePath)
if err != nil {
c.logger.Error().Err(err).Msgf("Failed to read file: %s", filePath)
@@ -238,8 +239,8 @@ func (c *Cache) load() (map[string]*CachedTorrent, error) {
ct.AddedOn = addedOn
}
ct.IsComplete = true
ct.Name = path.Clean(ct.Name)
ct.Files = fs
ct.Name = filepath.Clean(ct.Name)
results.Store(ct.Id, &ct)
}
}
@@ -393,19 +394,19 @@ func (c *Cache) sync(torrents []*types.Torrent) error {
func (c *Cache) GetTorrentFolder(torrent *types.Torrent) string {
switch c.folderNaming {
case WebDavUseFileName:
return path.Clean(torrent.Filename)
return filepath.Clean(torrent.Filename)
case WebDavUseOriginalName:
return path.Clean(torrent.OriginalFilename)
return filepath.Clean(torrent.OriginalFilename)
case WebDavUseFileNameNoExt:
return path.Clean(utils.RemoveExtension(torrent.Filename))
return filepath.Clean(utils.RemoveExtension(torrent.Filename))
case WebDavUseOriginalNameNoExt:
return path.Clean(utils.RemoveExtension(torrent.OriginalFilename))
return filepath.Clean(utils.RemoveExtension(torrent.OriginalFilename))
case WebDavUseID:
return torrent.Id
case WebdavUseHash:
return strings.ToLower(torrent.InfoHash)
default:
return path.Clean(torrent.Filename)
return filepath.Clean(torrent.Filename)
}
}
@@ -514,7 +515,7 @@ func (c *Cache) SaveTorrent(ct *CachedTorrent) {
func (c *Cache) saveTorrent(id string, data []byte) {
fileName := id + ".json"
filePath := path.Join(c.dir, fileName)
filePath := filepath.Join(c.dir, fileName)
// Use a unique temporary filename for concurrent safety
tmpFile := filePath + ".tmp." + strconv.FormatInt(time.Now().UnixNano(), 10)
@@ -675,7 +676,7 @@ func (c *Cache) DeleteTorrents(ids []string) {
func (c *Cache) removeFromDB(torrentId string) {
// Moves the torrent file to the trash
filePath := path.Join(c.dir, torrentId+".json")
filePath := filepath.Join(c.dir, torrentId+".json")
// Check if the file exists
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
@@ -683,8 +684,8 @@ func (c *Cache) removeFromDB(torrentId string) {
}
// Move the file to the trash
trashPath := path.Join(c.dir, "trash", torrentId+".json")
if err := os.MkdirAll(path.Dir(trashPath), 0755); err != nil {
trashPath := filepath.Join(c.dir, "trash", torrentId+".json")
if err := os.MkdirAll(filepath.Dir(trashPath), 0755); err != nil {
return
}
if err := os.Rename(filePath, trashPath); err != nil {

View File

@@ -2,12 +2,14 @@ package debrid
import (
"fmt"
"github.com/beevik/etree"
"github.com/sirrobot01/decypharr/internal/request"
"net/http"
"os"
path "path/filepath"
"path"
"path/filepath"
"time"
"github.com/beevik/etree"
"github.com/sirrobot01/decypharr/internal/request"
)
// resetPropfindResponse resets the propfind response cache for the specified parent directories.
@@ -64,15 +66,15 @@ func (c *Cache) refreshParentXml(torrents []os.FileInfo, clientName, parent stri
currentTime := time.Now().UTC().Format(http.TimeFormat)
// Add the parent directory
baseUrl := path.Clean(fmt.Sprintf("/webdav/%s/%s", clientName, parent))
parentPath := fmt.Sprintf("%s/", baseUrl)
baseUrl := path.Clean(path.Join("webdav", clientName, parent))
parentPath := path.Join(baseUrl)
addDirectoryResponse(multistatus, parentPath, parent, currentTime)
// Add torrents to the XML
for _, torrent := range torrents {
name := torrent.Name()
// Note the path structure change - parent first, then torrent name
torrentPath := fmt.Sprintf("/webdav/%s/%s/%s/",
torrentPath := filepath.Join("webdav",
clientName,
parent,
name,