protected void uploadFile_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;
            HttpPostedFile fileUpld = ekFileUpload.PostedFile;
            string hasValidExtension = "";
            List<string> AllowedFileTypes = new List<string>();

            AllowedFileTypes.AddRange(DocumentManagerData.Instance.FileTypes.ToString().Split(','.ToString().ToCharArray()));
            if (fileUpld.ContentLength > 0)
            {
                Ektron.Cms.UserAPI uAPI = new UserAPI();

                _fileExtension = Path.GetExtension(fileUpld.FileName);
                //hasValidExtension = (string) (AllowedFileTypes.Find(new Predicate<string>(new System.EventHandler(CheckExtension))));
                hasValidExtension = AllowedFileTypes.Find(new Predicate<string>(CheckExtension));

                if ((hasValidExtension != null)&& hasValidExtension != "")
                {

                    //If Image Gallery, Should check if the file type is an image file type
                    if ((Request.QueryString["prop"] != null)&& Request.QueryString["prop"].ToLower() == "image")
                    {
                        if (! Ektron.Cms.Common.EkFunctions.IsImage(_fileExtension))
                        {
                            ltrStatus.Text = m_refMsg.GetMessage("msg invalid file upload images only");
                            setInvalid();
                            return;
                        }
                    }

                    fileName = Path.GetFileName(fileUpld.FileName);
                    int fileLength = fileUpld.ContentLength;
                    byte[] fileData = new byte[fileLength - 1+ 1];
                    string file = Convert.ToString(fileUpld.InputStream.Read(fileData, 0, fileLength));

                    if (fileData.Length > 0)
                    {
                        System.IO.MemoryStream stream = new System.IO.MemoryStream(fileData);
                        m_refContApi.RequestInformationRef.UserId = uAPI.UserId;
                        m_refContApi.ContentLanguage = _contentLanguage;

                        Ektron.ASM.AssetConfig.AssetData asstData = new Ektron.ASM.AssetConfig.AssetData();
                        Ektron.Cms.API.Content.Content cContent = new Ektron.Cms.API.Content.Content();
                        asstData = m_refContApi.EkContentRef.GetAssetDataBasedOnFileName(fileName, _folderID, _taxonomyID);
                        if ((asstData != null)&& asstData.ID != "" && asstData.Name != "")
                        {
                            Ektron.Cms.AssetUpdateData astData = new AssetUpdateData();
                            TaxonomyBaseData[] taxonomyCatArray = null;
                            _contentID = Convert.ToInt64(asstData.ID);
                            ContentData cData = cContent.GetContent(_contentID, Ektron.Cms.ContentAPI.ContentResultType.Published);

                            astData.FileName = fileName;
                            astData.FolderId = _folderID;
                            astData.ContentId = cData.Id;
                            astData.Teaser = cData.Teaser;
                            astData.Comment = cData.Comment;
                            astData.Title = cData.Title;
                            astData.GoLive = cData.GoLive;
                            astData.TaxonomyTreeIds = this._taxonomyID.ToString();

                            //Assigning the categories
                            taxonomyCatArray = m_refContApi.ReadAllAssignedCategory(_contentID);
                            if ((taxonomyCatArray != null)&& taxonomyCatArray.Length > 0)
                            {
                                foreach (TaxonomyBaseData tBaseData in taxonomyCatArray)
                                {
                                    if (astData.TaxonomyTreeIds == "")
                                    {
                                        astData.TaxonomyTreeIds = tBaseData.TaxonomyId.ToString();
                                    }
                                    else
                                    {
                                        astData.TaxonomyTreeIds += (string) ("," + tBaseData.TaxonomyId.ToString());
                                    }
                                }
                            }

                            //Assigning the metadatas
                            if ((cData.MetaData != null)&& cData.MetaData.Length > 0)
                            {
                                astData.MetaData = new Ektron.Cms.AssetUpdateMetaData[cData.MetaData.Length - 1 + 1];
                                for (int i = 0; i <= cData.MetaData.Length - 1; i++)
                                {
                                    astData.MetaData[i] = new AssetUpdateMetaData();
                                    astData.MetaData[i].TypeId = cData.MetaData[i].TypeId;
                                    astData.MetaData[i].ContentId = cData.Id;
                                    astData.MetaData[i].Text = cData.MetaData[i].Text;
                                }
                            }
                            astData.EndDate = cData.EndDate;
                            astData.EndDateAction = (Ektron.Cms.Common.EkEnumeration.CMSEndDateAction) (Enum.Parse(typeof(Ektron.Cms.Common.EkEnumeration.CMSEndDateAction), cData.EndDateAction.ToString(), true));

                            //Updating the Content
                            bool isUpdated = m_refContApi.EditAsset(stream, astData);
                        }
                        else
                        {
                            Ektron.Cms.AssetUpdateData astData = new AssetUpdateData();
                            astData.FileName = fileName;
                            astData.FolderId = _folderID;
                            astData.Title = Path.GetFileNameWithoutExtension(fileName);
                            astData.LanguageId = _contentLanguage;
                            astData.TaxonomyTreeIds = this._taxonomyID.ToString();
                            _contentID = m_refContApi.AddAsset(stream, astData);
                        }

                        jsMetaUrl.Text = "";
                        if (m_refContApi.EkContentRef.DoesFolderRequireMetadataOrTaxonomy(_folderID, _contentLanguage))
                        {
                            var _taxString = string.Empty;
                            if (this._taxonomyID != -1)
                            {
                                _taxString = (string) ("&taxonomyId=" + this._taxonomyID);
                            }
                            jsMetaUrl.Text = m_refContApi.AppPath + "DMSMetadata.aspx?contentId=" + _contentID + "&idString=" + _contentID + "&folderId=" + _folderID + _taxString + "&close=true&EkTB_iframe=true&height=550&width=650&modal=true&refreshCaller=true";
                        }

                        isFileUploadComplete.Value = "true";
                        ClientScript.RegisterStartupScript(this.GetType(), "closeThickBox", "uploadClick();", true);
                    }
                }
                else
                {
                    ltrStatus.Text = m_refMsg.GetMessage("msg invalid file upload");
                    setInvalid();
                }
            }
            else
            {
                ltrStatus.Text = m_refMsg.GetMessage("lbl upload file");
                setInvalid();
            }
    }
