Simplify json xattr metadata

This commit is contained in:
Reinhard Pointner 2016-08-13 23:07:24 +08:00
parent 53c0e6593d
commit d06352e507
2 changed files with 22 additions and 4 deletions

View File

@ -1,10 +1,14 @@
package net.filebot.media;
import static java.util.Collections.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
import java.util.HashMap;
import java.util.Map;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
@ -19,9 +23,12 @@ public class MetaAttributes {
private final BasicFileAttributeView fileAttributeView;
private final MetaAttributeView metaAttributeView;
public MetaAttributes(File file) throws IOException {
private final Map<String, String> jsonTypeMap;
public MetaAttributes(File file, Map<String, String> jsonTypeMap) throws IOException {
this.metaAttributeView = new MetaAttributeView(file);
this.fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class);
this.jsonTypeMap = jsonTypeMap;
}
public void setCreationDate(long millis) throws IOException {
@ -46,7 +53,7 @@ public class MetaAttributes {
public void setObject(Object object) {
try {
metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object));
metaAttributeView.put(METADATA_KEY, JsonWriter.objectToJson(object, singletonMap(JsonWriter.TYPE_NAME_MAP, jsonTypeMap)));
} catch (Exception e) {
throw new RuntimeException(e);
}
@ -56,7 +63,11 @@ public class MetaAttributes {
try {
String jsonObject = metaAttributeView.get(METADATA_KEY);
if (jsonObject != null && jsonObject.length() > 0) {
return JsonReader.jsonToJava(jsonObject);
Map<String, Object> options = new HashMap<String, Object>(2);
options.put(JsonReader.TYPE_NAME_MAP, jsonTypeMap);
// options must be a modifiable map
return JsonReader.jsonToJava(jsonObject, options);
}
} catch (Exception e) {
throw new RuntimeException(e);

View File

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