forked from j62/ctbrec
Implement Renamer post-processor
This commit is contained in:
parent
17a32cd928
commit
bdcf1bee00
|
@ -0,0 +1,24 @@
|
||||||
|
package ctbrec.ui.settings;
|
||||||
|
|
||||||
|
import ctbrec.recorder.postprocessing.PostProcessor;
|
||||||
|
import ctbrec.recorder.postprocessing.Renamer;
|
||||||
|
import ctbrec.ui.settings.api.Category;
|
||||||
|
import ctbrec.ui.settings.api.Preferences;
|
||||||
|
import ctbrec.ui.settings.api.Setting;
|
||||||
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
|
|
||||||
|
public class RenamerPaneFactory extends AbstractPostProcessingPaneFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Preferences doCreatePostProcessorPane(PostProcessor pp) {
|
||||||
|
SimpleStringProperty fileTemplate = new SimpleStringProperty(null, Renamer.FILE_NAME_TEMPLATE, pp.getConfig().getOrDefault(Renamer.FILE_NAME_TEMPLATE, Renamer.DEFAULT));
|
||||||
|
properties.add(fileTemplate);
|
||||||
|
|
||||||
|
return Preferences.of(new MapPreferencesStorage(),
|
||||||
|
Category.of(pp.getName(),
|
||||||
|
Setting.of("File name", fileTemplate)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,16 +0,0 @@
|
||||||
package ctbrec.recorder.postprocessing;
|
|
||||||
|
|
||||||
import ctbrec.Recording;
|
|
||||||
|
|
||||||
public class RecordingRenamer extends AbstractPostProcessor {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "rename";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void postprocess(Recording rec) {
|
|
||||||
// TODO rename
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,116 @@
|
||||||
|
package ctbrec.recorder.postprocessing;
|
||||||
|
import static java.util.Optional.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.common.io.Files;
|
||||||
|
|
||||||
|
import ctbrec.Recording;
|
||||||
|
|
||||||
|
public class Renamer extends AbstractPostProcessor {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(Renamer.class);
|
||||||
|
public static final String FILE_NAME_TEMPLATE = "filename.template";
|
||||||
|
public static final String DEFAULT = "${modelSanitizedName}_${localDateTime}.${fileSuffix}";
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private String[] placeHolders = {
|
||||||
|
"${modelName}",
|
||||||
|
"${modelDisplayName}",
|
||||||
|
"${modelSanitizedName}",
|
||||||
|
"${siteName}",
|
||||||
|
"${siteSanitizedName}",
|
||||||
|
"${utcDateTime}",
|
||||||
|
"${localDateTime}",
|
||||||
|
"${epochSeconds}",
|
||||||
|
"${fileSuffix}",
|
||||||
|
"${modelNotes}"
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "rename";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postprocess(Recording rec) throws IOException {
|
||||||
|
String filenameTemplate = getConfig().getOrDefault(FILE_NAME_TEMPLATE, DEFAULT);
|
||||||
|
String filename = filenameTemplate
|
||||||
|
.replace("${modelName}", ofNullable(rec.getModel().getName()).orElse("modelName"))
|
||||||
|
.replace("${modelDisplayName}", ofNullable(rec.getModel().getDisplayName()).orElse("displayName"))
|
||||||
|
.replace("${modelSanitizedName}", ofNullable(rec.getModel().getSanitizedNamed()).orElse("sanitizedName"))
|
||||||
|
.replace("${siteName}", rec.getModel().getSite().getName())
|
||||||
|
.replace("${siteSanitizedName}", getSanitizedSiteName(rec))
|
||||||
|
.replace("${fileSuffix}", getFileSuffix(rec))
|
||||||
|
.replace("${epochSeconds}", Long.toString(rec.getStartDate().getEpochSecond()))
|
||||||
|
;
|
||||||
|
|
||||||
|
filename = replaceUtcDateTime(rec, filename);
|
||||||
|
filename = replaceLocalDateTime(rec, filename);
|
||||||
|
|
||||||
|
File src = rec.getPostProcessedFile();
|
||||||
|
File target = new File(src.getParentFile(), filename);
|
||||||
|
LOG.info("Renaming {} to {}", src.getName(), target.getName());
|
||||||
|
Files.copy(rec.getPostProcessedFile(), target);
|
||||||
|
//Files.move(rec.getPostProcessedFile(), target);
|
||||||
|
rec.setPostProcessedFile(target);
|
||||||
|
rec.getAssociatedFiles().add(target.getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String replaceUtcDateTime(Recording rec, String filename) {
|
||||||
|
return replaceDateTime(rec, filename, "utcDateTime", ZoneOffset.UTC);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String replaceLocalDateTime(Recording rec, String filename) {
|
||||||
|
return replaceDateTime(rec, filename, "localDateTime", ZoneId.systemDefault());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String replaceDateTime(Recording rec, String filename, String placeHolder, ZoneId zone) {
|
||||||
|
String pattern = "yyyy-mm-dd_HH-mm-ss";
|
||||||
|
Matcher m = Pattern.compile("\\$\\{" + placeHolder + "(?:\\((.*?)\\))?\\}").matcher(filename);
|
||||||
|
if (m.find()) {
|
||||||
|
String p = m.group(1);
|
||||||
|
if (p != null) {
|
||||||
|
pattern = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String formattedDate = getDateTime(rec, pattern, zone);
|
||||||
|
return m.replaceAll(formattedDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getDateTime(Recording rec, String pattern, ZoneId zone) {
|
||||||
|
return DateTimeFormatter.ofPattern(pattern)
|
||||||
|
.withLocale(Locale.getDefault())
|
||||||
|
.withZone(zone)
|
||||||
|
.format(rec.getStartDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
private CharSequence getFileSuffix(Recording rec) {
|
||||||
|
String filename = rec.getPostProcessedFile().getName();
|
||||||
|
return filename.substring(filename.lastIndexOf('.') + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private CharSequence getSanitizedSiteName(Recording rec) {
|
||||||
|
return rec.getModel().getSite().getName().replace(' ', '_').replace('\\', '_').replace('/', '_');
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String s = getName();
|
||||||
|
if (getConfig().containsKey(FILE_NAME_TEMPLATE)) {
|
||||||
|
s += " [" + getConfig().get(FILE_NAME_TEMPLATE) + ']';
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue