Example #1
0
 public Holiday(DAL.Models.Holiday holiday)
 {
     Id          = holiday.Id;
     Day         = holiday.Day;
     Month       = holiday.Month;
     Name        = holiday.Name;
     Picture     = holiday.Picture;
     Description = holiday.Description;
 }
        public dynamic UploadHolidaysFile()
        {
            var file = Request.Files.FirstOrDefault();

            var holidays             = new List <DAL.Models.Holiday>();
            var picturesDirectory    = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "Web", "Content", "Images", "HolidaysPictures"));
            var newPicturesDirectory = new DirectoryInfo(Path.Combine(picturesDirectory.FullName, "NewImages"));

            if (newPicturesDirectory.Exists)
            {
                newPicturesDirectory.Delete(true);
            }

            newPicturesDirectory.Create();

            var webClient = new WebClient();

            try
            {
                using (var reader = ExcelReaderFactory.CreateReader(file.Value))
                {
                    var dataSet = reader.AsDataSet();
                    var table   = dataSet.Tables[0];
                    var rows    = table.Rows;

                    for (int i = 0; i < rows.Count; i++)
                    {
                        var items   = rows[i].ItemArray;
                        var holiday = new DAL.Models.Holiday();

                        holiday.Day         = Convert.ToInt32(items[0]);
                        holiday.Month       = Convert.ToInt32(items[1]);
                        holiday.Name        = items[2].ToString();
                        holiday.Description = items[3].ToString();

                        var fileUrl = items[4].ToString();

                        var newFileName = Guid.NewGuid() + Path.GetExtension(fileUrl);
                        var argsIndex   = newFileName.IndexOf('?');
                        if (argsIndex > 0)
                        {
                            newFileName = newFileName.Remove(argsIndex);
                        }

                        var newFilePath = Path.Combine(newPicturesDirectory.FullName, newFileName);

                        if (Environment.OSVersion.Platform == PlatformID.Unix)
                        {
                            WebUtils.DownloadFile(fileUrl, newFilePath);
                        }
                        else
                        {
                            webClient.DownloadFile(fileUrl, newFilePath);
                        }

                        holiday.Picture = newFileName;

                        holidays.Add(holiday);
                    }
                }
            }
            catch (Exception ex)
            {
                return(Response.AsText(ex.ToString()));
            }

            Parallel.ForEach(picturesDirectory.EnumerateFiles(), (f) => f.Delete());
            Parallel.ForEach(newPicturesDirectory.EnumerateFiles(), (f) => f.MoveTo(Path.Combine(picturesDirectory.FullName, f.Name)));

            Repository.DeleteAllRows <DAL.Models.Holiday>();
            foreach (var holiday in holidays)
            {
                Repository.AddHoliday(holiday);
            }

            return(Response.AsText("ok"));
        }