private async Task <string> ReplaceMediaLink(Match match)
        {
            var start = Convert.ToString(match.Groups["start"]);
            var url   = HttpUtility.HtmlDecode(Convert.ToString(match.Groups["url"]));
            var query = HttpUtility.HtmlDecode(Convert.ToString(match.Groups["query"]));
            var end   = Convert.ToString(match.Groups["end"]);

            try
            {
                // We need to set current site before every call to GetMediaData to avoid null reference
                SiteContext.CurrentSiteName = _settings.Sitename;
                var data = CMSDialogHelper.GetMediaData(url, _settings.Sitename);
                if (data != null)
                {
                    switch (data.SourceType)
                    {
                    case MediaSourceEnum.Attachment:
                    case MediaSourceEnum.DocumentAttachments:
                    {
                        var assetUrl = await _assetSync.GetAssetUrl("attachment", data.AttachmentGuid);

                        var newQuery = TranslateMediaQuery(query);

                        return($"{start}{HttpUtility.HtmlEncode(assetUrl)}{HttpUtility.HtmlEncode(newQuery)}{end}");
                    }

                    case MediaSourceEnum.MediaLibraries:
                    {
                        var assetUrl = await _assetSync.GetAssetUrl("media", data.MediaFileGuid);

                        var newQuery = TranslateMediaQuery(query);

                        return($"{start}{HttpUtility.HtmlEncode(assetUrl)}{HttpUtility.HtmlEncode(newQuery)}{end}");
                    }
                    }
                }
            }
            catch (Exception ex)
            {
                SyncLog.LogException("KenticoKontentPublishing", "TRANSLATEURL", ex, 0, $"Failed to replace media URL '{url + query}', keeping the original URL.");
            }

            // Keep as it is if translation is not successful, only resolve to absolute URL if needed
            if (url.StartsWith("~"))
            {
                return($"{start}{HttpUtility.HtmlEncode(_settings.WebRoot)}{HttpUtility.HtmlEncode(url.Substring(1))}{HttpUtility.HtmlEncode(query)}{end}");
            }
            return(match.ToString());
        }
示例#2
0
    /// <summary>
    /// Configures the preview control.
    /// </summary>
    private void LoadPreview()
    {
        string url = txtUrl.Text;

        if (!string.IsNullOrEmpty(url))
        {
            bool        isImage = false;
            MediaSource source  = CMSDialogHelper.GetMediaData(url, null);
            if (source != null)
            {
                isImage = ImageHelper.IsImage(source.Extension);
            }

            // Only for image extensions add chset to url
            if (isImage)
            {
                url = URLHelper.UpdateParameterInUrl(url, "chset", Guid.NewGuid().ToString());

                int versionHistoryId = HistoryID;
                if (IsLiveSite && (versionHistoryId > 0))
                {
                    // Add requirement for latest version of files for current document
                    string newparams = "latestforhistoryid=" + versionHistoryId;
                    newparams += "&hash=" + ValidationHelper.GetHashString("h" + versionHistoryId, new HashSettings(""));

                    url += "&" + newparams;
                }
            }

            imagePreview.Visible   = true;
            imagePreview.URL       = url;
            imagePreview.SizeToURL = ValidationHelper.GetBoolean(ViewState[DialogParameters.IMG_SIZETOURL], false);
            imagePreview.Width     = widthHeightElem.Width > DefaultWidth ? DefaultWidth : widthHeightElem.Width;
            imagePreview.Height    = widthHeightElem.Height > DefaultHeight ? DefaultHeight : widthHeightElem.Height;

            SaveSession();
        }
        else
        {
            imagePreview.Visible = false;
        }
    }
    protected void imgRefresh_Click(object sender, EventArgs e)
    {
        if (this.plcMediaType.Visible)
        {
            MediaSource source = CMSDialogHelper.GetMediaData(this.txtUrl.Text, null);
            if ((source == null) || (source.MediaType == MediaTypeEnum.Unknown))
            {
                this.Properties.ItemNotSystem = true;

                // Try get source type from URL extension
                int index = this.txtUrl.Text.LastIndexOf('.');
                if (index > 0)
                {
                    string ext = this.txtUrl.Text.Substring(index);
                    if (ext.Contains("?"))
                    {
                        ext = URLHelper.RemoveQuery(ext);
                    }
                    if (source == null)
                    {
                        source = new MediaSource();
                    }
                    source.Extension = ext;
                    if (source.MediaType == MediaTypeEnum.Image)
                    {
                        try
                        {
                            // Get the data
                            WebClient   wc  = new WebClient();
                            byte[]      img = wc.DownloadData(this.txtUrl.Text.Trim());
                            ImageHelper ih  = new ImageHelper(img);
                            if (ih.ImageWidth > 0)
                            {
                                this.mWidth = ih.ImageWidth;
                            }
                            if (ih.ImageHeight > 0)
                            {
                                this.mHeight = ih.ImageHeight;
                            }
                            wc.Dispose();
                        }
                        catch { }
                    }
                    else
                    {
                        source.MediaWidth  = 300;
                        source.MediaHeight = 200;
                    }
                }
            }
            else
            {
                this.Properties.ItemNotSystem = false;
            }

            if (source != null)
            {
                // Set default dimensions when not specified
                if ((this.mWidth == 0) && (this.mHeight == 0))
                {
                    this.mWidth  = source.MediaWidth;
                    this.mHeight = source.MediaHeight;
                }
                switch (source.MediaType)
                {
                case MediaTypeEnum.Image:
                    this.drpMediaType.SelectedValue = "image";
                    break;

                case MediaTypeEnum.AudioVideo:
                    this.drpMediaType.SelectedValue = "av";
                    break;

                case MediaTypeEnum.Flash:
                    this.drpMediaType.SelectedValue = "flash";
                    break;

                default:
                    this.drpMediaType.SelectedValue = "";
                    this.plcInfo.Visible            = true;
                    this.lblInfo.ResourceString     = "dialogs.web.selecttype";
                    break;
                }
            }

            if (source != null)
            {
                SetLastType(source.MediaType);
            }

            ShowProperties();
        }
    }
