From d70e82401e5515ad563a4feab5f7859a2b8a0fe2 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 27 Mar 2016 17:40:35 +0000 Subject: [PATCH] Cache xattr values so that metadata works as expected at least for the current session even if xattr is not supported by the filesystem and thus metadata can't be persisted --- source/net/filebot/Resource.java | 4 +++ source/net/filebot/media/XattrMetaInfo.java | 40 ++++++++++++--------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/source/net/filebot/Resource.java b/source/net/filebot/Resource.java index 70017dce..42273edd 100644 --- a/source/net/filebot/Resource.java +++ b/source/net/filebot/Resource.java @@ -15,6 +15,10 @@ public interface Resource { return new TransformedResource(this, function); } + static Resource lazy(Resource resource) { + return resource.memoize(); + } + } class MemoizedResource implements Resource { diff --git a/source/net/filebot/media/XattrMetaInfo.java b/source/net/filebot/media/XattrMetaInfo.java index eff7fee7..49286402 100644 --- a/source/net/filebot/media/XattrMetaInfo.java +++ b/source/net/filebot/media/XattrMetaInfo.java @@ -9,6 +9,7 @@ import java.util.Locale; import net.filebot.Cache; import net.filebot.Cache.Compute; import net.filebot.CacheType; +import net.filebot.Resource; import net.filebot.WebServices; import net.filebot.web.Episode; import net.filebot.web.Movie; @@ -53,17 +54,21 @@ public class XattrMetaInfo { public synchronized Object getMetaInfo(File file) { return getCachedValue(xattrMetaInfoCache, file, key -> { - Object metadata = new MetaAttributes(file).getObject(); + Object metadata = xattr(file).getObject(); return isMetaInfo(metadata) ? metadata : null; }); } public synchronized String getOriginalName(File file) { return (String) getCachedValue(xattrOriginalNameCache, file, key -> { - return new MetaAttributes(file).getOriginalName(); + return xattr(file).getOriginalName(); }); } + private MetaAttributes xattr(File file) throws Exception { + return new MetaAttributes(file); + } + private Object getCachedValue(Cache cache, File file, Compute compute) { // try in-memory cache of previously stored xattr metadata try { @@ -86,11 +91,13 @@ public class XattrMetaInfo { } // set creation date to episode / movie release date + Resource xattr = Resource.lazy(() -> xattr(file)); + if (useCreationDate) { try { long t = getTimeStamp(model); if (t > 0) { - new MetaAttributes(file).setCreationDate(t); + xattr.get().setCreationDate(t); } } catch (Throwable e) { debug.warning("Failed to set creation date: " + e.getMessage()); @@ -98,20 +105,21 @@ public class XattrMetaInfo { } // store metadata object and original name as xattr - if (useExtendedFileAttributes) { - try { - MetaAttributes attr = new MetaAttributes(file); - if (isMetaInfo(model)) { - xattrMetaInfoCache.put(file, model); - attr.setObject(model); + try { + if (isMetaInfo(model)) { + xattrMetaInfoCache.put(file, model); + if (useExtendedFileAttributes) { + xattr.get().setObject(model); } - if (original != null && original.length() > 0 && getOriginalName(file) == null) { - xattrOriginalNameCache.put(file, original); - attr.setOriginalName(original); - } - } catch (Throwable e) { - debug.warning("Failed to set xattr: " + e.getMessage()); } + if (original != null && original.length() > 0 && getOriginalName(file) == null) { + xattrOriginalNameCache.put(file, original); + if (useExtendedFileAttributes) { + xattr.get().setOriginalName(original); + } + } + } catch (Throwable e) { + debug.warning("Failed to set xattr: " + e.getMessage()); } } @@ -122,7 +130,7 @@ public class XattrMetaInfo { if (useExtendedFileAttributes) { try { - new MetaAttributes(file).clear(); + xattr(file).clear(); } catch (Throwable e) { debug.warning("Failed to clear xattr: " + e.getMessage()); }