1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 16:58:51 -05:00

Minor optimizations

This commit is contained in:
Reinhard Pointner 2016-10-09 03:14:09 +08:00
parent fb8d6bd64c
commit 6c98f249d1
6 changed files with 26 additions and 39 deletions

View File

@ -1,6 +1,7 @@
package net.filebot.media; package net.filebot.media;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -9,31 +10,32 @@ import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime; import java.nio.file.attribute.FileTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream;
import com.cedarsoftware.util.io.JsonReader; import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter; import com.cedarsoftware.util.io.JsonWriter;
import net.filebot.MetaAttributeView; import net.filebot.MetaAttributeView;
import net.filebot.vfs.SimpleFileInfo;
import net.filebot.web.AudioTrack;
import net.filebot.web.Episode;
import net.filebot.web.Movie;
import net.filebot.web.MoviePart;
import net.filebot.web.MultiEpisode;
public class MetaAttributes { public class MetaAttributes {
public static final String FILENAME_KEY = "net.filebot.filename"; public static final String FILENAME_KEY = "net.filebot.filename";
public static final String METADATA_KEY = "net.filebot.metadata"; public static final String METADATA_KEY = "net.filebot.metadata";
public static final Map<String, String> JSON_TYPE_MAP = unmodifiableMap(Stream.of(Episode.class, MultiEpisode.class, Movie.class, MoviePart.class, AudioTrack.class, SimpleFileInfo.class).collect(toMap(Class::getName, Class::getSimpleName)));
private final BasicFileAttributeView fileAttributeView; private final BasicFileAttributeView fileAttributeView;
private final MetaAttributeView metaAttributeView; private final MetaAttributeView metaAttributeView;
private final Map<String, String> jsonTypeMap;
// compatibility constructor for sysinfo.groovy script
public MetaAttributes(File file) throws IOException { public MetaAttributes(File file) throws IOException {
this(file, emptyMap());
}
public MetaAttributes(File file, Map<String, String> jsonTypeMap) throws IOException {
this.metaAttributeView = new MetaAttributeView(file); this.metaAttributeView = new MetaAttributeView(file);
this.fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class); this.fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class);
this.jsonTypeMap = jsonTypeMap;
} }
public void setCreationDate(long millis) throws IOException { public void setCreationDate(long millis) throws IOException {
@ -58,7 +60,7 @@ public class MetaAttributes {
public void setObject(Object object) { public void setObject(Object object) {
try { try {
metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object, singletonMap(JsonWriter.TYPE_NAME_MAP, jsonTypeMap))); metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object, singletonMap(JsonWriter.TYPE_NAME_MAP, JSON_TYPE_MAP)));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -69,7 +71,7 @@ public class MetaAttributes {
String jsonObject = metaAttributeView.get(METADATA_KEY); String jsonObject = metaAttributeView.get(METADATA_KEY);
if (jsonObject != null && jsonObject.length() > 0) { if (jsonObject != null && jsonObject.length() > 0) {
Map<String, Object> options = new HashMap<String, Object>(2); Map<String, Object> options = new HashMap<String, Object>(2);
options.put(JsonReader.TYPE_NAME_MAP, jsonTypeMap); options.put(JsonReader.TYPE_NAME_MAP, JSON_TYPE_MAP);
// options must be a modifiable map // options must be a modifiable map
return JsonReader.jsonToJava(jsonObject, options); return JsonReader.jsonToJava(jsonObject, options);

View File

@ -1,27 +1,19 @@
package net.filebot.media; package net.filebot.media;
import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*; import static net.filebot.Logging.*;
import static net.filebot.Settings.*; import static net.filebot.Settings.*;
import static net.filebot.util.ExceptionUtilities.*; import static net.filebot.util.ExceptionUtilities.*;
import java.io.File; import java.io.File;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream;
import net.filebot.Cache; import net.filebot.Cache;
import net.filebot.CacheType; import net.filebot.CacheType;
import net.filebot.Resource; import net.filebot.Resource;
import net.filebot.WebServices; import net.filebot.WebServices;
import net.filebot.vfs.SimpleFileInfo;
import net.filebot.web.AudioTrack;
import net.filebot.web.Episode; import net.filebot.web.Episode;
import net.filebot.web.Movie; import net.filebot.web.Movie;
import net.filebot.web.MoviePart;
import net.filebot.web.MultiEpisode;
import net.filebot.web.SimpleDate; import net.filebot.web.SimpleDate;
public class XattrMetaInfo { public class XattrMetaInfo {
@ -34,8 +26,6 @@ public class XattrMetaInfo {
private final Cache xattrMetaInfoCache = Cache.getCache(MetaAttributes.METADATA_KEY, CacheType.Ephemeral); private final Cache xattrMetaInfoCache = Cache.getCache(MetaAttributes.METADATA_KEY, CacheType.Ephemeral);
private final Cache xattrOriginalNameCache = Cache.getCache(MetaAttributes.FILENAME_KEY, CacheType.Ephemeral); private final Cache xattrOriginalNameCache = Cache.getCache(MetaAttributes.FILENAME_KEY, CacheType.Ephemeral);
private final Map<String, String> jsonTypeMap = unmodifiableMap(Stream.of(Episode.class, MultiEpisode.class, Movie.class, MoviePart.class, AudioTrack.class, SimpleFileInfo.class).collect(toMap(Class::getName, Class::getSimpleName)));
public XattrMetaInfo(boolean useExtendedFileAttributes, boolean useCreationDate) { public XattrMetaInfo(boolean useExtendedFileAttributes, boolean useCreationDate) {
this.useExtendedFileAttributes = useExtendedFileAttributes; this.useExtendedFileAttributes = useExtendedFileAttributes;
this.useCreationDate = useCreationDate; this.useCreationDate = useCreationDate;
@ -86,11 +76,7 @@ public class XattrMetaInfo {
} }
private MetaAttributes xattr(File file) throws Exception { private MetaAttributes xattr(File file) throws Exception {
return new MetaAttributes(file, getJsonTypeMap()); return new MetaAttributes(file);
}
public Map<String, String> getJsonTypeMap() {
return jsonTypeMap;
} }
public synchronized void setMetaInfo(File file, Object model, String original) { public synchronized void setMetaInfo(File file, Object model, String original) {

View File

@ -228,7 +228,7 @@ class BindingDialog extends JDialog {
infoTextField.putClientProperty("model", info); infoTextField.putClientProperty("model", info);
infoTextField.setText(info == null ? "" : infoObjectFormat.format(info)); infoTextField.setText(info == null ? "" : infoObjectFormat.format(info));
infoTextField.setToolTipText(info == null ? "null" : "<html><pre>" + escapeHTML(asPrettyJsonString(info)) + "</pre></html>"); infoTextField.setToolTipText(info == null ? "null" : "<html><pre>" + escapeHTML(json(info, true)) + "</pre></html>");
} }
public Object getInfoObject() { public Object getInfoObject() {

View File

@ -7,7 +7,6 @@ import static javax.swing.SwingUtilities.*;
import static net.filebot.Logging.*; import static net.filebot.Logging.*;
import static net.filebot.Settings.*; import static net.filebot.Settings.*;
import static net.filebot.media.MediaDetection.*; import static net.filebot.media.MediaDetection.*;
import static net.filebot.media.XattrMetaInfo.*;
import static net.filebot.util.ExceptionUtilities.*; import static net.filebot.util.ExceptionUtilities.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.ui.LoadingOverlayPane.*; import static net.filebot.util.ui.LoadingOverlayPane.*;
@ -68,6 +67,7 @@ import net.filebot.UserFiles;
import net.filebot.WebServices; import net.filebot.WebServices;
import net.filebot.format.MediaBindingBean; import net.filebot.format.MediaBindingBean;
import net.filebot.mac.MacAppUtilities; import net.filebot.mac.MacAppUtilities;
import net.filebot.media.MetaAttributes;
import net.filebot.similarity.Match; import net.filebot.similarity.Match;
import net.filebot.ui.rename.FormatDialog.Mode; import net.filebot.ui.rename.FormatDialog.Mode;
import net.filebot.ui.rename.RenameModel.FormattedFuture; import net.filebot.ui.rename.RenameModel.FormattedFuture;
@ -362,8 +362,11 @@ public class RenamePanel extends JComponent {
installAction(this, WHEN_IN_FOCUSED_WINDOW, getKeyStroke(VK_F7, 0), newAction("Copy Debug Information", evt -> { installAction(this, WHEN_IN_FOCUSED_WINDOW, getKeyStroke(VK_F7, 0), newAction("Copy Debug Information", evt -> {
try { try {
withWaitCursor(evt.getSource(), () -> { withWaitCursor(evt.getSource(), () -> {
copyToClipboard(getDebugInfo()); String text = getDebugInfo();
if (text.length() > 0) {
copyToClipboard(text);
log.info("Match model has been copied to clipboard"); log.info("Match model has been copied to clipboard");
}
}); });
} catch (Exception e) { } catch (Exception e) {
debug.log(Level.WARNING, e, e::getMessage); debug.log(Level.WARNING, e, e::getMessage);
@ -617,12 +620,12 @@ public class RenamePanel extends JComponent {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Map<String, Object> options = new HashMap<String, Object>(2); Map<String, Object> options = new HashMap<String, Object>(2);
options.put(JsonWriter.TYPE_NAME_MAP, xattr.getJsonTypeMap()); options.put(JsonWriter.TYPE_NAME_MAP, MetaAttributes.JSON_TYPE_MAP);
options.put(JsonWriter.SKIP_NULL_FIELDS, true); options.put(JsonWriter.SKIP_NULL_FIELDS, true);
for (Match<Object, File> m : renameModel.matches()) { for (Match<Object, File> m : renameModel.matches()) {
Object v = m.getValue();
String f = getStructurePathTail(m.getCandidate()).getPath(); String f = getStructurePathTail(m.getCandidate()).getPath();
Object v = m.getValue();
// convert FastFile items // convert FastFile items
if (v instanceof File) { if (v instanceof File) {

View File

@ -24,12 +24,8 @@ public class JsonUtilities {
return JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true)); return JsonReader.jsonToJava(json.toString(), singletonMap(JsonReader.USE_MAPS, true));
} }
public static String asJsonString(Object object) { public static String json(Object object, boolean pretty) {
return JsonWriter.objectToJson(object); return JsonWriter.objectToJson(object, singletonMap(JsonWriter.PRETTY_PRINT, pretty));
}
public static String asPrettyJsonString(Object object) {
return JsonWriter.objectToJson(object, singletonMap(JsonWriter.PRETTY_PRINT, true));
} }
public static Map<?, ?> asMap(Object node) { public static Map<?, ?> asMap(Object node) {

View File

@ -53,9 +53,9 @@ public class TheTVDBClient extends AbstractEpisodeListProvider implements Artwor
return true; return true;
} }
protected Object postJson(String path, Object json) throws Exception { protected Object postJson(String path, Object object) throws Exception {
// curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'https://api.thetvdb.com/login' --data '{"apikey":"XXXXX"}' // curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'https://api.thetvdb.com/login' --data '{"apikey":"XXXXX"}'
ByteBuffer response = post(getEndpoint(path), asJsonString(json).getBytes(UTF_8), "application/json", null); ByteBuffer response = post(getEndpoint(path), json(object, false).getBytes(UTF_8), "application/json", null);
return readJson(UTF_8.decode(response)); return readJson(UTF_8.decode(response));
} }