示例#4
0
    /// <summary>
    /// Setup media dialog from selected item.
    /// </summary>
    /// <param name="selectionTable">Hash table from selected item</param>
    /// <param name="anchorsList">List of anchors from document</param>
    /// <param name="idsList">List of ids from document</param>
    private void SelectMediaDialog(IDictionary selectionTable, ICollection anchorsList, ICollection idsList)
    {
        string insertHeaderLocation = BaseFilePath + "Header.aspx" + RequestContext.CurrentQueryString;

        if (selectionTable.Count > 0)
        {
            string siteName = null;
            string url      = null;
            // If link dialog use only link url
            if (RequestContext.CurrentQueryString.ToLowerCSafe().Contains("link=1"))
            {
                if (selectionTable[DialogParameters.LINK_URL] != null)
                {
                    url = selectionTable[DialogParameters.LINK_URL].ToString();
                    if ((selectionTable[DialogParameters.LINK_PROTOCOL] != null) && (selectionTable[DialogParameters.LINK_PROTOCOL].ToString() != "other"))
                    {
                        // Add protocol only if not already presents
                        if (!url.StartsWithCSafe(selectionTable[DialogParameters.LINK_PROTOCOL].ToString()))
                        {
                            url = selectionTable[DialogParameters.LINK_PROTOCOL] + url;
                        }
                    }
                }
                else if (selectionTable[DialogParameters.URL_URL] != null)
                {
                    url = selectionTable[DialogParameters.URL_URL].ToString();
                }
            }
            else
            {
                // Get url from selection table
                if (selectionTable[DialogParameters.IMG_URL] != null)
                {
                    url = selectionTable[DialogParameters.IMG_URL].ToString();
                }
                else if (selectionTable[DialogParameters.FLASH_URL] != null)
                {
                    url = selectionTable[DialogParameters.FLASH_URL].ToString();
                }
                else if (selectionTable[DialogParameters.AV_URL] != null)
                {
                    url = selectionTable[DialogParameters.AV_URL].ToString();
                }
                else if (selectionTable[DialogParameters.LINK_URL] != null)
                {
                    url = selectionTable[DialogParameters.LINK_URL].ToString();
                }
                else if (selectionTable[DialogParameters.URL_URL] != null)
                {
                    url      = selectionTable[DialogParameters.URL_URL].ToString();
                    siteName = (selectionTable[DialogParameters.URL_SITENAME] != null ? selectionTable[DialogParameters.URL_SITENAME].ToString() : null);
                }
            }
            string query = URLHelper.RemoveUrlParameter(RequestContext.CurrentQueryString, "hash");

            // Get the data for media source
            MediaSource ms = CMSDialogHelper.GetMediaData(url, siteName);
            if (ms != null)
            {
                SessionHelper.SetValue("MediaSource", ms);

                // Preselect the tab
                if (!selectionTable.Contains(DialogParameters.EMAIL_TO) || !selectionTable.Contains(DialogParameters.ANCHOR_NAME))
                {
                    switch (ms.SourceType)
                    {
                    case MediaSourceEnum.DocumentAttachments:
                    case MediaSourceEnum.MetaFile:
                        query = URLHelper.AddUrlParameter(query, "tab", "attachments");
                        break;

                    case MediaSourceEnum.Content:
                        query = URLHelper.AddUrlParameter(query, "tab", "content");
                        break;

                    case MediaSourceEnum.MediaLibraries:
                        query = URLHelper.AddUrlParameter(query, "tab", "libraries");
                        break;

                    default:
                        query = URLHelper.AddUrlParameter(query, "tab", "web");
                        break;
                    }
                }

                // Update old format url
                if ((selectionTable.Contains(DialogParameters.URL_OLDFORMAT)) && (selectionTable.Contains(DialogParameters.URL_GUID)))
                {
                    if (String.IsNullOrEmpty(siteName))
                    {
                        siteName = SiteContext.CurrentSiteName;
                    }
                    string outUrl = ModuleCommands.MediaLibraryGetMediaFileUrl(selectionTable[DialogParameters.URL_GUID].ToString(), siteName);
                    if (!String.IsNullOrEmpty(outUrl))
                    {
                        selectionTable[DialogParameters.URL_URL] = outUrl;
                    }
                }

                // Set extension if not exist in selection table
                if ((selectionTable[DialogParameters.URL_EXT] == null) || ((selectionTable[DialogParameters.URL_EXT] != null) && (String.IsNullOrEmpty(selectionTable[DialogParameters.URL_EXT].ToString()))))
                {
                    selectionTable[DialogParameters.URL_EXT] = ms.Extension;
                }

                // Update selection table if only URL presents
                if (selectionTable.Contains(DialogParameters.URL_URL))
                {
                    switch (ms.MediaType)
                    {
                    case MediaTypeEnum.Image:
                        // Image
                        selectionTable[DialogParameters.IMG_URL]    = UrlResolver.ResolveUrl(selectionTable[DialogParameters.URL_URL].ToString());
                        selectionTable[DialogParameters.IMG_WIDTH]  = selectionTable[DialogParameters.URL_WIDTH];
                        selectionTable[DialogParameters.IMG_HEIGHT] = selectionTable[DialogParameters.URL_HEIGHT];
                        break;

                    case MediaTypeEnum.AudioVideo:
                        // Media
                        selectionTable[DialogParameters.AV_URL]    = UrlResolver.ResolveUrl(selectionTable[DialogParameters.URL_URL].ToString());
                        selectionTable[DialogParameters.AV_WIDTH]  = selectionTable[DialogParameters.URL_WIDTH];
                        selectionTable[DialogParameters.AV_HEIGHT] = selectionTable[DialogParameters.URL_HEIGHT];
                        selectionTable[DialogParameters.AV_EXT]    = ms.Extension;
                        break;

                    case MediaTypeEnum.Flash:
                        // Flash
                        selectionTable[DialogParameters.FLASH_URL]    = UrlResolver.ResolveUrl(selectionTable[DialogParameters.URL_URL].ToString());
                        selectionTable[DialogParameters.FLASH_WIDTH]  = selectionTable[DialogParameters.URL_WIDTH];
                        selectionTable[DialogParameters.FLASH_HEIGHT] = selectionTable[DialogParameters.URL_HEIGHT];
                        break;
                    }

                    if ((ms.SourceType == MediaSourceEnum.Content) && (ms.FileName != null) && (OutputFormat == OutputFormatEnum.NodeGUID))
                    {
                        string fileUrl = AttachmentURLProvider.GetPermanentAttachmentUrl(ms.NodeGuid, ms.FileName);
                        selectionTable[DialogParameters.URL_URL] = UrlResolver.ResolveUrl(fileUrl);
                    }
                    else if (OutputFormat != OutputFormatEnum.URL)
                    {
                        selectionTable[DialogParameters.URL_URL] = UrlResolver.ResolveUrl(selectionTable[DialogParameters.URL_URL].ToString());
                    }

                    selectionTable[DialogParameters.FILE_NAME] = ms.FileName;
                    selectionTable[DialogParameters.FILE_SIZE] = ms.FileSize;
                }

                // Add original size into table
                selectionTable[DialogParameters.IMG_ORIGINALWIDTH]  = ms.MediaWidth;
                selectionTable[DialogParameters.IMG_ORIGINALHEIGHT] = ms.MediaHeight;
            }
            else
            {
                if (selectionTable.Contains(DialogParameters.EMAIL_TO))
                {
                    query = URLHelper.AddUrlParameter(query, "tab", "email");
                }
                if (selectionTable.Contains(DialogParameters.ANCHOR_NAME))
                {
                    query = URLHelper.AddUrlParameter(query, "tab", "anchor");
                }
            }

            query = URLHelper.AddUrlParameter(query, "hash", QueryHelper.GetHash(query));
            insertHeaderLocation = BaseFilePath + "Header.aspx" + query;
        }


        // Set selected item into session
        SessionHelper.SetValue("DialogParameters", selectionTable);

        if ((anchorsList != null) && (anchorsList.Count > 0))
        {
            SessionHelper.SetValue("Anchors", anchorsList);
        }
        if ((idsList != null) && (idsList.Count > 0))
        {
            SessionHelper.SetValue("Ids", idsList);
        }

        if (((selectionTable[DialogParameters.LINK_TEXT] != null) &&
             (selectionTable[DialogParameters.LINK_TEXT].ToString() == "##LINKTEXT##")) ||
            ((selectionTable[DialogParameters.EMAIL_LINKTEXT] != null) &&
             (selectionTable[DialogParameters.EMAIL_LINKTEXT].ToString() == "##LINKTEXT##")) ||
            ((selectionTable[DialogParameters.ANCHOR_LINKTEXT] != null) &&
             (selectionTable[DialogParameters.ANCHOR_LINKTEXT].ToString() == "##LINKTEXT##")))
        {
            SessionHelper.SetValue("HideLinkText", true);
        }

        ltlScript.Text = ScriptHelper.GetScript("if (window.parent.frames['insertHeader']) { window.parent.frames['insertHeader'].location= \"" + ResolveUrl(insertHeaderLocation) + "\";} ");
    }
    protected void imgRefresh_Click(object sender, EventArgs e)
    {
        if (plcMediaType.Visible)
        {
            MediaSource source = CMSDialogHelper.GetMediaData(txtUrl.Text, null);
            if ((source == null) || (source.MediaType == MediaTypeEnum.Unknown))
            {
                ItemProperties.ItemNotSystem = true;

                // Try get source type from URL extension
                int index = txtUrl.Text.LastIndexOfCSafe('.');
                if (index > 0)
                {
                    string ext = txtUrl.Text.Substring(index);
                    if (ext.Contains("?"))
                    {
                        ext = URLHelper.RemoveQuery(ext);
                    }
                    if (source == null)
                    {
                        source = new MediaSource();
                    }
                    source.Extension = ext;
                    if (source.MediaType == MediaTypeEnum.Image)
                    {
                        // Get the data
                        GetExternalImageDimensions(txtUrl.Text.Trim());
                    }
                    else
                    {
                        source.MediaWidth  = 300;
                        source.MediaHeight = 200;
                    }
                }
            }
            else
            {
                ItemProperties.ItemNotSystem = false;
            }

            if (source != null)
            {
                // Set default dimensions when not specified
                if ((mWidth == 0) && (mHeight == 0))
                {
                    mWidth  = source.MediaWidth;
                    mHeight = source.MediaHeight;
                }
                switch (source.MediaType)
                {
                case MediaTypeEnum.Image:
                    drpMediaType.SelectedValue = "image";
                    break;

                case MediaTypeEnum.AudioVideo:
                    drpMediaType.SelectedValue = "av";
                    break;

                case MediaTypeEnum.Flash:
                    drpMediaType.SelectedValue = "flash";
                    break;

                default:
                    drpMediaType.SelectedValue = "";
                    plcInfo.Visible            = true;
                    lblInfo.ResourceString     = "dialogs.web.selecttype";
                    break;
                }
            }

            if (source != null)
            {
                SetLastType(source.MediaType);
            }

            ShowProperties();
        }
    }