@@ -7,6 +7,8 @@ local SystemHelper = {}
77local system_name = sys .get_sys_info ().system_name
88
99SystemHelper .is_linux = system_name == " Linux"
10+ SystemHelper .is_windows = system_name == " Windows"
11+ SystemHelper .is_linux_or_mac = SystemHelper .is_linux or system_name == " Darwin"
1012SystemHelper .is_mobile = ( (system_name == " iPhone OS" ) or (system_name == " Android" ) )
1113SystemHelper .is_debug = sys .get_engine_info ().is_debug
1214
@@ -17,6 +19,22 @@ function SystemHelper.get_timestamp(format)
1719 return os.date (format or ' %H:%M:%S' , os.time ()) --[[ @as string]]
1820end
1921
22+ --- Get correct system dependent filepath for given catalog
23+ --- @param app_catalog string Catalog /folder name
24+ --- @return string
25+ function SystemHelper .directory (app_catalog )
26+ if SystemHelper .is_linux then
27+ -- For Linux we modify the default path to make Linux users happy
28+ local config_dir = " config/" .. app_catalog
29+ return sys .get_save_file (config_dir , " " )
30+ elseif html5 then
31+ -- For HTML5 there's no need to get the full path
32+ return app_catalog
33+ end
34+
35+ return sys .get_save_file (app_catalog , " " )
36+ end
37+
2038--- Get correct system dependent filepath for given file
2139--- @param app_catalog string Catalog /folder name
2240--- @param file_name string File name
@@ -44,4 +62,106 @@ function SystemHelper.filepath(app_catalog, file_name, extension, add_timestamp)
4462 return sys .get_save_file (app_catalog , file_name )
4563end
4664
65+ --- Get a list of all saved files in the specified catalog
66+ --- @param app_catalog string Catalog /folder name
67+ --- @return table List of saved file paths
68+ --- @return string Error message if somethings goes wrong , OK string otherwise
69+ function SystemHelper .get_all_files_in_catalog (app_catalog )
70+ -- Get the base path for the app catalog
71+ local base_file_path
72+ if SystemHelper .is_linux then
73+ local config_dir = " config/" .. app_catalog
74+ base_file_path = sys .get_save_file (config_dir , " " )
75+ elseif html5 then
76+ return {}, " HTML5 does not support directory operations."
77+ else
78+ base_file_path = sys .get_save_file (app_catalog , " " )
79+ end
80+
81+ -- Extract directory from the file path
82+ local directory = base_file_path :match (" ^(.*)[/\\ ]" ) -- Handles both / and \ separators
83+ if not directory then
84+ return {}, " Unable to determine directory path from " .. base_file_path
85+ end
86+
87+ -- Platform-specific file listing command
88+ local list_files_cmd
89+ if SystemHelper .is_linux_or_mac then
90+ -- Linux/MacOS specific: using `ls` command
91+ list_files_cmd = " ls -1 \" " .. directory .. " \" "
92+ elseif SystemHelper .is_windows then
93+ -- Windows specific: using `dir` command
94+ list_files_cmd = " dir /b \" " .. directory .. " \" "
95+ else
96+ return {}, " Unsupported platform for directory operations."
97+ end
98+
99+ -- Capture the output of the command
100+ local files = {}
101+ local handle = io.popen (list_files_cmd )
102+ if handle then
103+ for file in handle :lines () do
104+ if file ~= " ." and file ~= " .." then
105+ table.insert (files , file )
106+ end
107+ end
108+ handle :close ()
109+ else
110+ return {}, " Error: Unable to execute directory listing command."
111+ end
112+
113+ if # files == 0 then
114+ return files , " No files found in directory."
115+ end
116+
117+ return files , " OK"
118+ end
119+
120+
121+ --- Extract timestamp from a filename with the format "squid_log_file_YYYY-MM-DD_HH_MM.txt"
122+ --- @param file_name string Filename to extract the timestamp from
123+ --- @return number | nil The Unix timestamp of the file , or nil if parsing fails
124+ local function extract_timestamp_from_filename (file_name )
125+ local year , month , day , hour , min = file_name :match (" _(%d%d%d%d)%-(%d%d)%-(%d%d)_(%d%d)_(%d%d)%." )
126+ if year and month and day and hour and min then
127+ return os.time ({ year = tonumber (year ), month = tonumber (month ), day = tonumber (day ),
128+ hour = tonumber (hour ), min = tonumber (min ), sec = 0 })
129+ end
130+ return nil
131+ end
132+
133+ --- Remove files older than a specified number of days based on their filename timestamp
134+ --- @param files_table table Table containing file names
135+ --- @param directory string Directory where the files are located
136+ --- @param days_threshold number Number of days before current date to consider files as old
137+ function SystemHelper .remove_old_files_by_filename (files_table , directory , days_threshold )
138+ -- Get current time
139+ local current_time = os.time ()
140+
141+ for _ , file_name in ipairs (files_table ) do
142+ local file_path = directory .. " /" .. file_name -- Adjust path separator if needed
143+
144+ -- Extract the timestamp from the filename
145+ local file_timestamp = extract_timestamp_from_filename (file_name )
146+ if file_timestamp then
147+ -- Calculate file age in days
148+ local file_age_days = os.difftime (current_time , file_timestamp ) / (60 * 60 * 24 )
149+ if file_age_days > days_threshold then
150+ -- Remove the file if it's older than the threshold
151+ -- local success, err =
152+ os.remove (file_path )
153+ --[[ if success then
154+ print("Removed old file:", file_path)
155+ else
156+ print("Error removing file:", file_path, err)
157+ end]]
158+ -- else
159+ -- print("File is not old enough to remove:", file_path)
160+ end
161+ -- else
162+ -- print("Skipping file due to invalid timestamp:", file_name)
163+ end
164+ end
165+ end
166+
47167return SystemHelper
0 commit comments