<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;

namespace Endzone.Umbraco.Extensions.PublishedContentExtensions
{
    public static class Media
    {
        //default values
        private const int ImageQuality = 100;

        public static IEnumerable<IPublishedContent> GetMultipleTypedMedia(this IPublishedContent content, string property)
        {
            if (!content.HasValue(property))
                return Enumerable.Empty<IPublishedContent>();

            var imageIds = content.GetPropertyValue<string>(property).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

            return umbracoHelper.TypedMedia(imageIds);
        }

        public static IPublishedContent GetMedia(this IPublishedContent content, string property, bool recursive = false)
        {
            var id = content.GetPropertyValue<string>(property, recursive);

            if (id == null)
                return null;

            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            return umbracoHelper.TypedMedia(id);
        }

        /// <summary>
        /// Works for MultipleMediaPicker where the image media is of type Image Cropper (as opposed to upload). Crops the images to the size specified in the crop.
        /// </summary>
        public static IHtmlString ShowImagesCropped(this IPublishedContent item, string cropAlias, string property = "image", string imgclass = "", bool recurse = false, bool lazy = false, string urlAppend = null, string id = null, string prepend = null, string append = null)
        {
            var imageQuality = GetWebsiteImageQuality(item);
            var htmlResult = new StringBuilder();
            if (item.HasValue(property, recurse))
            {
                var imagesList = item.GetPropertyValue<string>(property, recurse).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var imagesCollection = umbracoHelper.TypedMedia(imagesList).Where(x => x != null);
                var attribute = lazy ? "data-lazy" : "src";

                foreach (var imageItem in imagesCollection)
                {
                    var url = imageItem.GetCropUrl(cropAlias: cropAlias, imageCropMode: ImageCropMode.Crop, useCropDimensions: true, quality: imageQuality) + urlAppend;
                    htmlResult.Append(prepend);
                    htmlResult.Append($"<img {attribute}=\"{url}\" id=\"{id}\" class=\"{imgclass}\" alt=\"{imageItem.GetPropertyValue("altText")}\" title=\"{imageItem.GetPropertyValue("altText")}\" />");
                    htmlResult.Append(append);
                }
            }
            return new HtmlString(htmlResult.ToString());
        }

        /// <summary>
        /// Works for MultipleMediaPicker. Shows images in their original size.
        /// </summary>
        public static IHtmlString ShowImages(this IPublishedContent item, string property = "image", string imgclass = "", bool recurse = false, bool lazy = false, string urlAppend = null, string id = null, string prepend = null, string append = null)
        {
            var imageQuality = GetWebsiteImageQuality(item);
            var htmlResult = new StringBuilder();
            if (item.HasValue(property, recurse))
            {
                var imagesList = item.GetPropertyValue<string>(property, recurse).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var imagesCollection = umbracoHelper.TypedMedia(imagesList).Where(x => x != null);
                var attribute = lazy ? "data-lazy" : "src";

                foreach (var imageItem in imagesCollection)
                {
                    var url = imageItem.Url + urlAppend;
                    url = url.SetUrlParameter("quality", imageQuality);
                    htmlResult.Append(prepend);
                    htmlResult.Append($"<img {attribute}=\"{url}\" id=\"{id}\" class=\"{imgclass}\" alt=\"{imageItem.GetPropertyValue("altText")}\" title=\"{imageItem.GetPropertyValue("altText")}\" />");
                    htmlResult.Append(append);
                }
            }
            return new HtmlString(htmlResult.ToString());
        }