예제 #2
0
    protected void uploadFile_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;
        HttpPostedFile fileUpld = ekFileUpload.PostedFile;
        string hasValidExtension = "";
        List<string> AllowedFileTypes = new List<string>();
        bool createOverwriteThumbnail = false;
        bool isImageAsset = false;
        ContentData cData;

        long _checkTaxID = -1;
        AllowedFileTypes.AddRange(DocumentManagerData.Instance.FileTypes.ToString().Split(','));
        if (fileUpld.ContentLength > 0)
        {
            Ektron.Cms.UserAPI uAPI = new UserAPI();

            _fileExtension = Path.GetExtension(fileUpld.FileName);

            //If its an image asset then create/overwrite the thumbnail
            isImageAsset = Ektron.Cms.Common.EkFunctions.IsImage(_fileExtension);
            if (isImageAsset && !isMetadataOrTaxonomyRequired)
                createOverwriteThumbnail = true;

            hasValidExtension = AllowedFileTypes.Find(new Predicate<string>(delegate(string t) { return t.ToLower().Replace(" ", "") == ("*" + _fileExtension.ToLower()); }));
            if (hasValidExtension != null && hasValidExtension != "")
            {
                //If Image Gallery, Should check if the file type is an image file type
                if (Request.QueryString["isimage"] != null && Request.QueryString["isimage"] != "" && Convert.ToInt32(Request.QueryString["isimage"]) == 1)
                {
                    if (!isImageAsset)
                    {
                        _bTrue = false;
                        ltrStatus.Text = _messageHelper.GetMessage("msg invalid file upload images only");
                        setInvalid();
                    }

                }

                fileName = Path.GetFileName(fileUpld.FileName);
                if (fileName.IndexOf("&") > -1 || fileName.IndexOf("+") > -1 || fileName.IndexOf("%") > -1)
                {
                    _bTrue = false;
                    ltrStatus.Text = _messageHelper.GetMessage("msg cannot add file with add and plus");
                    setInvalid();
                }

                if (_bTrue)
                {
                    int fileLength = fileUpld.ContentLength;
                    byte[] fileData = new byte[fileLength];
                    string file = Convert.ToString(fileUpld.InputStream.Read(fileData, 0, fileLength));

                    if (fileData.Length > 0)
                    {
                        System.IO.Stream stream = new System.IO.MemoryStream(fileData);

                        if (isImageAsset)
                        {
                            if (!EkFunctions.isImageStreamValid(stream))
                            {
                                stream.Flush();
                                stream.Close();
                                setInvalid();
                                ltrStatus.Text = "The image is corrupted or not in correct format.";
                                return;
                            }
                            stream.Position = 0;
                        }

                        contentAPI.RequestInformationRef.UserId = uAPI.UserId;
                        contentAPI.ContentLanguage = _contentLanguage;

                        Ektron.ASM.AssetConfig.AssetData asstData = new Ektron.ASM.AssetConfig.AssetData();
                        Ektron.Cms.API.Content.Content cContent = new Ektron.Cms.API.Content.Content();
                        if (_isImageGallery)
                            _checkTaxID = Convert.ToInt64(_taxonomyIdList);
                        asstData = contentAPI.EkContentRef.GetAssetDataBasedOnFileName(fileName.Replace("'", "_"), _folderID, _checkTaxID);
                        if ((asstData != null && asstData.ID != "" && asstData.Name != "") || !String.IsNullOrEmpty(Request.QueryString["AssetID"]))
                        {
                            Ektron.Cms.AssetUpdateData astData = new AssetUpdateData();
                            TaxonomyBaseData[] taxonomyCatArray = null;
                            if (!String.IsNullOrEmpty(Request.QueryString["AssetID"]))
                                _contentID = Convert.ToInt64(Request.QueryString["AssetID"]);
                            else
                                _contentID = Convert.ToInt64(asstData.ID);
                            cData = cContent.GetContent(_contentID, Ektron.Cms.ContentAPI.ContentResultType.Published);

                            astData.FileName = fileName;
                            astData.FolderId = _folderID;
                            astData.ContentId = cData.Id;
                            astData.Teaser = cData.Teaser;
                            astData.Comment = cData.Comment;
                            astData.Title = cData.Title;
                            astData.GoLive = cData.GoLive;

                            //Assigning the categories
                            taxonomyCatArray = contentAPI.ReadAllAssignedCategory(_contentID);
                            if (taxonomyCatArray != null && taxonomyCatArray.Length > 0)
                            {
                                foreach (TaxonomyBaseData tBaseData in taxonomyCatArray)
                                {
                                    if (astData.TaxonomyTreeIds == "")
                                        astData.TaxonomyTreeIds = tBaseData.TaxonomyId.ToString();
                                    else
                                        astData.TaxonomyTreeIds += "," + tBaseData.TaxonomyId.ToString();
                                }
                            }

                            //Assigning the metadata
                            if (cData.MetaData != null && cData.MetaData.Length > 0)
                            {
                                astData.MetaData = new AssetUpdateMetaData[cData.MetaData.Length - 1];
                                for (int i = 0; i < cData.MetaData.Length - 1; i++)
                                {
                                    astData.MetaData[i] = new AssetUpdateMetaData();
                                    astData.MetaData[i].TypeId = cData.MetaData[i].TypeId;
                                    astData.MetaData[i].ContentId = cData.Id;
                                    astData.MetaData[i].Text = cData.MetaData[i].Text;
                                }
                            }
                            astData.EndDate = cData.EndDate;
                            astData.EndDateAction = (Ektron.Cms.Common.EkEnumeration.CMSEndDateAction)cData.EndDateAction;

                            //Updating the Content
                            bool isUpdated = contentAPI.EditAsset(stream, astData);

                            //Creating the thumbnail, as service takes a while to generate and we see a broken Image in that time.
                            if (!isUpdated && createOverwriteThumbnail)
                            {
                                cData = contentAPI.ShowContentById(cData.Id, contentAPI.CmsPreview, !contentAPI.CmsPreview);
                                if (cData.Status.ToLower() == "a")
                                    CreateThumbNailIfOneDoesntExist(GetPath(cData), 125, true);
                            }
                        }
                        else
                        {
                            Ektron.Cms.AssetUpdateData astData = new AssetUpdateData();
                            astData.FileName = fileName;
                            astData.FolderId = _folderID;
                            astData.TaxonomyTreeIds = _taxonomyIdList;
                            astData.Teaser = FillImageGalleryDescription();
                            astData.Title = Path.GetFileNameWithoutExtension(fileName);
                            astData.LanguageId = _contentLanguage;
                            _contentID = contentAPI.AddAsset(stream, astData);
                            //----------------searchable---------------------
                            IsContentSearchableSection(_contentID);
                            //----------------searchableEnd------------------
                            //Creating the thumbnail, as service takes a while to generate and we see a broken Image in that time.
                            if (_contentID > 0 && createOverwriteThumbnail)
                            {
                                cData = contentAPI.ShowContentById(_contentID, contentAPI.CmsPreview, !contentAPI.CmsPreview);
                                if (cData.Status.ToLower() == "a")
                                    CreateThumbNailIfOneDoesntExist(GetPath(cData), 125, false);
                            }
                        }
                        jsMetaUrl.Text = "";

                        if (isMetadataOrTaxonomyRequired || (isUrlAliasRequired && !EkFunctions.IsImage(Path.GetExtension(fileName))))
                        {
                            //put item into check-in state:
                            contentAPI.EkContentRef.CheckContentOutv2_0(_contentID);
                            contentAPI.EkContentRef.CheckIn(_contentID, "");

                            string _taxString = string.Empty;
                            if (_taxonomyIdList != "")
                                _taxString = "&taxonomyId=" + _taxonomyIdList;
                            jsMetaUrl.Text = contentAPI.AppPath + "DMSMetadata.aspx?contentId=" + _contentID + "&idString=" + _contentID + "&folderId=" + _folderID + _taxString + "&close=true&EkTB_iframe=true&height=550&width=650&modal=true&refreshCaller=true"; ;
                        }

                        isFileUploadComplete.Value = "true";
                        ClientScript.RegisterStartupScript(this.GetType(), "closeThickBox", "uploadClick();", true);
                    }
                }
            }
            else
            {
                setInvalid();
                ltrStatus.Text = _messageHelper.GetMessage("msg invalid file upload");
            }
        }
        else
        {
            setInvalid();
            ltrStatus.Text = _messageHelper.GetMessage("lbl upload file");
        }
    }