private AssetFileData GetAssetFile(string name, bool isPrimary)
        {
            AssetFileData returnValue = new AssetFileData();

            returnValue.Name      = name;
            returnValue.IsPrimary = isPrimary;

            return(returnValue);
        }
Beispiel #2
0
        public void TestAssetFileUpdateRetry()
        {
            var dataContextMock = new Mock <IMediaDataServiceContext>();

            int exceptionCount = 2;

            var asset = new AssetData {
                Name = "testData"
            };
            var file = new AssetFileData {
                Name = "testData"
            };
            var fakeResponse = new TestMediaDataServiceResponse {
                AsyncState = file
            };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Files", file));
            dataContextMock.Setup((ctxt) => ctxt.UpdateObject(file));

            dataContextMock.Setup((ctxt) => ctxt
                                  .SaveChangesAsync(file))
            .Returns(() => Task.Factory.StartNew <IMediaDataServiceResponse>(() =>
            {
                if (--exceptionCount > 0)
                {
                    throw fakeException;
                }
                return(fakeResponse);
            }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            file.SetMediaContext(_mediaContext);
            SetFileAsset(file, asset);

            file.Update();

            Assert.AreEqual(0, exceptionCount);
        }
Beispiel #3
0
        public void TestAssetFileCreateRetry()
        {
            var dataContextMock = new Mock <IMediaDataServiceContext>();

            int exceptionCount = 2;

            var expected = new AssetFileData {
                Name = "testData"
            };
            var fakeResponse = new TestMediaDataServiceResponse {
                AsyncState = expected
            };
            var fakeException = new WebException("testException", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AddObject("Files", It.IsAny <object>()));
            dataContextMock.Setup((ctxt) => ctxt
                                  .SaveChangesAsync(It.IsAny <object>()))
            .Returns(() => Task.Factory.StartNew <IMediaDataServiceResponse>(() =>
            {
                if (--exceptionCount > 0)
                {
                    throw fakeException;
                }
                return(fakeResponse);
            }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            var asset = new AssetData {
                Name = "testData"
            };

            asset.SetMediaContext(_mediaContext);
            IAssetFile file = ((IAsset)asset).AssetFiles.Create("test");

            Assert.AreEqual(expected.Name, file.Name);
            Assert.AreEqual(0, exceptionCount);
        }
 private void SetFileAsset(AssetFileData file, IAsset asset)
 {
     typeof(AssetFileData)
         .GetField("_asset", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
         .SetValue(file, asset);
 }
        public void TestAssetFileUpdateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var asset = new AssetData { Name = "testData"};
            var file = new AssetFileData { Name = "testData"};
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = file };
            var fakeException = new WebException("test", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AttachTo("Files", file));
            dataContextMock.Setup((ctxt) => ctxt.UpdateObject(file));

            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(file))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            file.SetMediaContext(_mediaContext);
            SetFileAsset(file, asset);

            file.Update();

            Assert.AreEqual(0, exceptionCount);
        }
        public void TestAssetFileCreateRetry()
        {
            var dataContextMock = new Mock<IMediaDataServiceContext>();

            int exceptionCount = 2;

            var expected = new AssetFileData { Name = "testData" };
            var fakeResponse = new TestMediaDataServiceResponse { AsyncState = expected };
            var fakeException = new WebException("testException", WebExceptionStatus.ConnectionClosed);

            dataContextMock.Setup((ctxt) => ctxt.AddObject("Files", It.IsAny<object>()));
            dataContextMock.Setup((ctxt) => ctxt
                .SaveChangesAsync(It.IsAny<object>()))
                .Returns(() => Task.Factory.StartNew<IMediaDataServiceResponse>(() =>
                {
                    if (--exceptionCount > 0) throw fakeException;
                    return fakeResponse;
                }));

            _mediaContext.MediaServicesClassFactory = new TestMediaServicesClassFactory(dataContextMock.Object);

            var asset = new AssetData { Name = "testData" };

            asset.SetMediaContext(_mediaContext);
            IAssetFile file = ((IAsset)asset).AssetFiles.Create("test");
            Assert.AreEqual(expected.Name, file.Name);
            Assert.AreEqual(0, exceptionCount);
        }
        private AssetFileData GetAssetFile(string name, bool isPrimary)
        {
            AssetFileData returnValue = new AssetFileData();

            returnValue.Name = name;
            returnValue.IsPrimary = isPrimary;

            return returnValue;
        }
Beispiel #8
0
 private void SetFileAsset(AssetFileData file, IAsset asset)
 {
     typeof(AssetFileData)
     .GetField("_asset", BindingFlags.NonPublic | BindingFlags.Instance)
     .SetValue(file, asset);
 }
    protected void Page_Load(object sender, System.EventArgs e)
    {
        long userId = Convert.ToInt64(CommonApi.GetEcmCookie()["user_id"]);
            int loginToken = System.Convert.ToInt32(Ektron.Cms.CommonApi.GetEcmCookie()["unique_id"]);
            string rejectedFiles = "";
            if (Request.Files != null&& Request.Files.Count > 0)
            {
                List<AssetFileData> assetIdFileNameList = new List<AssetFileData>();
                string typesStr = DocumentManagerData.Instance.FileTypes.Replace("*", "");
                string filetypes = "";
                if (Request.QueryString["isimage"] != null && Request.QueryString["isimage"] == "1")
                {
                    string[] AllowedFileTypes = null;
                    if (DocumentManagerData.Instance.FileTypes.Length > 0)
                    {
                        AllowedFileTypes = DocumentManagerData.Instance.FileTypes.Split(',');
                        if (AllowedFileTypes != null && AllowedFileTypes.Length > 0)
                        {
                            foreach (string filetype in AllowedFileTypes)
                            {
                                if (EkFunctions.IsImage(filetype.Trim().Replace("*", "")))
                                {
                                    if (filetypes.Length > 0)
                                        filetypes += "," + filetype;
                                    else
                                        filetypes = filetype;
                                }
                            }
                        }
                    }
                    fileTypeCol = new List<string>(filetypes.ToString().Replace("*","").Split(",".ToCharArray()));
                }
                else
                {
                    fileTypeCol = new List<string>(typesStr.ToString().Split(",".ToCharArray()));
                }

                for (int fileIndex = 0; fileIndex <= Request.Files.Count - 1; fileIndex++)
                {
                    HttpPostedFile postedFile = Request.Files[fileIndex];
                    Context.Cache[userId + loginToken + "RejectedFiles"] = "";
                    if (ValidFileType(Path.GetExtension(postedFile.FileName)) && IsValidFileName(Path.GetFileNameWithoutExtension(postedFile.FileName)))
                    {
                        try
                        {
                            AssetFileData fileData = new AssetFileData();
                            string assetId = System.Guid.NewGuid().ToString();
                            string docFilePath = DocumentManagerData.Instance.WebSharePath;
                            if (! System.IO.Path.IsPathRooted(docFilePath))
                            {
                                docFilePath = Ektron.ASM.AssetConfig.Utilities.UrlHelper.GetAppPhysicalPath() + docFilePath;
                            }
                            string destFileName = docFilePath + Path.GetFileName(postedFile.FileName) + assetId;

                            using (BinaryReader br = new BinaryReader(postedFile.InputStream))
                            {
                                byte[] buf;
                                using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(destFileName, FileMode.Create)))
                                {
                                    buf = br.ReadBytes(bufSize);
                                    if (Ektron.Cms.Common.EkFunctions.IsImage(Path.GetExtension(postedFile.FileName)))
                                    {
                                        Stream streamTemp = new MemoryStream(buf);
                                        if (!Ektron.Cms.Common.EkFunctions.isImageStreamValid(streamTemp))
                                        {
                                            if (rejectedFiles.Length > 0)
                                            {
                                                rejectedFiles += (string)(", " + postedFile.FileName);
                                            }
                                            else
                                            {
                                                rejectedFiles = postedFile.FileName;
                                            }
                                            continue;
                                        }
                                    }

                                    int index = 0;
                                    while (buf.Length > 0)
                                    {
                                        binaryWriter.Write(buf, 0, buf.Length);
                                        index += buf.Length;
                                        buf = br.ReadBytes(bufSize);
                                    }
                                    binaryWriter.Flush();
                                    binaryWriter.Close();
                                }

                                br.Close();
                            }

                            fileData.AssetId = assetId;
                            fileData.FileName = postedFile.FileName;
                            assetIdFileNameList.Add(fileData);
                        }
                        catch (Exception)
                        {
                            if (err.Number != 0)
                            {
                                if (-2147467259 == err.Number)
                                {
                                }
                                Response.Write(err.Description);
                            }
                            return;
                        }
                    }
                    else
                    {
                        if (rejectedFiles.Length > 0)
                        {
                            rejectedFiles += (string) (", " + postedFile.FileName);
                        }
                        else
                        {
                            rejectedFiles = postedFile.FileName;
                        }

                    }
                }

                if (Session[userId + loginToken + "Attachments"] == null)
                {
                    Session.Add(Convert.ToString(userId) + Convert.ToString(loginToken) + "Attachments", assetIdFileNameList);
                }
                else
                {
                    Session[Convert.ToString(userId) + Convert.ToString(loginToken) + "Attachments"] = assetIdFileNameList;
                }

                if (Session[Convert.ToString(userId) + Convert.ToString(loginToken) + "RejectedFiles"] == null)
                {
                    Session.Add(Convert.ToString(userId) + Convert.ToString(loginToken) + "RejectedFiles", rejectedFiles);
                }
                else
                {
                    Session[Convert.ToString(userId) + Convert.ToString(loginToken) + "RejectedFiles"] = rejectedFiles;
                }
            }
    }