        /// <summary>
        /// Works for MultipleMediaPicker where the image media is of type Image Cropper(as opposed to upload). Shows the image's urls in their cropped version.
        /// </summary>
        public static IHtmlString ShowImageUrlsCropped(this IPublishedContent item, string cropAlias, string property = "image", bool recurse = false, string prepend = null, string append = null)
        {
            var imageQuality = GetWebsiteImageQuality(item);
            var htmlResult = new StringBuilder();
            if (item.HasValue(property, recurse))
            {
                var imagesList = item.GetPropertyValue<string>(property, recurse).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var imagesCollection = umbracoHelper.TypedMedia(imagesList).Where(x => x != null);

                foreach (var image in imagesCollection)
                {
                    var url = prepend + image.GetCropUrl(cropAlias: cropAlias, imageCropMode: ImageCropMode.Crop, useCropDimensions: true, quality:imageQuality) + append;
                    htmlResult.Append(url);
                }
            }
            return new HtmlString(htmlResult.ToString());<commit_msg>Added image alt tag property name override.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;

namespace Endzone.Umbraco.Extensions.PublishedContentExtensions
{
    public static class Media
    {
        //default values
        private const int ImageQuality = 100;

        public static IEnumerable<IPublishedContent> GetMultipleTypedMedia(this IPublishedContent content, string property)
        {
            if (!content.HasValue(property))
                return Enumerable.Empty<IPublishedContent>();

            var imageIds = content.GetPropertyValue<string>(property).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

            return umbracoHelper.TypedMedia(imageIds);
        }

        public static IPublishedContent GetMedia(this IPublishedContent content, string property, bool recursive = false)
        {
            var id = content.GetPropertyValue<string>(property, recursive);

            if (id == null)
                return null;

            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            return umbracoHelper.TypedMedia(id);
        }

        /// <summary>
        /// Works for MultipleMediaPicker where the image media is of type Image Cropper (as opposed to upload). Crops the images to the size specified in the crop.
        /// </summary>
        public static IHtmlString ShowImagesCropped(this IPublishedContent item, string cropAlias, string property = "image", string imgclass = "", bool recurse = false, bool lazy = false, string urlAppend = null, string id = null, string prepend = null, string append = null, string altText = "altText")
        {
            var imageQuality = GetWebsiteImageQuality(item);
            var htmlResult = new StringBuilder();
            if (item.HasValue(property, recurse))
            {
                var imagesList = item.GetPropertyValue<string>(property, recurse).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var imagesCollection = umbracoHelper.TypedMedia(imagesList).Where(x => x != null);
                var attribute = lazy ? "data-lazy" : "src";

                foreach (var imageItem in imagesCollection)
                {
                    var url = imageItem.GetCropUrl(cropAlias: cropAlias, imageCropMode: ImageCropMode.Crop, useCropDimensions: true, quality: imageQuality) + urlAppend;
                    htmlResult.Append(prepend);
                    htmlResult.Append($"<img {attribute}=\"{url}\" id=\"{id}\" class=\"{imgclass}\" alt=\"{imageItem.GetPropertyValue(altText)}\" title=\"{imageItem.GetPropertyValue(altText)}\" />");
                    htmlResult.Append(append);
                }
            }
            return new HtmlString(htmlResult.ToString());
        }

        /// <summary>
        /// Works for MultipleMediaPicker. Shows images in their original size.
        /// </summary>
        public static IHtmlString ShowImages(this IPublishedContent item, string property = "image", string imgclass = "", bool recurse = false, bool lazy = false, string urlAppend = null, string id = null, string prepend = null, string append = null, string altText = "altText")
        {
            var imageQuality = GetWebsiteImageQuality(item);
            var htmlResult = new StringBuilder();
            if (item.HasValue(property, recurse))
            {
                var imagesList = item.GetPropertyValue<string>(property, recurse).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var imagesCollection = umbracoHelper.TypedMedia(imagesList).Where(x => x != null);
                var attribute = lazy ? "data-lazy" : "src";

                foreach (var imageItem in imagesCollection)
                {
                    var url = imageItem.Url + urlAppend;
                    url = url.SetUrlParameter("quality", imageQuality);
                    htmlResult.Append(prepend);
                    htmlResult.Append($"<img {attribute}=\"{url}\" id=\"{id}\" class=\"{imgclass}\" alt=\"{imageItem.GetPropertyValue(altText)}\" title=\"{imageItem.GetPropertyValue(altText)}\" />");
                    htmlResult.Append(append);
                }
            }
            return new HtmlString(htmlResult.ToString());
        }

        /// <summary>
        /// Works for MultipleMediaPicker where the image media is of type Image Cropper(as opposed to upload). Shows the image's urls in their cropped version.
        /// </summary>
        public static IHtmlString ShowImageUrlsCropped(this IPublishedContent item, string cropAlias, string property = "image", bool recurse = false, string prepend = null, string append = null)
        {
            var imageQuality = GetWebsiteImageQuality(item);
            var htmlResult = new StringBuilder();
            if (item.HasValue(property, recurse))
            {
                var imagesList = item.GetPropertyValue<string>(property, recurse).Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var imagesCollection = umbracoHelper.TypedMedia(imagesList).Where(x => x != null);

                foreach (var image in imagesCollection)
                {
                    var url = prepend + image.GetCropUrl(cropAlias: cropAlias, imageCropMode: ImageCropMode.Crop, useCropDimensions: true, quality: imageQuality) + append;
                    htmlResult.Append(url);
                }
            }
            return new HtmlString(htmlResult.ToString());
{'fraction_non_alphanumeric': 0.10384519142547335, 'fraction_numerical': 0.0005004587538577029, 'mean_word_length': 2.4563274718939176, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '979932', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2952, 'n_words': 766}