Fixing issue #353 Forward Slash in site or channel name causes recording error (#448)

* Fixing issue #353
Moved replaceIllegalCharacters method into StringUtils.
Modified classes that write out to disk to call this method when saving a file.

* Implementing suggested changes from comments in PR #448
This commit is contained in:
whelgeson
2018-12-16 18:10:56 -06:00
committed by Denny Sheirer
parent 4a4d1d4b2e
commit 72fe72c833
6 changed files with 27 additions and 28 deletions

View File

@@ -41,6 +41,7 @@ import io.github.dsheirer.source.SourceException;
import io.github.dsheirer.source.SourceManager;
import io.github.dsheirer.source.SourceType;
import io.github.dsheirer.source.config.SourceConfigTuner;
import io.github.dsheirer.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -286,7 +287,8 @@ public class ChannelProcessingManager implements ChannelEventListener
channel.getDecodeConfiguration().getDecoderType()));
/* Setup event logging */
List<Module> loggers = mEventLogManager.getLoggers(channel.getEventLogConfiguration(), channel.getName());
String cleanedChannelName = StringUtils.replaceIllegalCharacters(channel.getName());
List<Module> loggers = mEventLogManager.getLoggers(channel.getEventLogConfiguration(), cleanedChannelName);
if(!loggers.isEmpty())
{

View File

@@ -35,9 +35,6 @@ public abstract class EventLogger extends Module
{
private final static Logger mLog = LoggerFactory.getLogger(EventLogger.class);
private static final String[] ILLEGAL_FILENAME_CHARACTERS = {"#", "%", "&", "{", "}", "\\", "<", ">",
"*", "?", "/", " ", "$", "!", "'", "\"", ":", "@", "+", "`", "|", "="};
private Path mLogDirectory;
private String mFileNameSuffix;
private String mLogFileName;
@@ -75,7 +72,7 @@ public abstract class EventLogger extends Module
sb.append(File.separator);
sb.append(TimeStamp.getTimeStamp("_"));
sb.append("_");
sb.append(replaceIllegalCharacters(mFileNameSuffix));
sb.append(mFileNameSuffix);
mLogFileName = sb.toString();
@@ -92,19 +89,6 @@ public abstract class EventLogger extends Module
}
}
/**
* Replaces any illegal filename characters in the proposed filename
*/
private String replaceIllegalCharacters(String filename)
{
for(String illegalCharacter : ILLEGAL_FILENAME_CHARACTERS)
{
filename = filename.replace(illegalCharacter, "_");
}
return filename;
}
public void stop()
{
if(mLogFile != null)

View File

@@ -23,6 +23,7 @@ import io.github.dsheirer.sample.IOverflowListener;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.OverflowableTransferQueue;
import io.github.dsheirer.sample.buffer.ReusableAudioPacket;
import io.github.dsheirer.util.StringUtils;
import io.github.dsheirer.util.ThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -203,14 +204,11 @@ public class RecorderManager implements Listener<ReusableAudioPacket>
*/
private String getFilePrefix(ReusableAudioPacket packet)
{
StringBuilder sb = new StringBuilder();
sb.append(SystemProperties.getInstance().getApplicationFolder("recordings"));
sb.append(File.separator);
Path baseFilePath = SystemProperties.getInstance().getApplicationFolder("recordings");
Metadata metadata = packet.getMetadata();
StringBuilder sb = new StringBuilder();
sb.append(metadata.hasChannelConfigurationSystem() ? metadata.getChannelConfigurationSystem() + "_" : "");
sb.append(metadata.hasChannelConfigurationSite() ? metadata.getChannelConfigurationSite() + "_" : "");
sb.append(metadata.hasChannelConfigurationName() ? metadata.getChannelConfigurationName() + "_" : "");
@@ -225,7 +223,8 @@ public class RecorderManager implements Listener<ReusableAudioPacket>
}
}
return sb.toString();
String cleanedFileName = StringUtils.replaceIllegalCharacters(sb.toString());
return baseFilePath.resolve(cleanedFileName).toString();
}
public Path getRecordingBasePath()
@@ -240,7 +239,7 @@ public class RecorderManager implements Listener<ReusableAudioPacket>
{
StringBuilder sb = new StringBuilder();
sb.append(getRecordingBasePath());
sb.append(File.separator).append(channelName).append("_baseband");
sb.append(File.separator).append(StringUtils.replaceIllegalCharacters(channelName)).append("_baseband");
return new ComplexBufferWaveRecorder(BASEBAND_SAMPLE_RATE, sb.toString());
}

View File

@@ -20,6 +20,7 @@ import io.github.dsheirer.module.Module;
import io.github.dsheirer.sample.Listener;
import io.github.dsheirer.sample.buffer.IReusableByteBufferListener;
import io.github.dsheirer.sample.buffer.ReusableByteBuffer;
import io.github.dsheirer.util.StringUtils;
import io.github.dsheirer.util.TimeStamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -119,8 +120,8 @@ public class BinaryRecorder extends Module implements IReusableByteBufferListene
sb.append("_");
sb.append(mRecordingIdentifier.trim());
sb.append(".bits");
return mBaseRecordingPath.resolve(sb.toString());
String cleaned = StringUtils.replaceIllegalCharacters(sb.toString());
return mBaseRecordingPath.resolve(cleaned);
}
@Override

View File

@@ -89,7 +89,6 @@ public class ComplexBufferWaveRecorder extends Module implements IReusableComple
sb.append("_");
sb.append(TimeStamp.getTimeStamp("_"));
sb.append(".wav");
mFile = Paths.get(sb.toString());
mWriter = new ReusableBufferWaveWriter(mAudioFormat, mFile);

View File

@@ -20,6 +20,9 @@ package io.github.dsheirer.util;
public class StringUtils
{
private static final String[] ILLEGAL_FILENAME_CHARACTERS = {"#", "%", "&", "{", "}", "\\", "<", ">",
"*", "?", "/", " ", "$", "!", "'", "\"", ":", "@", "+", "`", "|", "="};
/**
* Compares string a to b and returns true if a is non-null and non-empty, b is non-null and non-empty, and
* a is equal to b.
@@ -38,4 +41,15 @@ public class StringUtils
{
return a == null || a.isEmpty();
}
/**
* Replaces any illegal filename characters in the proposed filename
*/
public static String replaceIllegalCharacters(String filename) {
for (String illegalCharacter : ILLEGAL_FILENAME_CHARACTERS) {
filename = filename.replace(illegalCharacter, "_");
}
return filename;
}
}