Ejemplo n.º 1
0
        /// <summary>
        /// Returns an URL to entry thumbnail image.
        /// Currently only used for album and artist main images.
        ///
        /// Gets the URL to the static images folder on disk if possible,
        /// otherwise gets the image from the DB.
        /// </summary>
        /// <param name="urlHelper">URL helper. Cannot be null.</param>
        /// <param name="imageInfo">Image information. Cannot be null.</param>
        /// <param name="size">Requested image size.</param>
        /// <param name="fullUrl">
        /// Whether the URL should always include the hostname and application path root.
        /// If this is false (default), the URL maybe either full (such as http://vocadb.net/Album/CoverPicture/123)
        /// or relative (such as /Album/CoverPicture/123).
        /// Usually this should be set to true if the image is to be referred from another domain.
        /// </param>
        /// <returns>URL to the image thumbnail.</returns>
        public static string ImageThumb(this UrlHelper urlHelper, IEntryImageInformation imageInfo, ImageSize size, bool fullUrl = false)
        {
            if (imageInfo == null)
            {
                return(null);
            }

            var    shouldExist = ShouldExist(imageInfo);
            string dynamicUrl  = null;

            // Use MVC dynamic actions (instead of static file) when requesting original or an image that doesn't exist on disk.
            if (imageInfo.EntryType == EntryType.Album)
            {
                if (size == ImageSize.Original)
                {
                    dynamicUrl = urlHelper.Action("CoverPicture", "Album", new { id = imageInfo.Id, v = imageInfo.Version });
                }
                else if (shouldExist && !imagePersister.HasImage(imageInfo, size))
                {
                    dynamicUrl = urlHelper.Action("CoverPictureThumb", "Album", new { id = imageInfo.Id, v = imageInfo.Version });
                }
            }
            else if (imageInfo.EntryType == EntryType.Artist)
            {
                if (size == ImageSize.Original)
                {
                    dynamicUrl = urlHelper.Action("Picture", "Artist", new { id = imageInfo.Id, v = imageInfo.Version });
                }
                else if (shouldExist && !imagePersister.HasImage(imageInfo, size))
                {
                    dynamicUrl = urlHelper.Action("PictureThumb", "Artist", new { id = imageInfo.Id, v = imageInfo.Version });
                }
            }

            var ssl = WebHelper.IsSSL(HttpContext.Current.Request);

            if (dynamicUrl != null)
            {
                return(fullUrl ? VocaUriBuilder.Absolute(dynamicUrl, ssl) : dynamicUrl);
            }

            if (!shouldExist)
            {
                var unknown = GetUnknownImageUrl(urlHelper, imageInfo);
                return(fullUrl ? VocaUriBuilder.Absolute(unknown, ssl) : unknown);
            }

            // For all other cases use the static file
            return(imagePersister.GetUrlAbsolute(imageInfo, size, ssl));
        }
Ejemplo n.º 2
0
        public EntryForPictureDisplayContract GetCoverPictureThumb(int albumId)
        {
            var size = new Size(ImageHelper.DefaultThumbSize, ImageHelper.DefaultThumbSize);

            return(repository.HandleQuery(ctx => {
                var album = ctx.Load(albumId);

                if (album.CoverPictureData == null || string.IsNullOrEmpty(album.CoverPictureMime) || album.CoverPictureData.HasThumb(size))
                {
                    return EntryForPictureDisplayContract.Create(album, PermissionContext.LanguagePreference, size);
                }

                var data = new EntryThumb(album, album.CoverPictureMime);

                if (imagePersister.HasImage(data, ImageSize.Thumb))
                {
                    using (var stream = imagePersister.GetReadStream(data, ImageSize.Thumb)) {
                        var bytes = StreamHelper.ReadStream(stream);
                        return EntryForPictureDisplayContract.Create(album, data.Mime, bytes, PermissionContext.LanguagePreference);
                    }
                }

                return EntryForPictureDisplayContract.Create(album, PermissionContext.LanguagePreference, size);
            }));
        }
Ejemplo n.º 3
0
        public EntryForPictureDisplayContract GetCoverPictureThumb(int albumId)
        {
            var size = ImageSize.Thumb;

            // TODO: this all should be moved to DynamicImageUrlFactory
            return(repository.HandleQuery(ctx => {
                var album = ctx.Load(albumId);

                // If there is no picture, return empty.
                if (album.CoverPictureData == null || string.IsNullOrEmpty(album.CoverPictureMime))
                {
                    return EntryForPictureDisplayContract.Create(album, PermissionContext.LanguagePreference);
                }

                // Try to read thumbnail from file system.
                var data = album.Thumb;
                if (imagePersister.HasImage(data, size))
                {
                    var bytes = imagePersister.ReadBytes(data, size);
                    return EntryForPictureDisplayContract.Create(album, data.Mime, bytes, PermissionContext.LanguagePreference);
                }

                // This should return the original image.
                return EntryForPictureDisplayContract.Create(album, PermissionContext.LanguagePreference);
            }));
        }
Ejemplo n.º 4
0
        public EntryForPictureDisplayContract GetPictureThumb(int artistId)
        {
            var size = ImageSize.Thumb;

            return(repository.HandleQuery(ctx => {
                var artist = ctx.Load(artistId);

                if (artist.Picture == null || string.IsNullOrEmpty(artist.PictureMime))
                {
                    return EntryForPictureDisplayContract.Create(artist, PermissionContext.LanguagePreference);
                }

                var data = artist.Thumb;

                if (imagePersister.HasImage(data, size))
                {
                    var bytes = imagePersister.ReadBytes(data, size);
                    return EntryForPictureDisplayContract.Create(artist, data.Mime, bytes, PermissionContext.LanguagePreference);
                }

                return EntryForPictureDisplayContract.Create(artist, PermissionContext.LanguagePreference);
            }));
        }