private void CreateNewMediaItemWithStreamAndLink(Stream stream, MediaItem currentItem, LinkField relatedField, Media originalMedia)
        {
            var mediaCreator = new MediaCreator();
            var options      = new MediaCreatorOptions
            {
                Versioned = false,
                IncludeExtensionInItemName = false,
                Database    = Factory.GetDatabase(ImageCompressionConstants.GlobalSettings.Database.Master),
                Destination = $"/sitecore/media library{currentItem.MediaPath}webp"
            };

            using (new SecurityModel.SecurityDisabler())
            {
                var newHiddenMedia = mediaCreator.CreateFromStream(stream, currentItem.Name + ".webp", options);

                newHiddenMedia.Editing.BeginEdit();
                newHiddenMedia.Appearance.Hidden = true;
                newHiddenMedia.Editing.EndEdit();

                currentItem.BeginEdit();
                relatedField.Value = string.Empty; //https://sitecore.stackexchange.com/questions/4104/how-to-programatically-update-a-general-link-field-in-a-custom-user-profille
                new LinkField(originalMedia.MediaData.MediaItem.InnerItem.Fields[ImageCompressionConstants.ImageFields.RELATED_IMAGE_FIELD])
                {
                    LinkType = "internal",
                    TargetID = newHiddenMedia.ID
                };
                currentItem.EndEdit();
            }
        }
        protected void UpdateImageFile(MediaItem currentItem, byte[] responseData)
        {
            currentItem.BeginEdit();
            Media  media  = MediaManager.GetMedia(currentItem);
            Stream stream = new MemoryStream(responseData);

            media.SetStream(stream, currentItem.Extension);
            currentItem.EndEdit();
        }
Exemple #3
0
        /// <summary>
        /// This function update the passed media item from the file received in the context
        /// </summary>
        /// <param name="_itm">Siecore.Data.Items.Item, Media Item</param>
        /// <param name="httpContext">System.Web.HttpContext, Current context from request</param>
        /// <returns>MediaItem, Updated Media Item</returns>
        public static MediaItem UpdateMediaItem(Item _itm, HttpContext httpContext)
        {
            MediaItem mediaItem = null;

            if (_itm != null && httpContext != null)
            {
                try
                {
                    mediaItem = new MediaItem(_itm);
                    mediaItem.BeginEdit();
                    if (httpContext.Request.Form["alt"] != null)
                    {
                        mediaItem.Alt = httpContext.Request.Form["alt"].ToString();
                    }
                    if (httpContext.Request.Form["Width"] != null)
                    {
                        mediaItem.InnerItem["Width"] = httpContext.Request.Form["Width"].ToString();
                    }
                    if (httpContext.Request.Form["Height"] != null)
                    {
                        mediaItem.InnerItem["Height"] = httpContext.Request.Form["Height"].ToString();
                    }
                    HttpFileCollection files = httpContext.Request.Files;
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFile httpPostedFile = files[i];
                        if (httpPostedFile.ContentLength != 0)
                        {
                            string fileName    = httpPostedFile.FileName;
                            Stream inputStream = httpPostedFile.InputStream;
                            string extension   = FileUtil.GetExtension(fileName);
                            try
                            {
                                mediaItem.Size = inputStream.Length;
                                Media media = MediaManager.GetMedia(mediaItem);
                                media.SetStream(inputStream, extension);
                                break;
                            }
                            catch
                            {
                                //Logger.Warn("Cannot create the versioned media item.");
                            }
                        }
                    }
                    mediaItem.EndEdit();
                }
                catch { }
            }
            return(mediaItem);
        }
