Skip to content

Commit d67461f

Browse files
committed
cleanup
1 parent a962708 commit d67461f

48 files changed

Lines changed: 1618 additions & 862 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

daemon/postprocess/Cleanup.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ bool MoveController::MoveFiles(const fs::path& src, const fs::path& dest)
120120
auto filename = fs::u8string(it->path().filename());
121121
if (filename[0] == '.')
122122
{
123-
if (it->is_directory())
123+
if (it->is_directory(ec))
124124
{
125125
it.disable_recursion_pending();
126126
}
127127
continue;
128128
}
129129

130-
if (it->is_directory())
130+
if (it->is_directory(ec))
131131
{
132132
dirs.push_back(it->path());
133133
}
@@ -137,6 +137,11 @@ bool MoveController::MoveFiles(const fs::path& src, const fs::path& dest)
137137
}
138138
}
139139

140+
if (IsStopped() || ec)
141+
{
142+
return false;
143+
}
144+
140145
for (const auto& d : dirs)
141146
{
142147
if (IsStopped()) return false;
@@ -194,6 +199,7 @@ bool MoveController::MoveFiles(const fs::path& src, const fs::path& dest)
194199
if (fs::u8string(dstPath.filename()) != filename)
195200
{
196201
std::string newName = fs::u8string(dstPath.filename());
202+
GuardedDownloadQueue guard = DownloadQueue::Guard();
197203
m_postInfo->GetNzbInfo()->RenameCompletedFile(filename.c_str(), newName.c_str());
198204
}
199205
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* This file is part of nzbget. See <https://nzbget.com>.
3+
*
4+
* Copyright (C) 2026 Denis <denis@nzbget.com>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
21+
#include "nzbget.h"
22+
#include "CollectionAnalyzer.h"
23+
#include "FileTypes.h"
24+
25+
namespace PostDownloadRenamer
26+
{
27+
28+
std::vector<CollectionAnalyzer::FileGroup>
29+
CollectionAnalyzer::BuildGroups(const std::vector<Candidate>& candidates)
30+
{
31+
std::vector<FileGroup> groups;
32+
for (const Candidate& candidate : candidates)
33+
{
34+
if (FileTypes::IsSubtitleExt(candidate.ext) || FileTypes::IsSampleStem(candidate.stem)) continue;
35+
36+
FileGroup* targetGroup = nullptr;
37+
for (FileGroup& group : groups)
38+
{
39+
if (group.parentDir == candidate.parentDir && group.extKey == candidate.extLower)
40+
{
41+
targetGroup = &group;
42+
break;
43+
}
44+
}
45+
46+
if (!targetGroup)
47+
{
48+
groups.push_back({candidate.parentDir, candidate.extLower, 0, 0, 0, false});
49+
targetGroup = &groups.back();
50+
}
51+
52+
targetGroup->count++;
53+
uintmax_t size = candidate.size;
54+
if (size > targetGroup->largest)
55+
{
56+
targetGroup->second = targetGroup->largest;
57+
targetGroup->largest = size;
58+
}
59+
else if (size > targetGroup->second)
60+
{
61+
targetGroup->second = size;
62+
}
63+
}
64+
65+
for (FileGroup& group : groups)
66+
{
67+
bool isAmbiguousCollection = (group.count >= 2 && group.largest <= group.second * 3);
68+
bool isAudio = FileTypes::IsAudioExt(group.extKey);
69+
group.skip = isAmbiguousCollection && !isAudio;
70+
}
71+
72+
return groups;
73+
}
74+
75+
CollectionAnalyzer::CollectionAnalyzer(const std::vector<Candidate>& candidates)
76+
: m_groups(BuildGroups(candidates))
77+
{
78+
}
79+
80+
bool CollectionAnalyzer::ShouldSkip(const Candidate& candidate) const
81+
{
82+
bool isSub = FileTypes::IsSubtitleExt(candidate.ext);
83+
bool isSample = FileTypes::IsSampleStem(candidate.stem);
84+
85+
if (isSub || isSample)
86+
{
87+
bool hasNonAudioGroup = false;
88+
bool allNonAudioSkipped = true;
89+
90+
for (const FileGroup& group : m_groups)
91+
{
92+
if (group.parentDir == candidate.parentDir && !FileTypes::IsAudioExt(group.extKey))
93+
{
94+
hasNonAudioGroup = true;
95+
if (!group.skip)
96+
{
97+
allNonAudioSkipped = false;
98+
}
99+
}
100+
}
101+
102+
return hasNonAudioGroup && allNonAudioSkipped;
103+
}
104+
105+
for (const FileGroup& group : m_groups)
106+
{
107+
if (group.parentDir == candidate.parentDir && group.extKey == candidate.extLower)
108+
{
109+
return group.skip;
110+
}
111+
}
112+
113+
return false;
114+
}
115+
116+
}
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,37 @@
1818
*/
1919

2020

21-
#ifndef POST_UNPACK_RENAMER_H
22-
#define POST_UNPACK_RENAMER_H
21+
#ifndef COLLECTION_ANALYZER_H
22+
#define COLLECTION_ANALYZER_H
2323

24-
#include "Thread.h"
25-
#include "DownloadInfo.h"
26-
#include <set>
27-
#include <vector>
2824
#include <string>
29-
#include <string_view>
25+
#include <vector>
26+
#include <cstdint>
27+
#include "PostDownloadRenamer.h"
3028

31-
namespace PostUnpackRenamer
29+
namespace PostDownloadRenamer
3230
{
3331

34-
class Controller final : public Thread
32+
class CollectionAnalyzer final
3533
{
3634
public:
37-
static void StartJob(PostInfo* postInfo);
38-
void Run() override;
39-
40-
int RenameFiles(PostInfo* postInfo);
41-
42-
std::string ResolveSubtitleName(std::string_view metaname, std::string_view stem, std::string_view ext);
43-
std::string ResolveUniqueName(std::string_view metaname, std::string_view stem, std::string_view ext,
44-
std::string_view baseName, const std::set<fs::path>& usedPaths, const fs::path& destPath);
35+
explicit CollectionAnalyzer(const std::vector<Candidate>& candidates);
36+
bool ShouldSkip(const Candidate& candidate) const;
4537

4638
private:
47-
PostInfo* m_postInfo = nullptr;
48-
int m_renamedCount = 0;
49-
50-
std::vector<fs::path> CollectCandidates(const fs::path& dir);
51-
void RenameCompleted();
39+
struct FileGroup
40+
{
41+
fs::path parentDir;
42+
std::string extKey;
43+
uintmax_t largest = 0;
44+
uintmax_t second = 0;
45+
int count = 0;
46+
bool skip = false;
47+
};
48+
49+
std::vector<FileGroup> m_groups;
50+
51+
static std::vector<FileGroup> BuildGroups(const std::vector<Candidate>& candidates);
5252
};
5353

5454
}

0 commit comments

Comments
 (0)