/// <summary> /// Checks if an image is already in the cache /// </summary> /// <param name="id">Guid of the media item</param> /// <param name="identifier">identifier from GetIdentifier()</param> /// <param name="width">Width of the finale image</param> /// <param name="height">height of the final image</param> /// <param name="borders">borders of the final image</param> /// <returns>Returns true if the image was added to the cache, false if the image is already in the cache</returns> /// <returns>true if the image is in the cache, otherwise false</returns> internal static bool IsInCache(IOwinContext context, CacheIdentifier identifier) { lock (_lockObject) { string filepath = GetFilePath(identifier); DateTime dateAdded; if (!identifier.IsTvRadio) { ISet <Guid> necessaryMIATypes = new HashSet <Guid>(); necessaryMIATypes.Add(ImporterAspect.ASPECT_ID); MediaItem item = MediaLibraryAccess.GetMediaItemById(context, identifier.MediaItemId, necessaryMIATypes, null); if (item == null) { return(false); } dateAdded = (DateTime)item.GetAspect(ImporterAspect.Metadata)[ImporterAspect.ATTR_DATEADDED]; } else { dateAdded = DateTime.Now.AddMonths(-1); // refresh image evry month } return(File.Exists(GetFilePath(identifier)) && DateTime.Compare(GetLastModifiedTime(filepath), dateAdded) >= 0); } }
/// <summary> /// Adds an image to the cache, but only if it doesn't exist yet /// </summary> /// <param name="data">The image data as byte Array</param> /// <param name="id">Guid of the media item</param> /// <param name="identifier">identifier from GetIdentifier()</param> /// <param name="width">Width of the finale image</param> /// <param name="height">height of the final image</param> /// <param name="borders">borders of the final image</param> /// <returns>Returns true if the image was added to the cache, false if the image is already in the cache</returns> internal static bool AddImageToCache(IOwinContext context, byte[] data, CacheIdentifier identifier) { lock (_lockObject) { if (IsInCache(context, identifier)) { return(false); } FileStream stream = File.OpenWrite(GetFilePath(identifier)); stream.Write(data, 0, data.Length); stream.Close(); return(true); } }
private static string GetFilePath(CacheIdentifier identifier) { lock (_lockObject) { string dataDirectory = ServiceRegistration.Get <IPathManager>().GetPath("<DATA>\\" + CHACHE_DIR); if (!Directory.Exists(dataDirectory)) { Directory.CreateDirectory(dataDirectory); } string filename = string.Format(FILENAME_PATTERN, identifier.MediaItemId, identifier.ClassId, identifier.Width, identifier.Height, identifier.Borders, identifier.Offset, identifier.FanArtType, identifier.FanArtMediaType); return(Path.Combine(dataDirectory, filename)); } }
internal static bool TryGetImageFromCache(IOwinContext context, CacheIdentifier identifier, out byte[] data) { lock (_lockObject) { data = new byte[0]; string filepath = GetFilePath(identifier); if (!IsInCache(context, identifier)) { return(false); } FileStream stream = File.OpenRead(GetFilePath(identifier)); data = new byte[Convert.ToInt32(stream.Length)]; stream.Read(data, 0, Convert.ToInt32(stream.Length)); stream.Close(); return(true); } }