Support "Generic File" renaming via the command-line tools by using `--db xattr -non-strict` so that only xattr-tagged files are processed in strict mode, but any generic file can be processed in non-strict mode (File object is used in absence of Episode/Movie object)

This commit is contained in:
Reinhard Pointner 2016-10-05 15:00:03 +08:00
parent bbc1bef181
commit 2d0a16688e
3 changed files with 23 additions and 28 deletions

View File

@ -115,8 +115,8 @@ public class CmdlineOperations implements CmdlineInterface {
return renameMusic(files, action, conflictAction, outputDir, format, getMusicIdentificationService(db)); return renameMusic(files, action, conflictAction, outputDir, format, getMusicIdentificationService(db));
} }
if (XattrMetaData.getName().equalsIgnoreCase(db)) { if (XattrMetaData.getIdentifier().equalsIgnoreCase(db)) {
return renameByMetaData(files, action, conflictAction, outputDir, format, filter, XattrMetaData); return renameFiles(files, action, conflictAction, outputDir, format, XattrMetaData, filter, strict);
} }
// auto-detect mode for each fileset // auto-detect mode for each fileset
@ -534,23 +534,23 @@ public class CmdlineOperations implements CmdlineInterface {
return renameAll(renameMap, renameAction, conflictAction, null); return renameAll(renameMap, renameAction, conflictAction, null);
} }
public List<File> renameByMetaData(Collection<File> files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, ExpressionFilter filter, XattrMetaInfoProvider service) throws Exception { public List<File> renameFiles(Collection<File> files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, XattrMetaInfoProvider service, ExpressionFilter filter, boolean strict) throws Exception {
log.config(format("Rename files using [%s]", service.getName())); log.config(format("Rename files using [%s]", service.getName()));
// force sort order // match to xattr metadata object or the file itself
List<File> selection = sortByUniquePath(files); Map<File, Object> matches = service.match(files, strict);
Map<File, File> renameMap = new LinkedHashMap<File, File>(); Map<File, File> renameMap = new LinkedHashMap<File, File>();
for (Entry<File, Object> it : service.getMetaData(selection).entrySet()) { for (Entry<File, Object> it : matches.entrySet()) {
MediaBindingBean bindingBean = new MediaBindingBean(it.getValue(), it.getKey()); MediaBindingBean bindingBean = new MediaBindingBean(it.getValue(), it.getKey(), matches);
if (filter == null || filter.matches(bindingBean)) { if (filter == null || filter.matches(bindingBean)) {
String newName = (format != null) ? format.format(bindingBean) : validateFileName(it.getValue().toString()); String newName = format != null ? format.format(bindingBean) : bindingBean.getInfoObject() instanceof File ? bindingBean.getInfoObject().toString() : validateFileName(bindingBean.getInfoObject().toString());
renameMap.put(it.getKey(), getDestinationFile(it.getKey(), newName, outputDir)); renameMap.put(it.getKey(), getDestinationFile(it.getKey(), newName, outputDir));
} }
} }
// rename files according to xattr metadata objects
return renameAll(renameMap, renameAction, conflictAction, null); return renameAll(renameMap, renameAction, conflictAction, null);
} }

View File

@ -1,8 +1,7 @@
package net.filebot.media; package net.filebot.media;
import static net.filebot.media.XattrMetaInfo.*;
import java.io.File; import java.io.File;
import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -22,13 +21,19 @@ public class XattrMetaInfoProvider implements Datasource {
return null; return null;
} }
public Map<File, Object> getMetaData(Iterable<File> files) { public Map<File, Object> match(Collection<File> files, boolean strict) {
// enable xattr regardless of -DuseExtendedFileAttributes system properties
XattrMetaInfo xattr = new XattrMetaInfo(true, false);
Map<File, Object> result = new LinkedHashMap<File, Object>(); Map<File, Object> result = new LinkedHashMap<File, Object>();
for (File f : files) { for (File f : files) {
Object metaObject = xattr.getMetaInfo(f); Object object = xattr.getMetaInfo(f);
if (metaObject != null) {
result.put(f, metaObject); if (object != null) {
result.put(f, object);
} else if (!strict) {
result.put(f, f);
} }
} }

View File

@ -314,21 +314,11 @@ public class RenamePanel extends JComponent {
try { try {
if (namesList.getModel().isEmpty()) { if (namesList.getModel().isEmpty()) {
withWaitCursor(evt.getSource(), () -> { withWaitCursor(evt.getSource(), () -> {
// try to read xattr from all files // match to xattr metadata object or the file itself
Map<File, Object> xattr = WebServices.XattrMetaData.getMetaData(renameModel.files()); Map<File, Object> xattr = WebServices.XattrMetaData.match(renameModel.files(), false);
// upper list is based on xattr metadata
List<File> files = new ArrayList<File>(xattr.keySet());
List<Object> objects = new ArrayList<Object>(xattr.values());
// lower list is just the fallback file object
renameModel.files().stream().filter(f -> !xattr.containsKey(f)).forEach(f -> {
files.add(f);
objects.add(f);
});
renameModel.clear(); renameModel.clear();
renameModel.addAll(objects, files); renameModel.addAll(xattr.values(), xattr.keySet());
}); });
} else { } else {
int index = namesList.getListComponent().getSelectedIndex(); int index = namesList.getListComponent().getSelectedIndex();