mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-22 13:35:42 -05:00
#2742 Call recordings organized into subfolders with shorter file names
- Audio > Record preference to organize call recordings: no subfolders, date, date/system, system/date (default), system/site/date, system/channel/date, system/talkgroup or radio/date, talkgroup or radio/date, with an optional hour folder below the date folder. - Folder names use identifier values (TG_1001, PATCH_500, RADIO_1234), not alias names. - Recording file names leave out the system, site and channel names when a folder already shows them. - Age-off removes recording folders that become empty, only inside the recordings folder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -65,6 +65,7 @@ public class AgeOffManager
|
||||
|
||||
private final DatabaseManager mDatabaseManager;
|
||||
private final DatabasePreference mPreference;
|
||||
private final UserPreferences mUserPreferences;
|
||||
private ScheduledFuture<?> mScheduledFuture;
|
||||
|
||||
/**
|
||||
@@ -73,6 +74,7 @@ public class AgeOffManager
|
||||
public AgeOffManager(DatabaseManager databaseManager, UserPreferences userPreferences)
|
||||
{
|
||||
mDatabaseManager = databaseManager;
|
||||
mUserPreferences = userPreferences;
|
||||
mPreference = userPreferences.getDatabasePreference();
|
||||
}
|
||||
|
||||
@@ -146,6 +148,7 @@ public class AgeOffManager
|
||||
|
||||
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
|
||||
long events = 0;
|
||||
Path root = mUserPreferences.getDirectoryPreference().getDirectoryRecording();
|
||||
|
||||
try(Connection connection = database.getConnection())
|
||||
{
|
||||
@@ -154,19 +157,21 @@ public class AgeOffManager
|
||||
if(retention.isEnabled())
|
||||
{
|
||||
long cutoff = System.currentTimeMillis() - retention.getMilliseconds();
|
||||
result = result.add(applyRetentionRule(connection, repository, cutoff));
|
||||
result = result.add(applyRetentionRule(connection, repository, cutoff, root));
|
||||
}
|
||||
|
||||
if(mPreference.isAgeRuleEnabled())
|
||||
{
|
||||
long cutoff = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(mPreference.getAgeRuleDays());
|
||||
result = result.add(applyAgeRule(connection, repository, cutoff, mPreference.getAgeRuleAction()));
|
||||
result = result.add(applyAgeRule(connection, repository, cutoff, mPreference.getAgeRuleAction(),
|
||||
root));
|
||||
}
|
||||
|
||||
if(mPreference.isSizeRuleEnabled())
|
||||
{
|
||||
long limit = mPreference.getSizeRuleLimitMB() * BYTES_PER_MB;
|
||||
result = result.add(applySizeRule(connection, repository, limit, mPreference.getSizeRuleAction()));
|
||||
result = result.add(applySizeRule(connection, repository, limit, mPreference.getSizeRuleAction(),
|
||||
root));
|
||||
}
|
||||
|
||||
if(mPreference.isEventRuleEnabled())
|
||||
@@ -197,7 +202,8 @@ public class AgeOffManager
|
||||
* Deletes duplicate calls and Do Not Monitor calls (without an alias record option) that started before the
|
||||
* cutoff time, with their recordings.
|
||||
*/
|
||||
static AgeOffResult applyRetentionRule(Connection connection, ICallRepository repository, long cutoff)
|
||||
static AgeOffResult applyRetentionRule(Connection connection, ICallRepository repository, long cutoff,
|
||||
Path root)
|
||||
throws SQLException
|
||||
{
|
||||
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
|
||||
@@ -205,7 +211,8 @@ public class AgeOffManager
|
||||
|
||||
while(!page.isEmpty())
|
||||
{
|
||||
result = result.add(remove(connection, repository, page, AgeOffAction.DELETE_CALL_AND_RECORDING));
|
||||
result = result.add(remove(connection, repository, page, AgeOffAction.DELETE_CALL_AND_RECORDING,
|
||||
root));
|
||||
page = repository.getDuplicateAndDoNotMonitorCalls(connection, cutoff, PAGE_SIZE);
|
||||
}
|
||||
|
||||
@@ -216,7 +223,7 @@ public class AgeOffManager
|
||||
* Removes the calls (or recordings) of calls that started before the cutoff time.
|
||||
*/
|
||||
static AgeOffResult applyAgeRule(Connection connection, ICallRepository repository, long cutoff,
|
||||
AgeOffAction action) throws SQLException
|
||||
AgeOffAction action, Path root) throws SQLException
|
||||
{
|
||||
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
|
||||
List<CallRecord> page = action == AgeOffAction.DELETE_CALL_AND_RECORDING ?
|
||||
@@ -225,7 +232,7 @@ public class AgeOffManager
|
||||
|
||||
while(!page.isEmpty())
|
||||
{
|
||||
result = result.add(remove(connection, repository, page, action));
|
||||
result = result.add(remove(connection, repository, page, action, root));
|
||||
|
||||
page = action == AgeOffAction.DELETE_CALL_AND_RECORDING ?
|
||||
repository.getCallsStartedBefore(connection, cutoff, PAGE_SIZE) :
|
||||
@@ -239,7 +246,7 @@ public class AgeOffManager
|
||||
* Removes the oldest recordings until the total size of the recordings is at or below the limit.
|
||||
*/
|
||||
static AgeOffResult applySizeRule(Connection connection, ICallRepository repository, long limitBytes,
|
||||
AgeOffAction action) throws SQLException
|
||||
AgeOffAction action, Path root) throws SQLException
|
||||
{
|
||||
AgeOffResult result = new AgeOffResult(0, 0, 0, 0);
|
||||
long total = repository.getRecordedFileSize(connection);
|
||||
@@ -266,7 +273,7 @@ public class AgeOffManager
|
||||
total -= call.getFileSize();
|
||||
}
|
||||
|
||||
result = result.add(remove(connection, repository, toRemove, action));
|
||||
result = result.add(remove(connection, repository, toRemove, action, root));
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -276,7 +283,7 @@ public class AgeOffManager
|
||||
* Deletes the recording files of the calls and then deletes the call rows or marks their recordings deleted.
|
||||
*/
|
||||
private static AgeOffResult remove(Connection connection, ICallRepository repository, List<CallRecord> calls,
|
||||
AgeOffAction action) throws SQLException
|
||||
AgeOffAction action, Path root) throws SQLException
|
||||
{
|
||||
long bytes = 0;
|
||||
int recordings = 0;
|
||||
@@ -286,7 +293,7 @@ public class AgeOffManager
|
||||
{
|
||||
ids.add(call.getId());
|
||||
|
||||
if(call.getFilePath() != null && deleteRecording(call.getFilePath()))
|
||||
if(call.getFilePath() != null && deleteRecording(call.getFilePath(), root))
|
||||
{
|
||||
recordings++;
|
||||
bytes += call.getFileSize();
|
||||
@@ -307,7 +314,7 @@ public class AgeOffManager
|
||||
* Deletes the audio recording file. Only audio files (.mp3, .wav) are deleted.
|
||||
* @return true if the file was deleted
|
||||
*/
|
||||
static boolean deleteRecording(String filePath)
|
||||
static boolean deleteRecording(String filePath, Path root)
|
||||
{
|
||||
String lower = filePath.toLowerCase(Locale.ROOT);
|
||||
|
||||
@@ -319,7 +326,10 @@ public class AgeOffManager
|
||||
|
||||
try
|
||||
{
|
||||
return Files.deleteIfExists(Path.of(filePath));
|
||||
Path path = Path.of(filePath);
|
||||
boolean deleted = Files.deleteIfExists(path);
|
||||
deleteEmptyFolders(path.getParent(), root);
|
||||
return deleted;
|
||||
}
|
||||
catch(IOException | RuntimeException e)
|
||||
{
|
||||
@@ -328,6 +338,50 @@ public class AgeOffManager
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the folder and its parent folders while they are empty, up to (not including) the recordings folder.
|
||||
* Folders outside the recordings folder are never deleted.
|
||||
* @param folder of a deleted recording
|
||||
* @param root recordings folder, or null to not delete any folders
|
||||
*/
|
||||
static void deleteEmptyFolders(Path folder, Path root)
|
||||
{
|
||||
if(folder == null || root == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Path rootPath = root.toAbsolutePath().normalize();
|
||||
Path current = folder.toAbsolutePath().normalize();
|
||||
|
||||
while(current != null && current.startsWith(rootPath) && !current.equals(rootPath))
|
||||
{
|
||||
try(var entries = Files.list(current))
|
||||
{
|
||||
if(entries.findAny().isPresent())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(IOException | RuntimeException e)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Files.delete(current);
|
||||
}
|
||||
catch(IOException | RuntimeException e)
|
||||
{
|
||||
//Not empty (a new recording was just started in it) or not accessible
|
||||
return;
|
||||
}
|
||||
|
||||
current = current.getParent();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Age-off results
|
||||
* @param calls deleted
|
||||
|
||||
Reference in New Issue
Block a user