Some improvements to beta

This commit is contained in:
Mukhtar Akere
2025-06-07 10:03:01 +01:00
parent dfcf8708f1
commit 84603b084b
8 changed files with 38 additions and 27 deletions

View File

@@ -211,7 +211,7 @@ func (r *RealDebrid) handleRarArchive(t *types.Torrent, data torrentInfo, select
fileMap := make(map[string]*types.File)
for i := range selectedFiles {
// RD converts special chars to '_' for RAR file paths
// TOOD: there might be more special chars to replace
// @TODO: there might be more special chars to replace
safeName := strings.NewReplacer("|", "_", "\"", "_", "\\", "_", "?", "_", "*", "_", ":", "_", "<", "_", ">", "_").Replace(selectedFiles[i].Name)
fileMap[safeName] = &selectedFiles[i]
}

View File

@@ -228,11 +228,14 @@ func (c *Cache) Start(ctx context.Context) error {
return fmt.Errorf("failed to create cache directory: %w", err)
}
c.logger.Info().Msgf("Started indexing...")
if err := c.Sync(ctx); err != nil {
return fmt.Errorf("failed to sync cache: %w", err)
}
// Fire the ready channel
close(c.ready)
c.logger.Info().Msgf("Indexing complete, %d torrents loaded", len(c.torrents.getAll()))
// initial download links
go c.refreshDownloadLinks(ctx)
@@ -241,7 +244,7 @@ func (c *Cache) Start(ctx context.Context) error {
c.logger.Error().Err(err).Msg("Failed to start cache worker")
}
c.repairChan = make(chan RepairRequest, 100)
c.repairChan = make(chan RepairRequest, 100) // Initialize the repair channel, max 100 requests buffered
go c.repairWorker(ctx)
cfg := config.Get()
@@ -398,9 +401,11 @@ func (c *Cache) Sync(ctx context.Context) error {
if len(deletedTorrents) > 0 {
c.logger.Info().Msgf("Found %d deleted torrents", len(deletedTorrents))
for _, id := range deletedTorrents {
if _, ok := cachedTorrents[id]; ok {
c.deleteTorrent(id, false) // delete from cache
}
// Remove from cache and debrid service
delete(cachedTorrents, id)
// Remove the json file from disk
c.removeFile(id, false)
}
}
@@ -752,13 +757,13 @@ func (c *Cache) deleteTorrent(id string, removeFromDebrid bool) bool {
if torrent, ok := c.torrents.getByID(id); ok {
c.torrents.removeId(id) // Delete id from cache
defer func() {
c.removeFromDB(id)
c.removeFile(id, false)
if removeFromDebrid {
_ = c.client.DeleteTorrent(id) // Skip error handling, we don't care if it fails
}
}() // defer delete from debrid
torrentName := torrent.Name
torrentName := c.GetTorrentFolder(torrent.Torrent)
if t, ok := c.torrents.getByName(torrentName); ok {
@@ -795,7 +800,7 @@ func (c *Cache) DeleteTorrents(ids []string) {
c.listingDebouncer.Call(true)
}
func (c *Cache) removeFromDB(torrentId string) {
func (c *Cache) removeFile(torrentId string, moveToTrash bool) {
// Moves the torrent file to the trash
filePath := filepath.Join(c.dir, torrentId+".json")
@@ -804,6 +809,14 @@ func (c *Cache) removeFromDB(torrentId string) {
return
}
if !moveToTrash {
// If not moving to trash, delete the file directly
if err := os.Remove(filePath); err != nil {
c.logger.Error().Err(err).Msgf("Failed to remove file: %s", filePath)
return
}
return
}
// Move the file to the trash
trashPath := filepath.Join(c.dir, "trash", torrentId+".json")
if err := os.MkdirAll(filepath.Dir(trashPath), 0755); err != nil {

View File

@@ -276,7 +276,7 @@ func (q *QBit) handleAddTorrentTags(w http.ResponseWriter, r *http.Request) {
request.JSONResponse(w, nil, http.StatusOK)
}
func (q *QBit) handleremoveTorrentTags(w http.ResponseWriter, r *http.Request) {
func (q *QBit) handleRemoveTorrentTags(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, "Failed to parse form data", http.StatusBadRequest)

View File

@@ -20,7 +20,7 @@ func (q *QBit) Routes() http.Handler {
r.Post("/createCategory", q.handleCreateCategory)
r.Post("/setCategory", q.handleSetCategory)
r.Post("/addTags", q.handleAddTorrentTags)
r.Post("/removeTags", q.handleremoveTorrentTags)
r.Post("/removeTags", q.handleRemoveTorrentTags)
r.Post("/createTags", q.handleCreateTags)
r.Get("/tags", q.handleGetTags)
r.Get("/pause", q.handleTorrentsPause)

View File

@@ -135,8 +135,3 @@ func (q *QBit) addTags(tags []string) bool {
}
return true
}
func (q *QBit) removeTags(tags []string) bool {
q.Tags = utils.RemoveItem(q.Tags, tags...)
return true
}

View File

@@ -439,9 +439,9 @@ func (r *Repair) repairArr(job *Job, _arr string, tmdbId string) ([]arr.ContentF
return brokenItems, nil
}
// Check first media to confirm mounts are accessible
if !r.isMediaAccessible(media) {
r.logger.Info().Msgf("Skipping repair. Parent directory not accessible. Check your mounts")
return brokenItems, nil
if err := r.checkMountUp(media); err != nil {
r.logger.Error().Err(err).Msgf("Mount check failed for %s", a.Name)
return brokenItems, fmt.Errorf("mount check failed: %w", err)
}
// Mutex for brokenItems
@@ -504,8 +504,8 @@ func (r *Repair) repairArr(job *Job, _arr string, tmdbId string) ([]arr.ContentF
return brokenItems, nil
}
// isMediaAccessible checks if the mounts are accessible
func (r *Repair) isMediaAccessible(media []arr.Content) bool {
// checkMountUp checks if the mounts are accessible
func (r *Repair) checkMountUp(media []arr.Content) error {
firstMedia := media[0]
for _, m := range media {
if len(m.Files) > 0 {
@@ -515,23 +515,21 @@ func (r *Repair) isMediaAccessible(media []arr.Content) bool {
}
files := firstMedia.Files
if len(files) == 0 {
return false
return fmt.Errorf("no files found in media %s", firstMedia.Title)
}
firstFile := files[0]
symlinkPath := getSymlinkTarget(firstFile.Path)
if symlinkPath == "" {
r.logger.Debug().Msgf("No symlink target found for %s", firstFile.Path)
return false
return fmt.Errorf("no symlink target found for %s", firstFile.Path)
}
r.logger.Debug().Msgf("Checking symlink parent directory for %s", symlinkPath)
parentSymlink := filepath.Dir(filepath.Dir(symlinkPath)) // /mnt/zurg/torrents/movie/movie.mkv -> /mnt/zurg/torrents
if _, err := os.Stat(parentSymlink); os.IsNotExist(err) {
r.logger.Debug().Msgf("Cannot access parent directory %s for %s", parentSymlink, firstFile.Path)
return false
return fmt.Errorf("parent directory %s not accessible for %s", parentSymlink, firstFile.Path)
}
return true
return nil
}
func (r *Repair) getBrokenFiles(job *Job, media arr.Content) []arr.ContentFile {

View File

@@ -41,6 +41,7 @@ type File struct {
reader io.ReadCloser
seekPending bool
content []byte
isRar bool
name string
metadataOnly bool

View File

@@ -245,6 +245,7 @@ func (h *Handler) OpenFile(ctx context.Context, name string, flag int, perm os.F
size: file.Size,
link: file.Link,
metadataOnly: metadataOnly,
isRar: file.IsRar,
modTime: cached.AddedOn,
}, nil
}
@@ -409,6 +410,8 @@ func (h *Handler) serveDirectory(w http.ResponseWriter, r *http.Request, file we
}
}
// Handlers
func (h *Handler) handleGet(w http.ResponseWriter, r *http.Request) {
fRaw, err := h.OpenFile(r.Context(), r.URL.Path, os.O_RDONLY, 0)
if err != nil {
@@ -458,7 +461,8 @@ func (h *Handler) handleGet(w http.ResponseWriter, r *http.Request) {
return
}
file.downloadLink = link
if h.cache.StreamWithRclone() {
// If the torrent file is not a RAR file and users enabled proxy streaming
if !file.isRar && h.cache.StreamWithRclone() {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")