filebot/source/net/filebot/media/XattrMetaInfo.java

133 lines
3.8 KiB
Java

package net.filebot.media;
import static net.filebot.Logging.*;
import static net.filebot.Settings.*;
import java.io.File;
import java.util.Locale;
import net.filebot.Cache;
import net.filebot.Cache.Compute;
import net.filebot.CacheType;
import net.filebot.WebServices;
import net.filebot.web.Episode;
import net.filebot.web.Movie;
import net.filebot.web.SimpleDate;
public class XattrMetaInfo {
public static final XattrMetaInfo xattr = new XattrMetaInfo(useExtendedFileAttributes(), useCreationDate());
private final boolean useExtendedFileAttributes;
private final boolean useCreationDate;
private final Cache xattrMetaInfoCache = Cache.getCache(MetaAttributes.METADATA_KEY, CacheType.Ephemeral);
private final Cache xattrOriginalNameCache = Cache.getCache(MetaAttributes.FILENAME_KEY, CacheType.Ephemeral);
public XattrMetaInfo(boolean useExtendedFileAttributes, boolean useCreationDate) {
this.useExtendedFileAttributes = useExtendedFileAttributes;
this.useCreationDate = useCreationDate;
}
public boolean isMetaInfo(Object object) {
return object instanceof Episode || object instanceof Movie;
}
public long getTimeStamp(Object object) throws Exception {
if (object instanceof Episode) {
Episode episode = (Episode) object;
if (episode.getAirdate() != null) {
return episode.getAirdate().getTimeStamp();
}
} else if (object instanceof Movie) {
Movie movie = (Movie) object;
if (movie.getYear() > 0 && movie.getTmdbId() > 0) {
SimpleDate releaseDate = WebServices.TheMovieDB.getMovieInfo(movie, Locale.ENGLISH, false).getReleased();
if (releaseDate != null) {
return releaseDate.getTimeStamp();
}
}
}
return -1;
}
public synchronized Object getMetaInfo(File file) {
return getCachedValue(xattrMetaInfoCache, file, key -> {
Object metadata = new MetaAttributes(file).getObject();
return isMetaInfo(metadata) ? metadata : null;
});
}
public synchronized String getOriginalName(File file) {
return (String) getCachedValue(xattrOriginalNameCache, file, key -> {
return new MetaAttributes(file).getOriginalName();
});
}
private Object getCachedValue(Cache cache, File file, Compute<?> compute) {
// try in-memory cache of previously stored xattr metadata
try {
return cache.computeIfAbsent(file, key -> {
if (useExtendedFileAttributes) {
return compute.apply(key);
}
return null;
});
} catch (Throwable e) {
debug.warning("Failed to read xattr: " + e.getMessage());
}
return null;
}
public synchronized void setMetaInfo(File file, Object model, String original) {
// only for Episode / Movie objects
if (!isMetaInfo(model) || !file.isFile()) {
return;
}
// set creation date to episode / movie release date
if (useCreationDate) {
try {
long t = getTimeStamp(model);
if (t > 0) {
new MetaAttributes(file).setCreationDate(t);
}
} catch (Throwable e) {
debug.warning("Failed to set creation date: " + e.getMessage());
}
}
// 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);
}
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());
}
}
}
public synchronized void clear(File file) {
// clear in-memory cache
xattrMetaInfoCache.remove(file);
xattrOriginalNameCache.remove(file);
if (useExtendedFileAttributes) {
try {
new MetaAttributes(file).clear();
} catch (Throwable e) {
debug.warning("Failed to clear xattr: " + e.getMessage());
}
}
}
}