From 467f51bdecebade0191ab95d387cbac132f7bc0d Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Sat, 6 Jul 2024 12:44:25 +0200 Subject: [PATCH] Proper log rotation, filename compatibility with windows --- internal/app.go | 6 +--- internal/commands.go | 6 +--- internal/handlers/options.go | 1 + internal/torrent/manager.go | 59 +++++++++++++++++++++++++++++++++++- internal/torrent/refresh.go | 4 +-- pkg/logutil/factory.go | 6 ++-- 6 files changed, 67 insertions(+), 15 deletions(-) diff --git a/internal/app.go b/internal/app.go index 1648ae8..120668e 100644 --- a/internal/app.go +++ b/internal/app.go @@ -5,7 +5,6 @@ import ( netHttp "net/http" "os" "os/exec" - "strings" "time" // _ "net/http/pprof" // Register pprof @@ -31,10 +30,7 @@ func MainApp(configPath string) { utils.EnsureDirExists("data/info") utils.EnsureDirExists("dump") // "zurgtorrent" files - dateStr := time.Now().Format(time.DateOnly) - timeStr := strings.ReplaceAll(time.Now().Format(time.TimeOnly), ":", "-") - logPath := fmt.Sprintf("logs/zurg-%s-%s.log", dateStr, timeStr) - log := logutil.NewLogger(logPath) + log := logutil.NewLogger("logs/zurg.log") zurglog := log.Named("zurg") // logger for this main function diff --git a/internal/commands.go b/internal/commands.go index 54cb35e..e1b6bfe 100644 --- a/internal/commands.go +++ b/internal/commands.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "strings" - "time" "github.com/debridmediamanager/zurg/internal/clear" "github.com/debridmediamanager/zurg/internal/version" @@ -23,10 +22,7 @@ func NetworkTest(testURL string) { utils.EnsureDirExists("logs") utils.EnsureDirExists("data") - dateStr := time.Now().Format(time.DateOnly) - timeStr := strings.ReplaceAll(time.Now().Format(time.TimeOnly), ":", "-") - logPath := fmt.Sprintf("logs/network-test-%s-%s.log", dateStr, timeStr) - log := logutil.NewLogger(logPath) + log := logutil.NewLogger("logs/network-test.log") proxyURL := os.Getenv("PROXY") if proxyURL != "" { diff --git a/internal/handlers/options.go b/internal/handlers/options.go index 1f714de..956c9f2 100644 --- a/internal/handlers/options.go +++ b/internal/handlers/options.go @@ -5,6 +5,7 @@ import "net/http" func (hs *Handlers) options(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == "OPTIONS" { + w.Header().Set("dav", "1,2") w.WriteHeader(http.StatusOK) return } diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index e0277da..930c9ac 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -8,6 +8,8 @@ import ( "net/http" "os" "path/filepath" + "regexp" + "runtime" "strings" "sync" "time" @@ -185,7 +187,7 @@ func (t *TorrentManager) getTorrentFiles(parentDir string) mapset.Set[string] { } func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) { - filePath := "data/" + t.GetKey(torrent) + ".zurgtorrent" + filePath := "data/" + t.getTorrentFilename(torrent) + ".zurgtorrent" file, err := os.Create(filePath) if err != nil { t.log.Warnf("Cannot create file %s: %v", filePath, err) @@ -534,3 +536,58 @@ func (t *TorrentManager) initializeDirectoryMaps() { // t.RootNode.AddChild(fs.NewFileNode(directory, true)) } } + +func (t *TorrentManager) getTorrentFilename(torrent *Torrent) string { + if t.Config.EnableRetainRDTorrentName() { + return sanitizeFileName(torrent.Name) + } + // drop the extension from the name + if t.Config.EnableRetainFolderNameExtension() && strings.Contains(torrent.Name, torrent.OriginalName) { + return sanitizeFileName(torrent.Name) + } + + ret := strings.TrimSuffix(torrent.OriginalName, ".mp4") + ret = strings.TrimSuffix(ret, ".mkv") + return sanitizeFileName(ret) +} + +func (t *TorrentManager) getTorrentInfoFilename(torrent *realdebrid.TorrentInfo) string { + if t.Config.EnableRetainRDTorrentName() { + return sanitizeFileName(torrent.Name) + } + // drop the extension from the name + if t.Config.EnableRetainFolderNameExtension() && strings.Contains(torrent.Name, torrent.OriginalName) { + return sanitizeFileName(torrent.Name) + } + + ret := strings.TrimSuffix(torrent.OriginalName, ".mp4") + ret = strings.TrimSuffix(ret, ".mkv") + return sanitizeFileName(ret) +} + +// sanitizeFileName takes a string and converts it to a valid Windows filename +func sanitizeFileName(input string) string { + if !isWindows() { + return input + } + // Define a regex pattern to match invalid filename characters + invalidChars := regexp.MustCompile(`[<>:"/\\|?*]+`) + + // Replace invalid characters with an underscore + sanitized := invalidChars.ReplaceAllString(input, "_") + + // Trim leading and trailing whitespace and dots + sanitized = strings.TrimSpace(sanitized) + sanitized = strings.Trim(sanitized, ".") + + // Ensure the filename is not empty + if sanitized == "" { + sanitized = "default_filename" + } + + return sanitized +} + +func isWindows() bool { + return runtime.GOOS == "windows" +} diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index fb7feef..292523d 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -190,7 +190,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *realdebrid.T } func (t *TorrentManager) convertToTorrent(info *realdebrid.TorrentInfo) *Torrent { - torrent := t.readTorrentFromFile("data/" + info.Hash + ".zurgtorrent") + torrent := t.readTorrentFromFile("data/" + t.getTorrentInfoFilename(info) + ".zurgtorrent") if torrent != nil && torrent.DownloadedIDs.ContainsOne(info.ID) { return torrent } @@ -391,7 +391,7 @@ func (t *TorrentManager) assignDirectory(tor *Torrent, triggerHook bool, outputL listing, _ := t.DirectoryMap.Get(directory) listing.Set(accessKey, tor) - if directory != INT_ALL { + if directory != config.ALL_TORRENTS { dirs = append(dirs, directory) } diff --git a/pkg/logutil/factory.go b/pkg/logutil/factory.go index 8252053..1cbf670 100644 --- a/pkg/logutil/factory.go +++ b/pkg/logutil/factory.go @@ -54,11 +54,13 @@ func NewLogger(logPath string) *Logger { consoleEncoderCore := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapLevel) // Set up file logging with overwrite mode - logFile := zapcore.AddSync(&lumberjack.Logger{ + lj := lumberjack.Logger{ Filename: logPath, MaxSize: 10, // megabytes MaxBackups: 20, - }) + } + lj.Rotate() + logFile := zapcore.AddSync(&lj) fmt.Println("Logging to", logPath) fileCfg := zapcore.EncoderConfig{