From a91819c2ca15cbdca03651763abe0a5b01f5a3ce Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Thu, 24 Jan 2019 19:00:02 +0100 Subject: [PATCH] Extend the recording name to include seconds and milliseconds This is necessary, because there are models, who stream on different sites with the same name as mentioned in #141. In that case it can happen that a recording for each site would be started within the same minute and one recording would overwrite the other. --- common/src/main/java/ctbrec/Config.java | 5 +++-- .../main/java/ctbrec/recorder/LocalRecorder.java | 13 ++++++------- .../java/ctbrec/recorder/download/HlsDownload.java | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/ctbrec/Config.java b/common/src/main/java/ctbrec/Config.java index 9170a5f1..085ab8ba 100644 --- a/common/src/main/java/ctbrec/Config.java +++ b/common/src/main/java/ctbrec/Config.java @@ -33,6 +33,7 @@ public class Config { private String filename; private List sites; private File configDir; + public static final String RECORDING_DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss_SSS"; private Config(List sites) throws FileNotFoundException, IOException { this.sites = sites; @@ -134,7 +135,7 @@ public class Config { public File getFileForRecording(Model model) { File dirForRecording = getDirForRecording(model); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm"); + SimpleDateFormat sdf = new SimpleDateFormat(RECORDING_DATE_FORMAT); String startTime = sdf.format(new Date()); File targetFile = new File(dirForRecording, model.getName() + '_' + startTime + ".ts"); return targetFile; @@ -146,7 +147,7 @@ public class Config { return new File(getSettings().recordingsDir, model.getName()); case ONE_PER_RECORDING: File modelDir = new File(getSettings().recordingsDir, model.getName()); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm"); + SimpleDateFormat sdf = new SimpleDateFormat(RECORDING_DATE_FORMAT); String startTime = sdf.format(new Date()); return new File(modelDir, startTime); case FLAT: diff --git a/common/src/main/java/ctbrec/recorder/LocalRecorder.java b/common/src/main/java/ctbrec/recorder/LocalRecorder.java index 52b49b6f..914f2335 100644 --- a/common/src/main/java/ctbrec/recorder/LocalRecorder.java +++ b/common/src/main/java/ctbrec/recorder/LocalRecorder.java @@ -66,7 +66,6 @@ public class LocalRecorder implements Recorder { private static final transient Logger LOG = LoggerFactory.getLogger(LocalRecorder.class); private static final boolean IGNORE_CACHE = true; - private static final String DATE_FORMAT = "yyyy-MM-dd_HH-mm"; private List models = Collections.synchronizedList(new ArrayList<>()); private Map recordingProcesses = Collections.synchronizedMap(new HashMap<>()); @@ -466,17 +465,17 @@ public class LocalRecorder implements Recorder { private List listMergedRecordings() { File recordingsDir = new File(config.getSettings().recordingsDir); List possibleRecordings = new LinkedList<>(); - listRecursively(recordingsDir, possibleRecordings, (dir, name) -> name.matches(".*?_\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}\\.(ts|mp4)")); - SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); + listRecursively(recordingsDir, possibleRecordings, (dir, name) -> name.matches(".*?_\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}_\\d{3}\\.(ts|mp4)")); + SimpleDateFormat sdf = new SimpleDateFormat(Config.RECORDING_DATE_FORMAT); List recordings = new ArrayList<>(); for (File ts: possibleRecordings) { try { String filename = ts.getName(); int extLength = filename.length() - filename.lastIndexOf('.'); - String dateString = filename.substring(filename.length() - extLength - DATE_FORMAT.length(), filename.length() - extLength); + String dateString = filename.substring(filename.length() - extLength - Config.RECORDING_DATE_FORMAT.length(), filename.length() - extLength); Date startDate = sdf.parse(dateString); Recording recording = new Recording(); - recording.setModelName(filename.substring(0, filename.length() - extLength - 1 - DATE_FORMAT.length())); + recording.setModelName(filename.substring(0, filename.length() - extLength - 1 - Config.RECORDING_DATE_FORMAT.length())); recording.setStartDate(Instant.ofEpochMilli(startDate.getTime())); String path = ts.getAbsolutePath().replace(config.getSettings().recordingsDir, ""); if(!path.startsWith("/")) { @@ -541,11 +540,11 @@ public class LocalRecorder implements Recorder { // start going over valid directories for (File rec : recordingsDirs) { - SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); + SimpleDateFormat sdf = new SimpleDateFormat(Config.RECORDING_DATE_FORMAT); if (rec.isDirectory()) { try { // ignore directories, which are probably not created by ctbrec - if (rec.getName().length() != DATE_FORMAT.length()) { + if (rec.getName().length() != Config.RECORDING_DATE_FORMAT.length()) { continue; } // ignore empty directories diff --git a/common/src/main/java/ctbrec/recorder/download/HlsDownload.java b/common/src/main/java/ctbrec/recorder/download/HlsDownload.java index 9eef5d7c..11a0d67c 100644 --- a/common/src/main/java/ctbrec/recorder/download/HlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/HlsDownload.java @@ -56,7 +56,7 @@ public class HlsDownload extends AbstractHlsDownload { running = true; startTime = Instant.now(); super.model = model; - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm"); + SimpleDateFormat sdf = new SimpleDateFormat(Config.RECORDING_DATE_FORMAT); String startTime = sdf.format(new Date()); Path modelDir = FileSystems.getDefault().getPath(config.getSettings().recordingsDir, model.getName()); downloadDir = FileSystems.getDefault().getPath(modelDir.toString(), startTime);