public static async Task ProcessAsync(IOwinContext context, WebMediaType type, string id, int maxWidth, int maxHeight, string borders = null) { if (id == null) { throw new BadRequestException("GetImageResized: id is null"); } if (maxWidth == 0) { throw new BadRequestException("GetImageResized: maxWidth is null"); } if (maxHeight == 0) { throw new BadRequestException("GetImageResized: maxHeight is null"); } Guid idGuid; if (!Guid.TryParse(id, out idGuid)) { throw new BadRequestException(String.Format("GetImageResized: Couldn't parse if '{0}' to Guid", id)); } ISet <Guid> necessaryMIATypes = new HashSet <Guid>(); necessaryMIATypes.Add(MediaAspect.ASPECT_ID); necessaryMIATypes.Add(ProviderResourceAspect.ASPECT_ID); necessaryMIATypes.Add(ImporterAspect.ASPECT_ID); necessaryMIATypes.Add(ImageAspect.ASPECT_ID); MediaItem item = MediaLibraryAccess.GetMediaItemById(context, idGuid, necessaryMIATypes, null); var resourcePathStr = item.PrimaryResources[item.ActiveResourceLocatorIndex].GetAttributeValue(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH); var resourcePath = ResourcePath.Deserialize(resourcePathStr.ToString()); var ra = GetResourceAccessor(resourcePath); IFileSystemResourceAccessor fsra = ra as IFileSystemResourceAccessor; if (fsra == null) { throw new InternalServerException("GetImage: failed to create IFileSystemResourceAccessor"); } // Resize ImageCache.CacheIdentifier identifier = ImageCache.GetIdentifier(idGuid, false, maxWidth, maxHeight, borders, 0, FanArtTypes.Undefined, FanArtMediaTypes.Image); byte[] resizedImage; if (ImageCache.TryGetImageFromCache(context, identifier, out resizedImage)) { Logger.Info("GetImageResized: Got image from cache"); } else { using (var resourceStream = fsra.OpenRead()) { byte[] buffer = new byte[resourceStream.Length]; resourceStream.Read(buffer, 0, Convert.ToInt32(resourceStream.Length)); resizedImage = Plugins.MP2Extended.WSS.Images.ResizeImage(buffer, maxWidth, maxHeight, borders); } // Add to cache if (ImageCache.AddImageToCache(context, resizedImage, identifier)) { Logger.Info("GetImageResized: Added image to cache"); } } using (var resourceStream = new MemoryStream(resizedImage)) { // HTTP/1.1 RFC2616 section 14.25 'If-Modified-Since' if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"])) { DateTime lastRequest = DateTime.Parse(context.Request.Headers["If-Modified-Since"]); if (lastRequest.CompareTo(fsra.LastChanged) <= 0) { context.Response.StatusCode = (int)HttpStatusCode.NotModified; } } // HTTP/1.1 RFC2616 section 14.29 'Last-Modified' context.Response.Headers["Last-Modified"] = fsra.LastChanged.ToUniversalTime().ToString("r"); string byteRangesSpecifier = context.Request.Headers["Range"]; IList <Range> ranges = ParseByteRanges(byteRangesSpecifier, resourceStream.Length); bool onlyHeaders = context.Request.Method == "HEAD" || context.Response.StatusCode == (int)HttpStatusCode.NotModified; if (ranges != null && ranges.Count > 0) { // We only support last range await SendRangeAsync(context, resourceStream, ranges[ranges.Count - 1], onlyHeaders); } else { await SendWholeFileAsync(context, resourceStream, onlyHeaders); } } }
public static Task <WebBoolResult> ProcessAsync(IOwinContext context, WebMediaType mediatype, int?provider, string id, WebFileType artworktype, int offset, int maxWidth, int maxHeight, string borders = null, string format = null) { int offsetInt = 0; if (id == null) { throw new BadRequestException("RequestImageResize: id is null"); } string fanartType; string fanArtMediaType; MapTypes(artworktype, mediatype, out fanartType, out fanArtMediaType); bool isTvRadio = fanArtMediaType == FanArtMediaTypes.ChannelTv || fanArtMediaType == FanArtMediaTypes.ChannelRadio; bool isRecording = mediatype == WebMediaType.Recording; Guid idGuid; int idInt; if (!Guid.TryParse(id, out idGuid) && !isTvRadio) { throw new BadRequestException(String.Format("RequestImageResize: Couldn't parse if '{0}' to Guid", id)); } if (int.TryParse(id, out idInt) && (fanArtMediaType == FanArtMediaTypes.ChannelTv || fanArtMediaType == FanArtMediaTypes.ChannelRadio)) { idGuid = IntToGuid(idInt); } ImageCache.CacheIdentifier identifier = ImageCache.GetIdentifier(idGuid, isTvRadio, maxWidth, maxHeight, borders, offsetInt, fanartType, fanArtMediaType); byte[] data; if (ImageCache.TryGetImageFromCache(context, identifier, out data)) { Logger.Info("RequestImageResize: got image from cache"); return(Task.FromResult(new WebBoolResult { Result = true })); } IList <FanArtImage> fanart = GetFanArtImages(context, id, isTvRadio, isRecording, fanartType, fanArtMediaType); // get offset if (offsetInt >= fanart.Count) { Logger.Warn("RequestImageResize: offset is too big! FanArt: {0} Offset: {1}", fanart.Count, offsetInt); offsetInt = 0; } byte[] resizedImage = Plugins.MP2Extended.WSS.Images.ResizeImage(fanart[offsetInt].BinaryData, maxWidth, maxHeight, borders); // Add to cache, but only if it is no dummy image if (fanart[offsetInt].Name != NO_FANART_IMAGE_NAME) { if (ImageCache.AddImageToCache(context, resizedImage, identifier)) { Logger.Info("RequestImageResize: Added image to cache"); return(Task.FromResult(new WebBoolResult { Result = true })); } } return(Task.FromResult(new WebBoolResult { Result = false })); }
// We just return a Thumbnail from MP public static async Task ProcessAsync(IOwinContext context, WebMediaType type, string itemId, int maxWidth, int maxHeight, string borders = null) { // set borders to transparent borders = "transparent"; if (itemId == null) { throw new BadRequestException("ExtractImageResized: id is null"); } if (maxWidth == 0) { maxWidth = 800; } if (maxHeight == 0) { maxHeight = 600; } string fanartType; string fanArtMediaType; MapTypes(WebFileType.Content, WebMediaType.File, out fanartType, out fanArtMediaType); bool isTvRadio = fanArtMediaType == FanArtMediaTypes.ChannelTv || fanArtMediaType == FanArtMediaTypes.ChannelRadio; bool isRecording = (type == WebMediaType.Recording); Guid idGuid; int idInt; if (!Guid.TryParse(itemId, out idGuid) && !isTvRadio) { throw new BadRequestException(String.Format("ExtractImageResized: Couldn't parse id '{0}'", itemId)); } else if (int.TryParse(itemId, out idInt) && (fanArtMediaType == FanArtMediaTypes.ChannelTv || fanArtMediaType == FanArtMediaTypes.ChannelRadio)) { idGuid = IntToGuid(idInt); } ImageCache.CacheIdentifier identifier = ImageCache.GetIdentifier(idGuid, isTvRadio, maxWidth, maxHeight, borders, 0, FanArtTypes.Thumbnail, FanArtMediaTypes.Undefined); Stream resourceStream; byte[] data; if (ImageCache.TryGetImageFromCache(context, identifier, out data)) { Logger.Info("GetArtworkResized: got image from cache"); resourceStream = ImageFile(data); context.Response.ContentType = "image/*"; await SendWholeFileAsync(context, resourceStream, false); resourceStream.Dispose(); return; } IList <FanArtImage> fanart = GetFanArtImages(context, itemId, isTvRadio, isRecording, fanartType, fanArtMediaType); // get a random FanArt from the List Random rnd = new Random(); int r = rnd.Next(fanart.Count); byte[] resizedImage; if (maxWidth != 0 && maxHeight != 0) { resizedImage = Plugins.MP2Extended.WSS.Images.ResizeImage(fanart[r].BinaryData, maxWidth, maxHeight, borders); } else { resizedImage = fanart[r].BinaryData; } // Add to cache, but only if it is no dummy image if (fanart[r].Name != NO_FANART_IMAGE_NAME) { if (ImageCache.AddImageToCache(context, resizedImage, identifier)) { Logger.Info("GetArtworkResized: Added image to cache"); } } resourceStream = ImageFile(resizedImage); context.Response.ContentType = "image/*"; await SendWholeFileAsync(context, resourceStream, false); resourceStream.Dispose(); }