public static WebMediaInfo GetMediaInfo(MediaSource source) { // we can't use our persistent cache for TV unfortunately, but we do cache them in memory for 60 seconds if (source.MediaType == WebMediaType.TV) { if (tvCache.ContainsKey(source.Id) && DateTime.Now - tvCache[source.Id].Item1 > TimeSpan.FromSeconds(60)) { return(tvCache[source.Id].Item2); } // save it to our memory cache for a while TsBuffer buf = new TsBuffer(source.Id); string path = buf.GetCurrentFilePath(); Log.Debug("Using path {0} from TS buffer {1} as source for {2}", path, source.Id, source.GetDebugName()); WebMediaInfo info = DoLoadMediaInfo(buf.GetCurrentFilePath(), true); tvCache[source.Id] = new Tuple <DateTime, WebMediaInfo>(DateTime.Now, info); return(info); } // load this item from persistent disk cache, if possible if (persistentCache.HasForSource(source)) { return(persistentCache.GetForSource(source)); } // some checks that only matter when we are actually going to load it from disk if (!source.Exists) { Log.Warn("Trying to load mediainfo for {0}, which doesn't seem to exist", source.GetDebugName()); return(null); } else if (!source.SupportsDirectAccess) { // not (yet?) supported Log.Warn("Loading mediainfo for non-direct access source {0} isn't supported yet", source.GetDebugName()); return(null); } // actually load it WebMediaInfo outInfo; using (var impersonator = source.GetImpersonator()) { outInfo = DoLoadMediaInfo(source.GetPath(), false); } if (outInfo != null) { persistentCache.Save(source, outInfo); } return(outInfo); }
public static WebMediaInfo GetMediaInfo(MediaSource source) { // Timeshiftings are a special case, as they can't be cached and need an additional path resolving step if (source.MediaType == WebMediaType.TV) { if (tvCache.ContainsKey(source.Id) && DateTime.Now - tvCache[source.Id].Item1 > TimeSpan.FromSeconds(60)) { return(tvCache[source.Id].Item2); } TsBuffer tsBuffer = new TsBuffer(source.Id); Log.Debug("Using path {0} from TS buffer {1} as source for {2}", tsBuffer.GetCurrentFilePath(), source.Id, source.GetDebugName()); WebMediaInfo info = LoadMediaInfo(tsBuffer.GetCurrentFilePath()); tvCache[source.Id] = new Tuple <DateTime, WebMediaInfo>(DateTime.Now, info); return(info); } using (var context = source.CreateNetworkContext()) { // verify the file actually exists and is accessible over the local file system if (!source.Exists) { Log.Warn("Trying to load MediaInfo for {0}, which does not exist or is inaccessible", source.GetDebugName()); return(null); } else if (!source.SupportsDirectAccess) { Log.Warn("Loading MediaInfo for non-direct access source {0} isn't supported yet", source.GetDebugName()); return(null); } // if we got the file in the cache, return it if we have it and the file hasn't been changed var fileInfo = source.GetFileInfo(); if (source.MediaType != WebMediaType.TV && persistentCache.HasForSource(source)) { var cachedItem = persistentCache.GetForSource(source); if (cachedItem.Size == fileInfo.Size && cachedItem.CachedDate >= fileInfo.LastModifiedTime) { return(cachedItem.Info); } } var info = LoadMediaInfo(context.RewritePath(source.GetPath())); if (info != null) { persistentCache.Save(source, new CachedInfoWrapper(info, fileInfo)); } return(info); } }