/// <summary> /// Sets the value to field. /// </summary> /// <param name="args">ClientPipelineArgs</param> /// <param name="selectedImage">Selected image</param> private void ImportImageFinished(ClientPipelineArgs args, SelectedImageModel selectedImage) { var itemIdString = args.Parameters[Templates.ImportImageClientPipeline.Parameters.MediaItemId]; if (!string.IsNullOrEmpty(itemIdString)) { var itemId = new ID(itemIdString); var mediaItem = _imageService.GetImageById(itemId); if (mediaItem != null) { this.SetValue(mediaItem); this.Update(); this.SetModified(); } else { SheerResponse.Alert(_statusMessageService.GetJobStatusValueCouldNotSet()); FotoWareFieldsLog.WriteToLog("--- FotoWareImageField: Could not set field-value ---"); } } else { SheerResponse.Alert(_statusMessageService.GetJobStatusValueCouldNotSet()); FotoWareFieldsLog.WriteToLog("--- FotoWareImageField: Could not set field-value ---"); } }
/// <summary> /// Sets the value to field. /// </summary> /// <param name="args"></param> /// <param name="selectedImage"></param> private void ImportImageFinished(ClientPipelineArgs args, SelectedImageModel selectedImage) { var itemIdString = args.Parameters[Templates.ImportImageClientPipeline.Parameters.MediaItemId]; if (!string.IsNullOrEmpty(itemIdString)) { var itemId = new ID(itemIdString); var mediaUrl = _imageService.GetImageUrlById(itemId); if (!string.IsNullOrEmpty(mediaUrl)) { var imageResponse = new RichTextImageResponse { Url = mediaUrl, Alt = selectedImage.Alt }; var imageResponeJson = JsonConvert.SerializeObject(imageResponse); SheerResponse.Eval("scClose(" + StringUtil.EscapeJavascriptString(imageResponeJson) + ")"); } else { SheerResponse.Alert(_statusMessageService.GetJobStatusValueCouldNotSet()); FotoWareFieldsLog.WriteToLog("--- SelectImageFromFotoWare Dialog: Could not set field-value ---"); } } else { SheerResponse.Alert(_statusMessageService.GetJobStatusValueCouldNotSet()); FotoWareFieldsLog.WriteToLog("--- SelectImageFromFotoWare Dialog: Could not set field-value ---"); } }
/// <summary> /// Sets the value to field. /// </summary> /// <param name="args"></param> /// <param name="selectedImage"></param> private void ImportImageFinished(ClientPipelineArgs args, SelectedImageModel selectedImage) { Assert.ArgumentNotNull((object)args, nameof(args)); Item itemNotNull = Client.GetItemNotNull(args.Parameters["itemid"], Language.Parse(args.Parameters["language"])); itemNotNull.Fields.ReadAll(); Field field = itemNotNull.Fields[args.Parameters["fieldid"]]; Assert.IsNotNull((object)field, "field"); ImageField imageField = new ImageField(field, field.Value); string parameter = args.Parameters["controlid"]; string xml = args.Parameters[Templates.ImportImageClientPipeline.Parameters.FieldValue]; var itemIdString = args.Parameters[Templates.ImportImageClientPipeline.Parameters.MediaItemId]; if (!string.IsNullOrEmpty(itemIdString)) { var itemId = new ID(itemIdString); var mediaItem = _imageService.GetImageById(itemId); if (mediaItem != null) { imageField.SetAttribute("mediaid", mediaItem.ID.ToString()); if (xml.Length > 0) { XmlValue xmlValue = new XmlValue(xml, "image"); string attribute1 = xmlValue.GetAttribute("height"); if (!string.IsNullOrEmpty(attribute1)) { imageField.Height = attribute1; } string attribute2 = xmlValue.GetAttribute("width"); if (!string.IsNullOrEmpty(attribute2)) { imageField.Width = attribute2; } } SheerResponse.SetAttribute("scHtmlValue", "value", WebEditImageCommand.RenderImage(args, imageField.Value)); SheerResponse.SetAttribute("scPlainValue", "value", imageField.Value); SheerResponse.Eval("scSetHtmlValue('" + parameter + "')"); } else { SheerResponse.Alert(_statusMessageService.GetJobStatusValueCouldNotSet()); FotoWareFieldsLog.WriteToLog("--- SelectImageImageFieldCommand: Could not set field-value ---"); } } else { SheerResponse.Alert(_statusMessageService.GetJobStatusValueCouldNotSet()); FotoWareFieldsLog.WriteToLog("--- SelectImageImageFieldCommand: Could not set field-value ---"); } }
/// <inheritdoc /> public void StartJob(SelectedImageModel selectedImage, ClientPipelineArgs args, Action <ClientPipelineArgs, SelectedImageModel> postAction) { ClientPipelineArgs currentPipelineArgs = Sitecore.Context.ClientPage.CurrentPipelineArgs as ClientPipelineArgs; if (currentPipelineArgs != null) { ExecuteSync("ImportFotoWareImageJob", "", ImportImage, postAction, selectedImage); } else { FotoWareFieldsLog.WriteToLog( "--- ImportFotoWareImageTest Job: ClientPipelineArgs is null ---"); } }
/// <summary> /// Generates a specific sitecore item name for selected image. /// </summary> /// <param name="selectedImage">Selected image</param> /// <returns>Item-name</returns> private string GetItemName(SelectedImageModel selectedImage) { try { var fullImageName = Path.GetFileName(selectedImage.ImageUrl.LocalPath); var imageNameArray = fullImageName.Split('.'); return(imageNameArray[0]); } catch (Exception e) { FotoWareFieldsLog.WriteToLog("--- Image Service: Could not generate item name for selected image: ---", e); return(string.Empty); } }
/// <inheritdoc /> public MediaItem UploadImage(SelectedImageModel selectedImage) { var contentLanguage = _languageRepository.GetContentLanguage(); try { return(_mediaRepository.UploadToMediaLibrary(selectedImage, GetItemName(selectedImage), contentLanguage)); } catch (Exception e) { FotoWareFieldsLog.WriteToLog("--- Image Service: FotoWare-Image could not be uploaded to sitecore media-library: ---", e); return(null); } }
/// <summary> /// Uploads the selected image to media-library and set value to image-field. /// </summary> /// <param name="args">ClientPipeline</param> protected void SelectFromFotoWare(ClientPipelineArgs args) { if (args.IsPostBack) { if (!args.HasResult || this.Value.Equals(args.Result)) { return; } SelectedImageModel selectedImage = null; if (args.Result == "Finished") { var selectedImageJson = args.Parameters[Templates.ImportImageClientPipeline.Parameters.SelectedImageJson]; selectedImage = _selectedImageModelMapper.MapJsonStringToSelectedImageModel(selectedImageJson); } else { selectedImage = _selectedImageModelMapper.MapJsonStringToSelectedImageModel(args.Result); args.Parameters.Add(Templates.ImportImageClientPipeline.Parameters.SelectedImageJson, args.Result); } if (selectedImage != null) { _fotoWareImageJob.StartJob(selectedImage, args, this.ImportImageFinished); } else { //image has to be exported to upload FotoWareFieldsLog.WriteToLog("--- FotoWareImageField: Selected image has to be exported before ---"); SheerResponse.Alert(_statusMessageService.GetImageMustBeExportedMessage()); } } else { _fotoWareImageJob.ResetStatus(); string url = UIUtil.GetUri("control:SelectImageFromFotoWareDialog"); string str = this.GetValue(); if (!string.IsNullOrEmpty(str)) { url = string.Format("{0}&value={1}", (object)url, (object)str); } SheerResponse.ShowModalDialog(url, "1200", "750", "", true, "1200", "750", false); args.WaitForPostBack(); } }
/// <summary> /// Sets the metadata from FotoWare for image-item. /// </summary> /// <param name="itemId">ID of Image</param> /// <param name="imageData">Metadata of Image</param> /// <param name="tenantUrl">Tenant-Url</param> /// <param name="language">Content-Language</param> /// <returns></returns> private MediaItem UpdateMetaData(ID itemId, SelectedImageModel imageData, string tenantUrl, Language language) { try { using (new SecurityDisabler()) { using (new LanguageSwitcher(language)) { var mediaItem = _database.GetItem(itemId); if (mediaItem != null) { mediaItem.Editing.BeginEdit(); mediaItem.Fields[Templates.ImageMediaItem.Fields.Software].Value = "FotoWare"; mediaItem.Fields[Templates.ImageMediaItem.Fields.LastModification].Value = imageData.LastModification.ToString(); mediaItem.Appearance.DisplayName = imageData.Title; mediaItem.Fields[Templates.ImageMediaItem.Fields.Alt].Value = imageData.Alt; mediaItem.Fields[Templates.ImageMediaItem.Fields.Title].Value = imageData.Title; mediaItem.Fields[Templates.ImageMediaItem.Fields.Keywords].Value = string.Join(" ", imageData.Keywords.ToArray()); mediaItem.Fields[Templates.ImageMediaItem.Fields.Description].Value = imageData.Description; mediaItem.Fields[Templates.ImageMediaItem.Fields.Height].Value = imageData.Height.ToString(); mediaItem.Fields[Templates.ImageMediaItem.Fields.Width].Value = imageData.Width.ToString(); mediaItem.Fields[Sitecore.Buckets.Util.Constants.BucketableField].Value = "1"; //make Item bucketable mediaItem.Fields[Templates.ImageMediaItem.Fields.FotoWareUrl].Value = string.Format("{0}|{1}", tenantUrl + imageData.MetaDataUrl, imageData.ImageUrl); mediaItem.Editing.EndEdit(); return(new MediaItem(mediaItem)); } } } } catch (Exception e) { FotoWareFieldsLog.WriteToLog("--- Media Repository: Metadata could not be updated: ---", e); return(null); } return(null); }
/// <summary> /// Imports the selected Image to media-library. /// </summary> /// <param name="args">ClientPipelineArgs</param> /// <param name="selectedImage">Selected image</param> private void ImportImage(ClientPipelineArgs args, SelectedImageModel selectedImage) { Job job = Sitecore.Context.Job; job.Status.State = JobState.Running; job.Status.AddMessage(_statusMessageService.GetJobStatusImporting()); var uploadedImage = _imageService.UploadImage(selectedImage); if (uploadedImage != null) { args.Parameters.Add(Templates.ImportImageClientPipeline.Parameters.MediaItemId, uploadedImage.ID.ToString()); job.Status.AddMessage(_statusMessageService.GetImageUploadSuccessfullyMessage()); job.Status.State = JobState.Finished; } else { job.Status.AddMessage(_statusMessageService.GetImageCouldNotUploadedMessage()); job.Status.LogError(_statusMessageService.GetImageCouldNotUploadedMessage()); FotoWareFieldsLog.WriteToLog( "--- ImportFotoWareImageProgressBar Job: Selected image could not be added to media library ---"); job.Status.Failed = true; } }
/// <summary> /// Starts the import job with progress bar. /// </summary> /// <param name="jobName">Job-Name</param> /// <param name="icon">Icon</param> /// <param name="action">Start-Action</param> /// <param name="postAction">Finish-Action</param> /// <param name="selectedImage">Selected image</param> private static void ExecuteSync(string jobName, string icon, Action <ClientPipelineArgs, SelectedImageModel> action, Action <ClientPipelineArgs, SelectedImageModel> postAction, SelectedImageModel selectedImage) { ClientPipelineArgs currentPipelineArgs = Context.ClientPage.CurrentPipelineArgs as ClientPipelineArgs; if (currentPipelineArgs == null) { return; } if (currentPipelineArgs.IsPostBack && !string.IsNullOrEmpty(HandleString)) { if (postAction != null) { postAction(currentPipelineArgs, selectedImage); } return; } string name = Client.Site.Name; object target = action.Target; string str = action.Method.Name; object[] objArray = new object[] { currentPipelineArgs, selectedImage }; JobOptions jobOption = new JobOptions(jobName, "FotoWareFields", name, target, str, objArray) { ContextUser = Context.User, ClientLanguage = Context.Language }; Job job = JobManager.Start(jobOption); LongRunningOptions longRunningOption = new LongRunningOptions(job.Handle.ToString()) { Title = "Import image from FotoWare", Icon = icon, Threshold = 5 }; HandleString = job.Handle.ToString(); longRunningOption.ShowModal(true); currentPipelineArgs.WaitForPostBack(); }
/// <inheritdoc /> public MediaItem UploadToMediaLibrary(SelectedImageModel selectedImage, string itemName, Language language) { var targetSitecorePath = Settings.GetSetting("Foundation.FotoWareExtensions.TargetPath", "/sitecore/media library/FotoWare"); var tenantUrl = Settings.GetSetting("Foundation.FotoWareExtensions.FotoWare.TenantUrl", string.Empty); if (!string.IsNullOrEmpty(tenantUrl)) { if (!string.IsNullOrEmpty(targetSitecorePath)) { try { var webRequest = WebRequest.Create(selectedImage.ImageUrl); using (var webResponse = webRequest.GetResponse()) { using (var stream = webResponse.GetResponseStream()) { using (var memoryStream = new MemoryStream()) { if (stream != null) { stream.CopyTo(memoryStream); } else { return(null); } var mediaCreator = new MediaCreator(); var options = new MediaCreatorOptions { Versioned = false, IncludeExtensionInItemName = false, Database = _database, Destination = string.Format("{0}/{1}", targetSitecorePath, itemName), AlternateText = selectedImage.Alt }; using (new SecurityDisabler()) { Item item = null; using (new EventDisabler()) { item = mediaCreator.CreateFromStream(memoryStream, Path.GetFileName(selectedImage.ImageUrl.LocalPath), options); } if (item != null) { return(UpdateMetaData(item.ID, selectedImage, tenantUrl, language)); } return(null); } } } } } catch (Exception e) { FotoWareFieldsLog.WriteToLog( "--- Media Repository: FotoWare-Image could not be uploaded to sitecore media-library: ---", e); return(null); } } else { FotoWareFieldsLog.WriteToLog("--- Media Repository: target-path-setting is not set in config-file. : ---"); return(null); } } else { FotoWareFieldsLog.WriteToLog("--- Media Repository: tenant-url-setting is not set in config-file. : ---"); return(null); } }