From 1024231f0aa4a4e37606065c74ab71e3b27b6507 Mon Sep 17 00:00:00 2001 From: denny Date: Sun, 22 Nov 2026 03:43:14 -0500 Subject: [PATCH] #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 --- .../database/ageoff/AgeOffManager.java | 80 +++++- .../record/RecordPreferenceEditor.java | 49 +++- .../preference/record/RecordPreference.java | 39 +++ .../github/dsheirer/record/CallRecorder.java | 85 ++++-- .../record/RecordingFolderLayout.java | 93 +++++++ .../dsheirer/record/RecordingFolders.java | 248 ++++++++++++++++++ .../database/ageoff/AgeOffFolderTest.java | 72 +++++ .../dsheirer/record/RecordingFoldersTest.java | 142 ++++++++++ 8 files changed, 775 insertions(+), 33 deletions(-) create mode 100644 src/main/java/io/github/dsheirer/record/RecordingFolderLayout.java create mode 100644 src/main/java/io/github/dsheirer/record/RecordingFolders.java create mode 100644 src/test/java/io/github/dsheirer/database/ageoff/AgeOffFolderTest.java create mode 100644 src/test/java/io/github/dsheirer/record/RecordingFoldersTest.java diff --git a/src/main/java/io/github/dsheirer/database/ageoff/AgeOffManager.java b/src/main/java/io/github/dsheirer/database/ageoff/AgeOffManager.java index 7ab923de..bd4ad407 100644 --- a/src/main/java/io/github/dsheirer/database/ageoff/AgeOffManager.java +++ b/src/main/java/io/github/dsheirer/database/ageoff/AgeOffManager.java @@ -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 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 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 diff --git a/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java b/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java index 2ef67aff..60bb1c40 100644 --- a/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java +++ b/src/main/java/io/github/dsheirer/gui/preference/record/RecordPreferenceEditor.java @@ -23,6 +23,7 @@ import io.github.dsheirer.preference.UserPreferences; import io.github.dsheirer.preference.record.RecordPreference; import io.github.dsheirer.record.BasebandRecordFormat; import io.github.dsheirer.record.RecordFormat; +import io.github.dsheirer.record.RecordingFolderLayout; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Insets; @@ -80,11 +81,55 @@ public class RecordPreferenceEditor extends HBox mEditorPane.add(recordAll, 0, 2, 2, 1); Label recordAllNote = new Label("Records the audio of every call. When off, only calls with an alias " + "that has the Record option are recorded. Duplicate and Do Not Monitor calls are deleted after the " + - "retention time set in the Call Database preferences. Use the Call Database age-off rules to limit " + - "the disk space used by recordings."); + "retention time set in the Database Settings preferences. Use the Database Settings age-off rules " + + "to limit the disk space used by recordings."); recordAllNote.setWrapText(true); recordAllNote.setMaxWidth(560); mEditorPane.add(recordAllNote, 0, 3, 2, 1); + + Label layoutLabel = new Label("Organize Call Recordings:"); + ComboBox layoutComboBox = new ComboBox<>(); + layoutComboBox.getItems().addAll(RecordingFolderLayout.values()); + layoutComboBox.setValue(mRecordPreference.getRecordingFolderLayout()); + layoutComboBox.setVisibleRowCount(RecordingFolderLayout.values().length); + mEditorPane.add(layoutLabel, 0, 4); + mEditorPane.add(layoutComboBox, 1, 4); + + CheckBox hourFolder = new CheckBox("Add an hour folder below the date folder (for busy systems)"); + hourFolder.setSelected(mRecordPreference.isRecordingHourFolder()); + mEditorPane.add(hourFolder, 0, 5, 2, 1); + + Label example = new Label(); + example.setStyle("-fx-font-family: monospace;"); + mEditorPane.add(example, 0, 6, 2, 1); + Runnable updateExample = () -> { + RecordingFolderLayout layout = layoutComboBox.getValue(); + hourFolder.setDisable(layout == null || !layout.hasDate()); + example.setText("Example: " + (layout == null || layout == RecordingFolderLayout.NONE ? + "/" : "/" + layout.getExample() + "/" + + (hourFolder.isSelected() ? "14/" : "")) + ""); + }; + layoutComboBox.valueProperty().addListener((observable, oldValue, layout) -> { + if(layout != null) + { + mRecordPreference.setRecordingFolderLayout(layout); + } + + updateExample.run(); + }); + hourFolder.selectedProperty().addListener((observable, oldValue, selected) -> { + mRecordPreference.setRecordingHourFolder(selected); + updateExample.run(); + }); + updateExample.run(); + + Label layoutNote = new Label("Applies to new call recordings; existing recordings stay where they are. " + + "The date is the call start date. Talkgroup or Radio is the TO talkgroup (TG_1001), patch group " + + "(PATCH_500) or radio of a private call (RADIO_1234), else the FROM radio, else the channel name. " + + "Folder names use the identifier values, not alias names, so they don't change when aliases change."); + layoutNote.setWrapText(true); + layoutNote.setMaxWidth(560); + mEditorPane.add(layoutNote, 0, 7, 2, 1); } return mEditorPane; diff --git a/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java b/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java index cf5f9c07..8b4c0ccc 100644 --- a/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java +++ b/src/main/java/io/github/dsheirer/preference/record/RecordPreference.java @@ -23,6 +23,7 @@ import io.github.dsheirer.preference.Preference; import io.github.dsheirer.preference.PreferenceType; import io.github.dsheirer.record.BasebandRecordFormat; import io.github.dsheirer.record.RecordFormat; +import io.github.dsheirer.record.RecordingFolderLayout; import io.github.dsheirer.sample.Listener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,6 +39,8 @@ public class RecordPreference extends Preference private static final RecordFormat DEFAULT_RECORD_FORMAT = RecordFormat.MP3; private static final String PREFERENCE_KEY_BASEBAND_RECORD_FORMAT = "baseband.record.format"; private static final String PREFERENCE_KEY_RECORD_ALL_CALLS = "audio.record.all.calls"; + private static final String PREFERENCE_KEY_FOLDER_LAYOUT = "audio.record.folder.layout"; + private static final String PREFERENCE_KEY_HOUR_FOLDER = "audio.record.folder.hour"; private static final BasebandRecordFormat DEFAULT_BASEBAND_RECORD_FORMAT = BasebandRecordFormat.WAVE; private final static Logger mLog = LoggerFactory.getLogger(RecordPreference.class); private Preferences mPreferences = Preferences.userNodeForPackage(RecordPreference.class); @@ -150,4 +153,40 @@ public class RecordPreference extends Preference mPreferences.putBoolean(PREFERENCE_KEY_RECORD_ALL_CALLS, recordAllCalls); notifyPreferenceUpdated(); } + + /** + * How call audio recordings are organized into subfolders of the recordings folder. Default: System / Date. + */ + public RecordingFolderLayout getRecordingFolderLayout() + { + try + { + return RecordingFolderLayout.valueOf(mPreferences.get(PREFERENCE_KEY_FOLDER_LAYOUT, + RecordingFolderLayout.DEFAULT.name())); + } + catch(IllegalArgumentException iae) + { + return RecordingFolderLayout.DEFAULT; + } + } + + public void setRecordingFolderLayout(RecordingFolderLayout layout) + { + mPreferences.put(PREFERENCE_KEY_FOLDER_LAYOUT, layout.name()); + notifyPreferenceUpdated(); + } + + /** + * Indicates if an hour folder (00 - 23) is added below the date folder, for busy systems. Default: false. + */ + public boolean isRecordingHourFolder() + { + return mPreferences.getBoolean(PREFERENCE_KEY_HOUR_FOLDER, false); + } + + public void setRecordingHourFolder(boolean hourFolder) + { + mPreferences.putBoolean(PREFERENCE_KEY_HOUR_FOLDER, hourFolder); + notifyPreferenceUpdated(); + } } diff --git a/src/main/java/io/github/dsheirer/record/CallRecorder.java b/src/main/java/io/github/dsheirer/record/CallRecorder.java index b5a89694..430145ba 100644 --- a/src/main/java/io/github/dsheirer/record/CallRecorder.java +++ b/src/main/java/io/github/dsheirer/record/CallRecorder.java @@ -41,6 +41,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.nio.file.NoSuchFileException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -92,6 +93,17 @@ public class CallRecorder return mUserPreferences.getDirectoryPreference().getDirectoryRecording(); } + /** + * Folder for a new recording: the recordings folder, or a subfolder of it from the folder layout preference. + */ + Path getRecordingFolder(IdentifierCollection identifierCollection) + { + var preference = mUserPreferences.getRecordPreference(); + return RecordingFolders.getFolder(getRecordingBasePath(), preference.getRecordingFolderLayout(), + preference.isRecordingHourFolder(), RecordingFolders.CallFolders.from(identifierCollection), + System.currentTimeMillis()); + } + /** * Indicates if the call is recorded: all calls when record all calls is enabled, otherwise only calls with an * alias that has the record option. Duplicate and Do Not Monitor calls are recorded and later deleted by the @@ -109,6 +121,15 @@ public class CallRecorder { Path path = getAudioRecordingPath(identifierCollection, recordFormat); + try + { + Files.createDirectories(path.getParent()); + } + catch(IOException ioe) + { + mLog.error("Unable to create the recording folder [" + path.getParent() + "] - " + ioe.getMessage()); + } + if(!Files.exists(path)) { return path; @@ -192,7 +213,16 @@ public class CallRecorder try { - mWriter = createWriter(path, recordFormat, mAudioSegment); + try + { + mWriter = createWriter(path, recordFormat, mAudioSegment); + } + catch(NoSuchFileException nsfe) + { + //The age-off removed the (empty) folder after it was created - create it again + Files.createDirectories(path.getParent()); + mWriter = createWriter(path, recordFormat, mAudioSegment); + } call.setRecordFormat(recordFormat.name()); call.setFilePath(path.toAbsolutePath().toString()); call.setRecordingState(RecordingState.RECORDING); @@ -271,38 +301,57 @@ public class CallRecorder /** * Provides a formatted audio recording filename to use as the final audio filename. */ + /** + * Appends a file name part, with an underscore separator after the previous part. + */ + private static void appendPart(StringBuilder sb, String part) + { + if(part != null && !part.isBlank()) + { + if(!sb.isEmpty()) + { + sb.append("_"); + } + + sb.append(part.trim()); + } + } + + private static String getConfiguration(IdentifierCollection identifierCollection, Form form) + { + Identifier identifier = identifierCollection.getIdentifier(IdentifierClass.CONFIGURATION, form, Role.ANY); + return identifier instanceof StringIdentifier stringIdentifier ? stringIdentifier.getValue() : null; + } + private Path getAudioRecordingPath(IdentifierCollection identifierCollection, RecordFormat recordFormat) { StringBuilder sb = new StringBuilder(); if(identifierCollection != null) { - Identifier system = identifierCollection.getIdentifier(IdentifierClass.CONFIGURATION, Form.SYSTEM, Role.ANY); + //Leave out the system, site and channel names when a recording subfolder already shows them + RecordingFolderLayout layout = mUserPreferences.getRecordPreference().getRecordingFolderLayout(); - if(system != null) + if(!layout.hasSystemFolder()) { - sb.append(((StringIdentifier)system).getValue()).append("_"); + appendPart(sb, getConfiguration(identifierCollection, Form.SYSTEM)); } - Identifier site = identifierCollection.getIdentifier(IdentifierClass.CONFIGURATION, Form.SITE, Role.ANY); - - if(site != null) + if(!layout.hasSiteFolder()) { - sb.append(((StringIdentifier)site).getValue()).append("_"); + appendPart(sb, getConfiguration(identifierCollection, Form.SITE)); } - Identifier channel = identifierCollection.getIdentifier(IdentifierClass.CONFIGURATION, Form.CHANNEL, Role.ANY); - - if(channel != null) + if(!layout.hasChannelFolder()) { - sb.append(((StringIdentifier)channel).getValue()).append("_"); + appendPart(sb, getConfiguration(identifierCollection, Form.CHANNEL)); } Identifier to = identifierCollection.getIdentifier(IdentifierClass.USER, Form.TALKGROUP, Role.TO); if(to != null) { - sb.append("_TO_").append(clean(to.toString())); + appendPart(sb, "TO_" + clean(to.toString())); } else { @@ -310,7 +359,7 @@ public class CallRecorder if(!toIdentifiers.isEmpty()) { - sb.append("_TO_").append(clean(toIdentifiers.get(0).toString())); + appendPart(sb, "TO_" + clean(toIdentifiers.get(0).toString())); } } @@ -318,7 +367,7 @@ public class CallRecorder if(from != null) { - sb.append("_FROM_").append(clean(from.toString())); + appendPart(sb, "FROM_" + clean(from.toString())); } else { @@ -330,7 +379,7 @@ public class CallRecorder { if(identifier.getForm() != Form.TONE) { - sb.append("_FROM_").append(clean(identifier.toString())); + appendPart(sb, "FROM_" + clean(identifier.toString())); break; } } @@ -352,7 +401,7 @@ public class CallRecorder if(toneSequence.hasTones()) { - sb.append("_TONES"); + appendPart(sb, "TONES"); for(Tone tone: toneIdentifier.getValue().getTones()) { @@ -409,7 +458,7 @@ public class CallRecorder sbFinal.append(recordFormat.getExtension()); - return getRecordingBasePath().resolve(sbFinal.toString()); + return getRecordingFolder(identifierCollection).resolve(sbFinal.toString()); } diff --git a/src/main/java/io/github/dsheirer/record/RecordingFolderLayout.java b/src/main/java/io/github/dsheirer/record/RecordingFolderLayout.java new file mode 100644 index 00000000..df2b88dd --- /dev/null +++ b/src/main/java/io/github/dsheirer/record/RecordingFolderLayout.java @@ -0,0 +1,93 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record; + +/** + * How call audio recordings are organized into subfolders of the recordings folder. The date folder is the date the + * call started (local time), named yyyy-MM-dd so that the folders sort by date. + */ +public enum RecordingFolderLayout +{ + NONE("No subfolders", ""), + DATE("Date", "2026-11-22"), + DATE_SYSTEM("Date / System", "2026-11-22/Metro"), + SYSTEM_DATE("System / Date", "Metro/2026-11-22"), + SYSTEM_SITE_DATE("System / Site / Date", "Metro/Site-1/2026-11-22"), + SYSTEM_CHANNEL_DATE("System / Channel / Date", "Metro/Control-A/2026-11-22"), + SYSTEM_TALKGROUP_DATE("System / Talkgroup or Radio / Date", "Metro/TG_1001/2026-11-22"), + TALKGROUP_DATE("Talkgroup or Radio / Date", "TG_1001/2026-11-22"); + + public static final RecordingFolderLayout DEFAULT = SYSTEM_DATE; + + private final String mLabel; + private final String mExample; + + RecordingFolderLayout(String label, String example) + { + mLabel = label; + mExample = example; + } + + /** + * Example folder path for this layout + */ + public String getExample() + { + return mExample; + } + + /** + * Indicates if the layout has a date folder (that an hour folder can be added below) + */ + public boolean hasDate() + { + return this != NONE; + } + + /** + * Indicates if the layout has a system folder, so the recording file name leaves out the system name + */ + public boolean hasSystemFolder() + { + return this == DATE_SYSTEM || this == SYSTEM_DATE || this == SYSTEM_SITE_DATE || this == SYSTEM_CHANNEL_DATE || + this == SYSTEM_TALKGROUP_DATE; + } + + /** + * Indicates if the layout has a site folder, so the recording file name leaves out the site name + */ + public boolean hasSiteFolder() + { + return this == SYSTEM_SITE_DATE; + } + + /** + * Indicates if the layout has a channel folder, so the recording file name leaves out the channel name + */ + public boolean hasChannelFolder() + { + return this == SYSTEM_CHANNEL_DATE; + } + + @Override + public String toString() + { + return mLabel; + } +} diff --git a/src/main/java/io/github/dsheirer/record/RecordingFolders.java b/src/main/java/io/github/dsheirer/record/RecordingFolders.java new file mode 100644 index 00000000..3d74d975 --- /dev/null +++ b/src/main/java/io/github/dsheirer/record/RecordingFolders.java @@ -0,0 +1,248 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record; + +import io.github.dsheirer.call.CallManager; +import io.github.dsheirer.identifier.Form; +import io.github.dsheirer.identifier.Identifier; +import io.github.dsheirer.identifier.IdentifierClass; +import io.github.dsheirer.identifier.IdentifierCollection; +import io.github.dsheirer.identifier.Role; +import io.github.dsheirer.util.StringUtils; + +import java.nio.file.Path; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +/** + * Builds the subfolder of the recordings folder for a call recording, from the folder layout preference. + */ +public final class RecordingFolders +{ + private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + private static final DateTimeFormatter HOUR = DateTimeFormatter.ofPattern("HH"); + private static final int MAX_FOLDER_NAME_LENGTH = 64; + static final String UNKNOWN_SYSTEM = "Unknown-System"; + static final String UNKNOWN_SITE = "Unknown-Site"; + static final String UNKNOWN_CHANNEL = "Unknown-Channel"; + static final String UNKNOWN_TALKGROUP = "Unknown"; + + private RecordingFolders() + { + } + + /** + * Folder names of a call, extracted from its identifiers + * @param system name from the channel configuration + * @param site name from the channel configuration + * @param channel name from the channel configuration + * @param talkgroup folder name of the TO talkgroup or radio (see {@link #getTalkgroupFolder}) + */ + public record CallFolders(String system, String site, String channel, String talkgroup) + { + /** + * Extracts the folder names from the identifiers of a call + */ + public static CallFolders from(IdentifierCollection identifiers) + { + if(identifiers == null) + { + return new CallFolders(null, null, null, null); + } + + String channel = getConfiguration(identifiers, Form.CHANNEL); + return new CallFolders(getConfiguration(identifiers, Form.SYSTEM), getConfiguration(identifiers, Form.SITE), + channel, getTalkgroupFolder(identifiers, channel)); + } + } + + /** + * Folder for the recording, below the recordings folder. + * @param root recordings folder + * @param layout of the subfolders + * @param hourFolder to add an hour folder (00 - 23) below the date folder + * @param folders of the call + * @param timestamp of the call start + */ + public static Path getFolder(Path root, RecordingFolderLayout layout, boolean hourFolder, CallFolders folders, + long timestamp) + { + Path folder = root; + + for(String name: getFolderNames(layout, hourFolder, folders, timestamp)) + { + folder = folder.resolve(name); + } + + return folder; + } + + /** + * Subfolder names for the recording, outermost first. + */ + static List getFolderNames(RecordingFolderLayout layout, boolean hourFolder, CallFolders folders, + long timestamp) + { + List names = new ArrayList<>(); + + if(layout == null || layout == RecordingFolderLayout.NONE) + { + return names; + } + + LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()); + String date = DATE.format(time); + String system = clean(folders.system(), UNKNOWN_SYSTEM); + + switch(layout) + { + case DATE -> names.add(date); + case DATE_SYSTEM -> + { + names.add(date); + names.add(system); + } + case SYSTEM_DATE -> + { + names.add(system); + names.add(date); + } + case SYSTEM_SITE_DATE -> + { + names.add(system); + names.add(clean(folders.site(), UNKNOWN_SITE)); + names.add(date); + } + case SYSTEM_CHANNEL_DATE -> + { + names.add(system); + names.add(clean(folders.channel(), UNKNOWN_CHANNEL)); + names.add(date); + } + case SYSTEM_TALKGROUP_DATE -> + { + names.add(system); + names.add(clean(folders.talkgroup(), UNKNOWN_TALKGROUP)); + names.add(date); + } + case TALKGROUP_DATE -> + { + names.add(clean(folders.talkgroup(), UNKNOWN_TALKGROUP)); + names.add(date); + } + default -> {} + } + + //The hour folder goes directly below the date folder + if(hourFolder) + { + names.add(names.indexOf(date) + 1, HOUR.format(time)); + } + + return names; + } + + /** + * Folder name for the talkgroup or radio of a call: the TO talkgroup (TG_1001), patch group (PATCH_500) or radio + * of a private call (RADIO_1234), else the FROM radio (e.g. a conventional channel with radio IDs), else the + * channel name (a conventional channel without identifiers), else null. The plain identifier value is used, not + * the alias name or the talkgroup display format, so that the folder doesn't change when they change. + */ + static String getTalkgroupFolder(IdentifierCollection identifiers, String channel) + { + String to = getIdentifierFolder(identifiers.getToIdentifier()); + + if(to != null) + { + return to; + } + + String from = getIdentifierFolder(identifiers.getFromIdentifier()); + + if(from != null) + { + return from; + } + + return channel; + } + + private static String getIdentifierFolder(Identifier identifier) + { + if(identifier == null || identifier.getValue() == null) + { + return null; + } + + Long value = CallManager.getValue(identifier); + + return switch(identifier.getForm()) + { + case TALKGROUP -> value != null ? "TG_" + value : null; + case PATCH_GROUP -> value != null ? "PATCH_" + value : null; + case RADIO -> value != null ? "RADIO_" + value : null; + case TONE, TALKER_ALIAS -> null; + default -> identifier.toString(); + }; + } + + private static String getConfiguration(IdentifierCollection identifiers, Form form) + { + Identifier identifier = identifiers.getIdentifier(IdentifierClass.CONFIGURATION, form, Role.ANY); + return identifier != null && identifier.getValue() != null ? identifier.getValue().toString() : null; + } + + /** + * Folder name with illegal characters replaced, no leading or trailing dots or spaces (not allowed on Windows), + * and a maximum length. Reserved Windows device names get a trailing underscore. + */ + static String clean(String name, String fallback) + { + if(name == null || name.isBlank()) + { + return fallback; + } + + String cleaned = StringUtils.replaceIllegalCharacters(name.trim()); + + if(cleaned.length() > MAX_FOLDER_NAME_LENGTH) + { + cleaned = cleaned.substring(0, MAX_FOLDER_NAME_LENGTH); + } + + cleaned = cleaned.replaceAll("^[.\\s]+|[.\\s]+$", ""); + + if(cleaned.isEmpty()) + { + return fallback; + } + + if(cleaned.toUpperCase(Locale.ROOT).matches("CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9]")) + { + cleaned = cleaned + "_"; + } + + return cleaned; + } +} diff --git a/src/test/java/io/github/dsheirer/database/ageoff/AgeOffFolderTest.java b/src/test/java/io/github/dsheirer/database/ageoff/AgeOffFolderTest.java new file mode 100644 index 00000000..523f9827 --- /dev/null +++ b/src/test/java/io/github/dsheirer/database/ageoff/AgeOffFolderTest.java @@ -0,0 +1,72 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.database.ageoff; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Age-off removes the recording subfolders that become empty, only inside the recordings folder. + */ +public class AgeOffFolderTest +{ + @TempDir + Path mTempDir; + + @Test + public void emptyFoldersAreRemovedUpToTheRecordingsFolder() throws Exception + { + Path root = mTempDir.resolve("recordings"); + Path day = Files.createDirectories(root.resolve("Metro").resolve("TG_1001").resolve("2026-11-22")); + Path otherDay = Files.createDirectories(root.resolve("Metro").resolve("TG_1002").resolve("2026-11-22")); + Path first = Files.writeString(day.resolve("a.mp3"), "a"); + Path second = Files.writeString(day.resolve("b.mp3"), "b"); + Files.writeString(otherDay.resolve("c.mp3"), "c"); + + assertTrue(AgeOffManager.deleteRecording(first.toString(), root)); + assertTrue(Files.isDirectory(day), "folder still has a recording"); + + assertTrue(AgeOffManager.deleteRecording(second.toString(), root)); + assertFalse(Files.exists(day), "empty date folder removed"); + assertFalse(Files.exists(day.getParent()), "empty talkgroup folder removed"); + assertTrue(Files.isDirectory(root.resolve("Metro")), "system folder has another talkgroup"); + assertTrue(Files.isDirectory(root), "recordings folder is never removed"); + } + + @Test + public void foldersOutsideTheRecordingsFolderAreKept() throws Exception + { + Path root = Files.createDirectories(mTempDir.resolve("recordings")); + Path elsewhere = Files.createDirectories(mTempDir.resolve("old").resolve("2025-01-01")); + Path file = Files.writeString(elsewhere.resolve("a.wav"), "a"); + + assertTrue(AgeOffManager.deleteRecording(file.toString(), root)); + assertTrue(Files.isDirectory(elsewhere), "folder outside the recordings folder kept"); + + Path flat = Files.writeString(root.resolve("b.mp3"), "b"); + assertTrue(AgeOffManager.deleteRecording(flat.toString(), root)); + assertTrue(Files.isDirectory(root)); + } +} diff --git a/src/test/java/io/github/dsheirer/record/RecordingFoldersTest.java b/src/test/java/io/github/dsheirer/record/RecordingFoldersTest.java new file mode 100644 index 00000000..2de0507f --- /dev/null +++ b/src/test/java/io/github/dsheirer/record/RecordingFoldersTest.java @@ -0,0 +1,142 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ +package io.github.dsheirer.record; + +import io.github.dsheirer.identifier.Identifier; +import io.github.dsheirer.identifier.IdentifierCollection; +import io.github.dsheirer.identifier.configuration.ChannelNameConfigurationIdentifier; +import io.github.dsheirer.identifier.configuration.SiteConfigurationIdentifier; +import io.github.dsheirer.identifier.configuration.SystemConfigurationIdentifier; +import io.github.dsheirer.identifier.patch.PatchGroup; +import io.github.dsheirer.module.decode.p25.identifier.patch.APCO25PatchGroup; +import io.github.dsheirer.module.decode.p25.identifier.radio.APCO25RadioIdentifier; +import io.github.dsheirer.module.decode.p25.identifier.talkgroup.APCO25Talkgroup; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Recording subfolders for each folder layout + */ +public class RecordingFoldersTest +{ + private static final long TIME = LocalDateTime.of(2026, 11, 22, 14, 5, 0).atZone(ZoneId.systemDefault()) + .toInstant().toEpochMilli(); + private static final RecordingFolders.CallFolders FOLDERS = + new RecordingFolders.CallFolders("Metro", "Site 1", "Control A", "TG_1001"); + + private static List names(RecordingFolderLayout layout, boolean hour) + { + return RecordingFolders.getFolderNames(layout, hour, FOLDERS, TIME); + } + + @Test + public void layouts() + { + assertEquals(List.of(), names(RecordingFolderLayout.NONE, true), "no subfolders, hour ignored"); + assertEquals(List.of("2026-11-22"), names(RecordingFolderLayout.DATE, false)); + assertEquals(List.of("2026-11-22", "Metro"), names(RecordingFolderLayout.DATE_SYSTEM, false)); + assertEquals(List.of("Metro", "2026-11-22"), names(RecordingFolderLayout.SYSTEM_DATE, false)); + assertEquals(List.of("Metro", "Site-1", "2026-11-22"), names(RecordingFolderLayout.SYSTEM_SITE_DATE, false)); + assertEquals(List.of("Metro", "Control-A", "2026-11-22"), names(RecordingFolderLayout.SYSTEM_CHANNEL_DATE, + false)); + assertEquals(List.of("Metro", "TG_1001", "2026-11-22"), names(RecordingFolderLayout.SYSTEM_TALKGROUP_DATE, + false)); + assertEquals(List.of("TG_1001", "2026-11-22"), names(RecordingFolderLayout.TALKGROUP_DATE, false)); + } + + @Test + public void hourFolderIsBelowDate() + { + assertEquals(List.of("Metro", "2026-11-22", "14"), names(RecordingFolderLayout.SYSTEM_DATE, true)); + assertEquals(List.of("2026-11-22", "14", "Metro"), names(RecordingFolderLayout.DATE_SYSTEM, true)); + } + + @Test + public void missingNamesUseFallbacks() + { + RecordingFolders.CallFolders empty = new RecordingFolders.CallFolders(null, " ", "..", null); + assertEquals(List.of("Unknown-System", "Unknown-Site", "2026-11-22"), + RecordingFolders.getFolderNames(RecordingFolderLayout.SYSTEM_SITE_DATE, false, empty, TIME)); + assertEquals(List.of("Unknown-System", "Unknown-Channel", "2026-11-22"), + RecordingFolders.getFolderNames(RecordingFolderLayout.SYSTEM_CHANNEL_DATE, false, empty, TIME)); + assertEquals(List.of("Unknown", "2026-11-22"), + RecordingFolders.getFolderNames(RecordingFolderLayout.TALKGROUP_DATE, false, empty, TIME)); + } + + @Test + public void fileNameLeavesOutFolderNames() + { + for(RecordingFolderLayout layout: RecordingFolderLayout.values()) + { + List folders = names(layout, false); + assertEquals(folders.contains("Metro"), layout.hasSystemFolder(), layout + " system"); + assertEquals(folders.contains("Site-1"), layout.hasSiteFolder(), layout + " site"); + assertEquals(folders.contains("Control-A"), layout.hasChannelFolder(), layout + " channel"); + } + } + + @Test + public void namesAreSafeFolderNames() + { + assertEquals("Fire-Ops", RecordingFolders.clean("Fire/Ops", "x")); + assertEquals("a-b-c", RecordingFolders.clean("a\\b:c", "x")); + assertEquals("Metro", RecordingFolders.clean(" .Metro. ", "x")); + assertEquals("con_", RecordingFolders.clean("con", "x"), "reserved Windows name"); + assertEquals(64, RecordingFolders.clean("A".repeat(100), "x").length()); + assertEquals("x", RecordingFolders.clean("...", "x")); + } + + private static IdentifierCollection identifiers(Identifier... identifiers) + { + List list = new ArrayList<>(List.of(identifiers)); + list.add(new SystemConfigurationIdentifier("Metro")); + list.add(new SiteConfigurationIdentifier("Site 1")); + list.add(new ChannelNameConfigurationIdentifier("Control A")); + return new IdentifierCollection(list); + } + + @Test + public void talkgroupFolderFromIdentifiers() + { + RecordingFolders.CallFolders group = RecordingFolders.CallFolders.from(identifiers( + APCO25Talkgroup.create(1001), APCO25RadioIdentifier.createFrom(5551212))); + assertEquals(new RecordingFolders.CallFolders("Metro", "Site 1", "Control A", "TG_1001"), group); + + PatchGroup patchGroup = new PatchGroup(APCO25Talkgroup.create(500)); + patchGroup.addPatchedTalkgroup(APCO25Talkgroup.create(1001)); + assertEquals("PATCH_500", RecordingFolders.CallFolders.from(identifiers( + APCO25PatchGroup.create(patchGroup))).talkgroup()); + + assertEquals("RADIO_1234", RecordingFolders.CallFolders.from(identifiers( + APCO25RadioIdentifier.createTo(1234), APCO25RadioIdentifier.createFrom(5551212))).talkgroup(), + "private call: TO radio"); + assertEquals("RADIO_5551212", RecordingFolders.CallFolders.from(identifiers( + APCO25RadioIdentifier.createFrom(5551212))).talkgroup(), "no TO: FROM radio"); + assertEquals("Control A", RecordingFolders.CallFolders.from(identifiers()).talkgroup(), + "no identifiers: channel name"); + assertEquals(new RecordingFolders.CallFolders(null, null, null, null), + RecordingFolders.CallFolders.from(null)); + } +}