1919
2020
2121#include " nzbget.h"
22- #include " PostUnpackRenamer.h"
22+ #include " PostRenamer.h"
23+ #include " CollectionAnalyzer.h"
2324#include " Options.h"
2425#include " Log.h"
2526#include " FileSystem.h"
2627#include " Deobfuscation.h"
2728#include " FileTypes.h"
29+ #include " Util.h"
2830
29- namespace PostUnpackRenamer
31+ namespace PostRenamer
3032{
3133
3234void Controller::StartJob (PostInfo* postInfo)
@@ -35,7 +37,7 @@ void Controller::StartJob(PostInfo* postInfo)
3537
3638 if (!renamer)
3739 {
38- error (" Failed to allocate memory for PostUnpackRenamer ::Controller" );
40+ error (" Failed to allocate memory for PostRenamer ::Controller" );
3941 return ;
4042 }
4143
@@ -133,9 +135,9 @@ std::string Controller::ResolveUniqueName(std::string_view metaname, std::string
133135 return candidate;
134136}
135137
136- std::vector<fs::path > Controller::CollectCandidates (const fs::path& dir)
138+ std::vector<PostRenamer::Candidate > Controller::CollectCandidates (const fs::path& dir)
137139{
138- std::vector<fs::path > candidates;
140+ std::vector<Candidate > candidates;
139141
140142 fs::error_code ec;
141143 for (auto it = fs::recursive_directory_iterator (dir, fs::directory_options::skip_permission_denied, ec);
@@ -152,7 +154,18 @@ std::vector<fs::path> Controller::CollectCandidates(const fs::path& dir)
152154
153155 if (Util::MatchFileExt (filename.c_str (), g_Options->GetRenameIgnoreExt (), " ," )) continue ;
154156
155- candidates.push_back (it->path ());
157+ Candidate candidate;
158+ candidate.path = it->path ();
159+ candidate.parentDir = candidate.path .parent_path ();
160+ candidate.filename = filename;
161+ candidate.stem = fs::u8string (candidate.path .stem ());
162+ candidate.ext = fs::u8string (candidate.path .extension ());
163+ candidate.extLower = candidate.ext ;
164+ std::transform (candidate.extLower .begin (), candidate.extLower .end (), candidate.extLower .begin (),
165+ [](unsigned char c) { return std::tolower (c); });
166+ candidate.size = fs::file_size (candidate.path , ec);
167+ if (ec) candidate.size = 0 ;
168+ candidates.push_back (std::move (candidate));
156169 }
157170 return candidates;
158171}
@@ -181,17 +194,19 @@ int Controller::RenameFiles(PostInfo* postInfo)
181194 }
182195
183196 auto candidates = CollectCandidates (destPath);
197+ CollectionAnalyzer analyzer (candidates);
184198
185199 std::set<fs::path> usedPaths;
186200 int renamedCount = 0 ;
187201
188- for (const fs::path& fullPath : candidates)
202+ for (Candidate& candidate : candidates)
189203 {
190204 if (IsStopped ()) break ;
205+ if (analyzer.ShouldSkip (candidate)) continue ;
191206
192- std::string ext = fs::u8string (fullPath. extension ()) ;
193- std::string stem = fs::u8string (fullPath .stem ()) ;
194- std::string filename = fs::u8string (fullPath. filename ()) ;
207+ const std::string& filename = candidate. filename ;
208+ const std::string& stem = candidate .stem ;
209+ const std::string& ext = candidate. ext ;
195210 std::string newName;
196211
197212 if (FileTypes::IsSubtitleExt (ext))
@@ -207,23 +222,23 @@ int Controller::RenameFiles(PostInfo* postInfo)
207222 newName = metaname + ext;
208223 }
209224
210- fs::path parentDir = fullPath .parent_path ();
211- std::string candidate = ResolveUniqueName (metaname, stem, ext, newName, usedPaths, parentDir);
212- fs::path newPath = parentDir / candidate ;
225+ fs::path parentDir = candidate. path .parent_path ();
226+ std::string candidateName = ResolveUniqueName (metaname, stem, ext, newName, usedPaths, parentDir);
227+ fs::path newPath = parentDir / candidateName ;
213228 usedPaths.insert (newPath);
214229
215- nzbInfo->PrintMessage (Message::mkInfo, " Renaming obfuscated file %s to %s" , filename.c_str (), candidate .c_str ());
230+ nzbInfo->PrintMessage (Message::mkInfo, " Renaming obfuscated file %s to %s" , filename.c_str (), candidateName .c_str ());
216231 fs::error_code moveEc;
217- fs::move_file (fullPath , newPath, moveEc);
232+ fs::move_file (candidate. path , newPath, moveEc);
218233 if (moveEc)
219234 {
220235 nzbInfo->PrintMessage (Message::mkWarning,
221236 " Could not rename obfuscated file %s to %s: %s" ,
222- filename.c_str (), candidate .c_str (), moveEc.message ().c_str ());
237+ filename.c_str (), candidateName .c_str (), moveEc.message ().c_str ());
223238 continue ;
224239 }
225240
226- if (!nzbInfo->RenameCompletedFile (filename.c_str (), candidate .c_str ()))
241+ if (!nzbInfo->RenameCompletedFile (filename.c_str (), candidateName .c_str ()))
227242 {
228243 nzbInfo->PrintMessage (Message::mkWarning,
229244 " Could not update completed file record for %s" , filename.c_str ());
0 commit comments