mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-23 16:28:51 -05:00
* expose file media.types entry as {mediaType} binding
This commit is contained in:
parent
1525623ccd
commit
30d3134f81
@ -1,7 +1,5 @@
|
||||
|
||||
package net.sourceforge.filebot;
|
||||
|
||||
|
||||
import static java.util.Collections.*;
|
||||
import static net.sourceforge.tuned.XPathUtilities.*;
|
||||
|
||||
@ -18,75 +16,81 @@ import net.sourceforge.tuned.FileUtilities.ExtensionFileFilter;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
|
||||
public class MediaTypes {
|
||||
|
||||
|
||||
private static final MediaTypes defaultInstance = parseDefault();
|
||||
|
||||
|
||||
|
||||
private static MediaTypes parseDefault() {
|
||||
try {
|
||||
Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(MediaTypes.class.getResourceAsStream("media.types"));
|
||||
Map<String, List<String>> types = new HashMap<String, List<String>>();
|
||||
|
||||
|
||||
for (Node it : getChildren("type", dom.getFirstChild())) {
|
||||
List<String> extensions = new ArrayList<String>(2);
|
||||
for (Node ie : getChildren("extension", it)) {
|
||||
extensions.add(getTextContent(ie));
|
||||
}
|
||||
|
||||
|
||||
types.put(getAttribute("name", it), extensions);
|
||||
}
|
||||
|
||||
|
||||
return new MediaTypes(types);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Map<String, List<String>> types;
|
||||
private Map<String, ExtensionFileFilter> filters = synchronizedMap(new HashMap<String, ExtensionFileFilter>());
|
||||
|
||||
|
||||
|
||||
public MediaTypes(Map<String, List<String>> types) {
|
||||
this.types = types;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<String> getExtensionList(String name) {
|
||||
List<String> list = new ArrayList<String>();
|
||||
|
||||
|
||||
for (Entry<String, List<String>> type : types.entrySet()) {
|
||||
if (type.getKey().startsWith(name)) {
|
||||
list.addAll(type.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ExtensionFileFilter getFilter(String name) {
|
||||
ExtensionFileFilter filter = filters.get(name);
|
||||
|
||||
|
||||
if (filter == null) {
|
||||
filter = new ExtensionFileFilter(getExtensionList(name));
|
||||
filters.put(name, filter);
|
||||
}
|
||||
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static MediaTypes getDefault() {
|
||||
return defaultInstance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Map<String, List<String>> getTypes() {
|
||||
return types;
|
||||
}
|
||||
|
||||
public String getMediaType(String extension) {
|
||||
for (Entry<String, List<String>> it : getTypes().entrySet()) {
|
||||
if (it.getValue().contains(extension)) {
|
||||
return it.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ExtensionFileFilter getDefaultFilter(String name) {
|
||||
return defaultInstance.getFilter(name);
|
||||
}
|
||||
|
||||
|
||||
// some convenience filters
|
||||
public static final ExtensionFileFilter AUDIO_FILES = getDefaultFilter("audio");
|
||||
public static final ExtensionFileFilter VIDEO_FILES = getDefaultFilter("video");
|
||||
|
@ -27,6 +27,7 @@ import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import net.sourceforge.filebot.Cache;
|
||||
import net.sourceforge.filebot.MediaTypes;
|
||||
import net.sourceforge.filebot.WebServices;
|
||||
import net.sourceforge.filebot.hash.HashType;
|
||||
import net.sourceforge.filebot.media.MetaAttributes;
|
||||
@ -626,6 +627,11 @@ public class MediaBindingBean {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Define("mediaType")
|
||||
public List<String> getMediaType() throws Exception {
|
||||
return asList(MediaTypes.getDefault().getMediaType(getExtension()).split("/")); // format engine does not allow / in binding value
|
||||
}
|
||||
|
||||
@Define("file")
|
||||
public File getMediaFile() {
|
||||
return mediaFile;
|
||||
|
@ -225,7 +225,7 @@ public class FormatDialog extends JDialog {
|
||||
|
||||
// initialize window properties
|
||||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||
setSize(620, 445);
|
||||
setSize(650, 450);
|
||||
|
||||
// initialize data
|
||||
setState(initMode, lockOnBinding != null ? lockOnBinding : restoreSample(initMode), lockOnBinding != null);
|
||||
|
Loading…
Reference in New Issue
Block a user