Proper log rotation, filename compatibility with windows

This commit is contained in:
Ben Adrian Sarmiento
2024-07-06 12:44:25 +02:00
parent b501b800dd
commit 467f51bdec
6 changed files with 67 additions and 15 deletions

View File

@@ -5,7 +5,6 @@ import (
netHttp "net/http" netHttp "net/http"
"os" "os"
"os/exec" "os/exec"
"strings"
"time" "time"
// _ "net/http/pprof" // Register pprof // _ "net/http/pprof" // Register pprof
@@ -31,10 +30,7 @@ func MainApp(configPath string) {
utils.EnsureDirExists("data/info") utils.EnsureDirExists("data/info")
utils.EnsureDirExists("dump") // "zurgtorrent" files utils.EnsureDirExists("dump") // "zurgtorrent" files
dateStr := time.Now().Format(time.DateOnly) log := logutil.NewLogger("logs/zurg.log")
timeStr := strings.ReplaceAll(time.Now().Format(time.TimeOnly), ":", "-")
logPath := fmt.Sprintf("logs/zurg-%s-%s.log", dateStr, timeStr)
log := logutil.NewLogger(logPath)
zurglog := log.Named("zurg") // logger for this main function zurglog := log.Named("zurg") // logger for this main function

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"github.com/debridmediamanager/zurg/internal/clear" "github.com/debridmediamanager/zurg/internal/clear"
"github.com/debridmediamanager/zurg/internal/version" "github.com/debridmediamanager/zurg/internal/version"
@@ -23,10 +22,7 @@ func NetworkTest(testURL string) {
utils.EnsureDirExists("logs") utils.EnsureDirExists("logs")
utils.EnsureDirExists("data") utils.EnsureDirExists("data")
dateStr := time.Now().Format(time.DateOnly) log := logutil.NewLogger("logs/network-test.log")
timeStr := strings.ReplaceAll(time.Now().Format(time.TimeOnly), ":", "-")
logPath := fmt.Sprintf("logs/network-test-%s-%s.log", dateStr, timeStr)
log := logutil.NewLogger(logPath)
proxyURL := os.Getenv("PROXY") proxyURL := os.Getenv("PROXY")
if proxyURL != "" { if proxyURL != "" {

View File

@@ -5,6 +5,7 @@ import "net/http"
func (hs *Handlers) options(next http.Handler) http.Handler { func (hs *Handlers) options(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" { if r.Method == "OPTIONS" {
w.Header().Set("dav", "1,2")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }

View File

@@ -8,6 +8,8 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -185,7 +187,7 @@ func (t *TorrentManager) getTorrentFiles(parentDir string) mapset.Set[string] {
} }
func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) { func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) {
filePath := "data/" + t.GetKey(torrent) + ".zurgtorrent" filePath := "data/" + t.getTorrentFilename(torrent) + ".zurgtorrent"
file, err := os.Create(filePath) file, err := os.Create(filePath)
if err != nil { if err != nil {
t.log.Warnf("Cannot create file %s: %v", filePath, err) 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)) // 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"
}

View File

@@ -190,7 +190,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *realdebrid.T
} }
func (t *TorrentManager) convertToTorrent(info *realdebrid.TorrentInfo) *Torrent { 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) { if torrent != nil && torrent.DownloadedIDs.ContainsOne(info.ID) {
return torrent return torrent
} }
@@ -391,7 +391,7 @@ func (t *TorrentManager) assignDirectory(tor *Torrent, triggerHook bool, outputL
listing, _ := t.DirectoryMap.Get(directory) listing, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, tor) listing.Set(accessKey, tor)
if directory != INT_ALL { if directory != config.ALL_TORRENTS {
dirs = append(dirs, directory) dirs = append(dirs, directory)
} }

View File

@@ -54,11 +54,13 @@ func NewLogger(logPath string) *Logger {
consoleEncoderCore := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapLevel) consoleEncoderCore := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapLevel)
// Set up file logging with overwrite mode // Set up file logging with overwrite mode
logFile := zapcore.AddSync(&lumberjack.Logger{ lj := lumberjack.Logger{
Filename: logPath, Filename: logPath,
MaxSize: 10, // megabytes MaxSize: 10, // megabytes
MaxBackups: 20, MaxBackups: 20,
}) }
lj.Rotate()
logFile := zapcore.AddSync(&lj)
fmt.Println("Logging to", logPath) fmt.Println("Logging to", logPath)
fileCfg := zapcore.EncoderConfig{ fileCfg := zapcore.EncoderConfig{