public string GetMediaUrl(MediaItem mediaItem)
        {
            if (mediaItem == null)
            {
                return(null);
            }

            var urlBuilderOptions = new MediaUrlBuilderOptions
            {
                AbsolutePath           = true,
                AlwaysIncludeServerUrl = true,
                LanguageEmbedding      = LanguageEmbedding.Never
            };

            string mediaUrl = _mediaManager.GetMediaUrl(mediaItem, urlBuilderOptions);

            if (string.IsNullOrEmpty(mediaUrl))
            {
                return(null);
            }

            if (mediaUrl.EndsWith(".ashx") &&
                !string.IsNullOrEmpty(mediaItem.Extension))
            {
                mediaUrl = mediaUrl.Replace(".ashx", $".{mediaItem.Extension}");
            }

            return(mediaUrl);
        }
Exemplo n.º 2
0
        public static string ImageUrl(this ImageField imageField, MediaUrlBuilderOptions options)
        {
            if (imageField?.MediaItem == null)
            {
                throw new ArgumentNullException(nameof(imageField));
            }

            return(options == null?imageField.ImageUrl() : HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imageField.MediaItem, options)));
        }
        public static string ImageUrl(this Item item, ID imageFieldId, MediaUrlBuilderOptions options = null)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            var imageField = (ImageField)item.Fields[imageFieldId];

            return(imageField?.MediaItem == null ? string.Empty : imageField.ImageUrl(options));
        }
Exemplo n.º 4
0
        public XmlRpcStruct newMediaObject(string blogid, string username, string password, XmlRpcStruct rpcstruct)
        {
            // Check user validation
            Authenticate(username, password);

            var name     = rpcstruct["name"].ToString();
            var media    = (byte[])rpcstruct["bits"];
            var blogName = string.Empty;

            var currentBlog = GetContentDatabase().GetItem(blogid);

            blogName = currentBlog.Name;

            // Get filename
            var fileName  = Path.GetFileName(name);
            var imageName = ItemUtil.ProposeValidItemName(Path.GetFileNameWithoutExtension(fileName));

            // Create strem from byte array
            var memStream = new MemoryStream(media);
            var md        = new MediaCreatorOptions();

            md.Destination   = string.Join("/", new string[] { Constants.Paths.WeBlogMedia, blogName, imageName });
            md.Database      = GetContentDatabase();
            md.AlternateText = imageName;

            // Check access rights
            CheckUserCreateRights(md.Destination);

            // Create mediaitem
            var mediaItem = MediaManager.Creator.CreateFromStream(memStream, fileName, md);

            // Close stream
            memStream.Close();
            memStream.Dispose();

            // Publish mediaitem to web database
            ContentHelper.PublishItemAndRequiredAncestors(mediaItem.ID);

            // Get the mediaitem url and return it
            var rstruct = new XmlRpcStruct();

#if FEATURE_URL_BUILDERS
            var options = new MediaUrlBuilderOptions()
#else
            var options = new MediaUrlOptions()
#endif
            {
                AbsolutePath = false,
                UseItemPath  = false
            };

            rstruct.Add("url", MediaManager.GetMediaUrl(mediaItem, options));
            return(rstruct);
        }
        public static string ImageUrl(this MediaItem mediaItem, int width, int height)
        {
            if (mediaItem == null)
            {
                throw new ArgumentNullException(nameof(mediaItem));
            }

            var options = new MediaUrlBuilderOptions {
                Height = height, Width = width
            };
            var url       = MediaManager.GetMediaUrl(mediaItem, options);
            var cleanUrl  = StringUtil.EnsurePrefix('/', url);
            var hashedUrl = HashingUtils.ProtectAssetUrl(cleanUrl);

            return(hashedUrl);
        }
        private static void UpdateSchemaProperties(IEnumerable <SchemaProperty> schemaProperties)
        {
            ItemUrlBuilderOptions urlOptions = new ItemUrlBuilderOptions {
                AlwaysIncludeServerUrl = true, LanguageEmbedding = LanguageEmbedding.Never
            };

            MediaUrlBuilderOptions mediaurlOptions = new MediaUrlBuilderOptions();

            mediaurlOptions.AlwaysIncludeServerUrl = true;

            if (schemaProperties != null)
            {
                foreach (var item in schemaProperties)
                {
                    if (item.ItemScope)
                    {
                        UpdateSchemaProperties(item.Properties);
                    }
                    else
                    {
                        if (item.FieldType.Value == Constants.SchemaFieldTypes.CurrentItemURL)
                        {
                            item.FieldValue = LinkManager.GetItemUrl(Sitecore.Context.Item, urlOptions);
                        }
                        else if (item.FieldType.Value == Constants.SchemaFieldTypes.FromField)
                        {
                            item.FieldValue = Sitecore.Context.Item.Fields[item.FieldValue]?.Value;
                        }
                        else if (item.FieldType.Value == Constants.SchemaFieldTypes.FromParent)
                        {
                            item.FieldValue = Sitecore.Context.Item.Parent?.Fields[item.FieldType.Value]?.Value;
                        }
                        else if (item.FieldType.Value == Constants.SchemaFieldTypes.Image)
                        {
                            Sitecore.Data.Fields.ImageField imgField = Sitecore.Context.Item.Fields[item.FieldValue];

                            if (imgField != null)
                            {
                                var theURL = MediaManager.GetMediaUrl(imgField.MediaItem, mediaurlOptions);

                                item.FieldValue = HashingUtils.ProtectAssetUrl(theURL);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        public static string MediaUrl(this SitecoreHelper sitecoreHelper, ID fieldId, Item item, MediaUrlBuilderOptions options)
        {
            ImageField imageField = item?.Fields[fieldId];

            if (imageField == null || imageField.MediaItem == null)
            {
                return(string.Empty);
            }
            var url = options != null?MediaManager.GetMediaUrl(imageField.MediaItem, options) : MediaManager.GetMediaUrl(imageField.MediaItem);

            return(HashingUtils.ProtectAssetUrl(url));
        }
Exemplo n.º 8
0
 public static string MediaUrl(this SitecoreHelper sitecoreHelper, ID fieldId, MediaUrlBuilderOptions options)
 {
     return(MediaUrl(sitecoreHelper, fieldId, sitecoreHelper.CurrentItem, options));
 }
 public override string GetMediaUrl(MediaItem item, MediaUrlBuilderOptions options)
 {
     return(this.IsLocalProviderSet() ? this.localProvider.Value.GetMediaUrl(item, options) : null);
 }
        public static string MediaUrl(this Item item, ID mediaFieldId, MediaUrlBuilderOptions options = null)
        {
            var targetItem = item.TargetItem(mediaFieldId);

            return(targetItem == null ? string.Empty : (MediaManager.GetMediaUrl(targetItem) ?? string.Empty));
        }