Exemple #4
0
        public static MediaItem UploadMedia(string mediaURl, string destination, Item item, ImageField imageField = null)
        {
            string    uploadedMediaPath = string.Empty;
            MediaItem mediaItem         = null;
            string    cleanMediaURl     = mediaURl;

            if (mediaURl.Contains("?"))
            {
                cleanMediaURl = cleanMediaURl.Remove(cleanMediaURl.IndexOf('?'));
            }
            string extension = Path.GetExtension(cleanMediaURl);
            string mediaName = Path.GetFileName(cleanMediaURl);

            mediaName = mediaName.Replace(extension, "").Replace("-", " ").Replace("_", " ").Trim();

            //int nameStartIndex = mediaURl.LastIndexOf('/');
            //mediaURl.Substring(nameStartIndex).Replace(extension, "");

            try
            {
                var webRequest = WebRequest.Create(mediaURl);
                using (var webResponse = webRequest.GetResponse())
                {
                    using (var stream = webResponse.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            using (var memoryStream = new MemoryStream())
                            {
                                stream.CopyTo(memoryStream);

                                var mediaCreator = new MediaCreator();
                                var options      = new MediaCreatorOptions
                                {
                                    Versioned = false,
                                    IncludeExtensionInItemName = true,
                                    Database    = item.Database,
                                    Destination = destination + mediaName
                                };

                                using (new SecurityDisabler())
                                {
                                    mediaItem = item.Database.GetItem(options.Destination);
                                    if (mediaItem == null)
                                    {
                                        var creator = new MediaCreator();
                                        mediaItem = MediaManager.Creator.CreateFromStream(memoryStream, mediaName + extension, options);
                                        ImportReporter.Write(item, Level.Info, string.Format("New media item has been imported from {0}", mediaURl), extension, "Media Import");
                                    }
                                    mediaItem.BeginEdit();

                                    if (imageField != null)
                                    {
                                        imageField.MediaID = mediaItem.ID;
                                    }

                                    uploadedMediaPath = mediaItem.MediaPath;
                                    mediaItem.Alt     = mediaName;
                                    mediaItem.EndEdit();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ImportReporter.Write(item, Level.Error, string.Format("Error during media import from {0}. Error: {1}", mediaURl, ex.Message), extension, "Media Import");
            }

            return(mediaItem);
        }
Exemple #5
0
        public MediaItem UploadMedia(string mediaUrl, string destination, Item item, ImageField imageField = null)
        {
            MediaItem mediaItem     = null;
            string    cleanMediaUrl = mediaUrl.Contains("?")
                ? mediaUrl.Remove(mediaUrl.IndexOf('?'))
                : mediaUrl;

            string extension = Path.GetExtension(cleanMediaUrl);
            string mediaName = Path.GetFileName(cleanMediaUrl);

            mediaName = mediaName
                        .Replace(extension, "")
                        .Replace("-", " ")
                        .Replace("_", " ")
                        .Trim();

            try
            {
                var webRequest = WebRequest.Create(mediaUrl);
                using (var webResponse = webRequest.GetResponse())
                    using (var stream = webResponse.GetResponseStream())
                    {
                        if (stream == null)
                        {
                            return(mediaItem);
                        }

                        using (var memoryStream = new MemoryStream())
                        {
                            stream.CopyTo(memoryStream);

                            var mediaCreator = new MediaCreator();
                            var options      = new MediaCreatorOptions
                            {
                                Versioned = false,
                                IncludeExtensionInItemName = true,
                                Database    = item.Database,
                                Destination = destination + mediaName
                            };

                            using (new SecurityDisabler())
                            {
                                mediaItem = item.Database.GetItem(options.Destination);
                                if (mediaItem == null)
                                {
                                    mediaItem = MediaManager.Creator.CreateFromStream(memoryStream, mediaName + extension, options);
                                    Logger.Log($"New media item has been imported from {mediaUrl}", item.Paths.FullPath, LogType.Info);
                                }

                                if (imageField != null)
                                {
                                    imageField.MediaID = mediaItem.ID;
                                }

                                mediaItem.BeginEdit();
                                mediaItem.Alt = mediaName;
                                mediaItem.EndEdit();
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                Logger.Log($"Error during media import from {mediaUrl}. Error: {ex.Message}", item.Paths.FullPath, LogType.Error, imageField?.InnerField?.InnerItem?.Name);
            }

            return(mediaItem);
        }