@@ -64,39 +64,57 @@ void Utils::RotateFiles(const std::string &folder_path) {
6464 fs::path oldest_hour_folder = hour_folders.front ();
6565
6666 try {
67- auto mp4_files = GetFiles (oldest_hour_folder.string (), " .mp4" );
68- if (!mp4_files.empty ()) {
69- std::sort (mp4_files.begin (), mp4_files.end ());
70-
71- const auto &oldest_file = mp4_files.front ().second ;
72- fs::remove (oldest_file);
73- INFO_PRINT (" Deleted oldest .mp4 file: %s" , oldest_file.string ().c_str ());
74-
75- fs::path corresponding_image = oldest_file;
76- corresponding_image.replace_extension (" .jpg" );
77- if (fs::exists (corresponding_image)) {
78- fs::remove (corresponding_image);
79- INFO_PRINT (" Deleted corresponding .jpg file: %s" ,
80- corresponding_image.string ().c_str ());
67+ std::vector<fs::directory_entry> media_files;
68+ for (const auto &file : fs::directory_iterator (oldest_hour_folder)) {
69+ if (file.is_regular_file ()) {
70+ std::string ext = file.path ().extension ().string ();
71+ if (ext == " .mp4" || ext == " .jpg" ) {
72+ media_files.push_back (file);
73+ }
8174 }
75+ }
8276
83- if (fs::is_empty (oldest_hour_folder)) {
84- fs::remove (oldest_hour_folder);
85- INFO_PRINT (" Deleted empty hour folder: %s" , oldest_hour_folder.string ().c_str ());
77+ if (media_files.empty ()) {
78+ fs::remove_all (oldest_hour_folder);
79+ INFO_PRINT (" Deleted empty hour folder: %s" , oldest_hour_folder.string ().c_str ());
80+ return ;
81+ }
8682
87- if (fs::is_empty (oldest_date_folder)) {
88- fs::remove (oldest_date_folder);
89- INFO_PRINT (" Deleted empty date folder: %s" ,
90- oldest_date_folder.string ().c_str ());
91- }
92- }
83+ // sort by filename
84+ std::sort (media_files.begin (), media_files.end (),
85+ [](const fs::directory_entry &a, const fs::directory_entry &b) {
86+ return a.path ().filename () < b.path ().filename ();
87+ });
88+
89+ fs::path oldest_file = media_files.front ().path ();
90+ fs::remove (oldest_file);
91+ INFO_PRINT (" Deleted file: %s" , oldest_file.string ().c_str ());
92+
93+ // delete same name with different extension
94+ fs::path counterpart = oldest_file;
95+ if (oldest_file.extension () == " .mp4" ) {
96+ counterpart.replace_extension (" .jpg" );
9397 } else {
94- fs::remove_all (oldest_hour_folder);
98+ counterpart.replace_extension (" .mp4" );
99+ }
100+
101+ if (fs::exists (counterpart)) {
102+ fs::remove (counterpart);
103+ INFO_PRINT (" Deleted counterpart file: %s" , counterpart.string ().c_str ());
104+ }
105+
106+ // clean up folder if empty
107+ if (fs::is_empty (oldest_hour_folder)) {
108+ fs::remove (oldest_hour_folder);
95109 INFO_PRINT (" Deleted empty hour folder: %s" , oldest_hour_folder.string ().c_str ());
110+
111+ if (fs::is_empty (oldest_date_folder)) {
112+ fs::remove (oldest_date_folder);
113+ INFO_PRINT (" Deleted empty date folder: %s" , oldest_date_folder.string ().c_str ());
114+ }
96115 }
97116 } catch (const fs::filesystem_error &e) {
98117 std::cerr << " Error while deleting: " << e.what () << std::endl;
99- return ;
100118 }
101119}
102120
0 commit comments