public static FileDescription ToFileDescription(this FileUploadHttpPostedFileWrapper postedFile, int userId)
 {
     var fileDescription = new FileDescription();
     var fileSize = (int)Math.Ceiling((decimal)(postedFile.ContentLength / 1024));
     fileSize = fileSize > 0 ? fileSize : 1;
     fileDescription.FileName = postedFile.FileName;
     fileDescription.Size = fileSize;
     fileDescription.UploadTime = DateTime.Now;
     fileDescription.UserId = userId;
     return fileDescription;
 }
        public void AddSingleFileDescriptionWithoutFileName()
        {
            //arrange
            string filename = "TestFileDescription" + DateTime.Today;

            //act
            var fileDescription = new FileDescription()
            {
                FileName = null,
                Size = 1,
                UploadTime = DateTime.Now
            };
            fd.Add(fileDescription);
            dbContext.SaveChanges();

            //assert
            Assert.IsFalse(fileDescription.Id == 0);
        }
        public void AddSingleFileDescriptionWithoutFileName()
        {
            //arrange
            var fileDescription = new FileDescription()
            {
                FileName = null,
                Size = 1,
                UploadTime = DateTime.Now,
                UserId = 1
            };

            //act
            fd.Add(fileDescription);
            dbContext.SaveChanges();

            //assert
            Assert.IsFalse(fileDescription.Id == 0);
        }
        /// <summary>
        /// Sends file to cache for parsing
        /// </summary>
        /// <param name="fileName">Destination file name</param>
        /// <param name="fileDescription">Original POCO description of file</param>
       
        public static ICollection<UploadResult> ParseFile(string fileName, FileDescription fileDescription)
        {
            var cacheFileUploadServiceClient = new CacheFileUploadService.FileUploadServiceSoapClient();
#if DEBUG
            fileName = @"D:\FileUploaderTest\" + fileDescription.FileName;
#endif            
            var cacheUploadResults = cacheFileUploadServiceClient.ParseFile(fileName);
            var uploadResult = new List<UploadResult>();

            foreach (var result in cacheUploadResults)
            {
                uploadResult.Add(
                    new UploadResult() 
                    { 
                         RowNumber = result.Id,
                         File = fileDescription,
                         Message = result.MessageText,
                         Status = (Status)result.RowStatus,
                         FileId = fileDescription.Id
                    }); 
            }

            return uploadResult;
        }
        public ActionResult SaveAndPersist(IEnumerable<HttpPostedFileBase> upload)
        {
            StringBuilder messageBuilder = new StringBuilder();

            if (upload != null)
            {
                var fileBase = (HttpPostedFileWrapper)upload.FirstOrDefault();
                var fileDescription = new FileDescription();
                FileUploadHttpPostedFileWrapper file = new FileUploadHttpPostedFileWrapper(fileBase);
                var fileName = Path.GetFileName(file.FileName);
                var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
#if DEBUG
                physicalPath = @"\\10.94.23.31\FileUploaderTest\"+fileName;
#endif
                try
                {
                    using (var tran = new TransactionScope())
                    {
                        file.SaveAs(physicalPath);
                        fileDescription = file.ToFileDescription(User.Identity.GetUserId());
                        db.FileDescriptions.Add(fileDescription);
                        var uploadResult = FuleUploadUtility.ParseFile(physicalPath, fileDescription);
                        fileDescription.UploadResults = uploadResult;
                        db.SaveChanges();
                        tran.Complete();
                    }
                }
                catch (UnauthorizedAccessException ex) 
                {
                    messageBuilder.Append(ex.Message + "<br/>");
                }
                catch (DirectoryNotFoundException ex)
                {
                    messageBuilder.Append(ex.Message + "<br/>");
                }
                catch (System.IO.IOException ex)
                {
                    messageBuilder.Append(ex.Message + "<br/>");
                }
                catch (InvalidOperationException ex)
                {
#if DEBUG
                    messageBuilder.Append(ex.Message + "<br/>");
#endif
                    messageBuilder.Append("Файлът неможе да бъде качен!" + "<br/>");
                    if (System.IO.File.Exists(physicalPath))
                    {
                        try
                        {
                            System.IO.File.Delete(physicalPath);
                        }
                        catch (Exception e)
                        {
                            messageBuilder.Append(e.Message + "<br/>");
                        }
                    }
                }
                catch (Exception exc)
                {
#if DEBUG
                    messageBuilder.Append(exc.Message + "<br/>");
#endif
                    messageBuilder.Append("Файлът неможе да бъде качен!<br/>");
                    if (System.IO.File.Exists(physicalPath))
                    {
                        try
                        {
                            System.IO.File.Delete(physicalPath);
                        }
                        catch (Exception e)
                        {
                            messageBuilder.Append(e.Message + "<br/>");
                        }
                    }
                }

                if (messageBuilder.Length > 0)
                {
                    return Content(messageBuilder.ToString());
                }
                else
                {
                    return Json(new { status = 200, fileId = fileDescription.Id });
                }

            }

            return Content("Файлът неможе да бъде качен!");
        }
        public void BulkInsert10FileDescriptionReceive10recordsTest()
        {
            string filename = "TestFileDescription" + DateTime.Today;

            //arrange
            var fileDescriptions = new List<FileDescription>();
            for (int i = 0; i < 10; i++)
            {
                var fileDescription = new FileDescription()
                {
                    FileName = filename,
                    Size = 1,
                    UploadTime = DateTime.Now
                };
                fileDescriptions.Add(fileDescription);
            }
            //act
            fd.BulkInsert(fileDescriptions);

            dbContext.SaveChanges();

            //assert
            var actualFileDescriptionsInDataBase = fd.All().Where(x => (x.FileName == filename)).ToList();
            Assert.AreEqual(10, actualFileDescriptionsInDataBase.Count);
        }
 public void InsertFileDescriptionRecordWithoutNameShouldThrowExceptionTest()
 {
     //arrange
     var fileDescription = new FileDescription()
     {
         FileName = null,
         Size = 1,
         UploadTime = DateTime.Now
     };
     //act
     fd.Add(fileDescription);
     dbContext.SaveChanges();
 }