// GET: ImportContent
        public ActionResult Index()
        {
            EF.ImportContentConfiguration importContent;
            using (var dctx = new EF.MindSageDataModelsContainer())
            {
                importContent = dctx.ImportContentConfigurations.Where(it => !it.RecLog.DeletedDate.HasValue).ToList().OrderBy(it => it.RecLog.CreatedDate).LastOrDefault();
            }

            var isImportSettingAvailable = importContent != null;
            var result = new ImportContentViewModel
            {
                Id = isImportSettingAvailable ? importContent.Id : -1,
                BaseURL = isImportSettingAvailable ? importContent.BaseURL : string.Empty,
                HomePageURL = isImportSettingAvailable ? importContent.HomePageURL : string.Empty,
                PagesURLs = isImportSettingAvailable ? JsonConvert.DeserializeObject<IEnumerable<string>>(importContent.PagesURLs) : Enumerable.Empty<string>(),
                ReferenceFileURLs = isImportSettingAvailable ? JsonConvert.DeserializeObject<IEnumerable<string>>(importContent.ReferenceFileURLs) : Enumerable.Empty<string>(),
                ReplaceSections = isImportSettingAvailable ? JsonConvert.DeserializeObject<List<ReplaceSectionInformation>>(importContent.ReplaceSections) : new List<ReplaceSectionInformation>(),
                StorageInfo = isImportSettingAvailable ? JsonConvert.DeserializeObject<StorageInformation>(importContent.StorageInfo) : new StorageInformation(),
            };
            return View(result);
        }
        public ActionResult Index(ImportContentViewModel model)
        {
            cleanModel(model);
            var configuration = updateDatabase(model);
            
            var credential = $"DefaultEndpointsProtocol=https;AccountName={ model.StorageInfo.AccountName };AccountKey={ model.StorageInfo.StorageKey }";
            var storageAccount = CloudStorageAccount.Parse(credential);
            var tableClient = storageAccount.CreateCloudBlobClient();
            var blobRef = tableClient.GetContainerReference("scripts");
            blobRef.CreateIfNotExists();

            var styleText = string.Empty;
            var ReferenceOldName = string.Empty;
            var ReferenceName = string.Empty;
            foreach (var item in model.ReferenceFileURLs)
            {
                if (item.IndexOf("style.css") > -1)
                {
                    var ReferHttp = new HttpDownloader(item);
                    styleText = ReferHttp.GetPage();
                    ReferenceOldName = item;
                    ReferenceName = item.Replace("?", "/").Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last(element => element.IndexOf("style.css") > -1);
                }
            }

            foreach (var item in model.ReferenceFileURLs)
            {
                if(item.IndexOf("style.css") == -1) {
                    var fontsHttp = new HttpDownloader(item);
                    var fontsText = fontsHttp.GetPage();
                    var fontsName = item.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

                    using (var fileStream = GenerateStreamFromString(fontsText))
                    {
                        var blockBlob = blobRef.GetBlockBlobReference(fontsName);
                        blockBlob.UploadFromStream(fileStream);
                        var url = blockBlob.Uri.AbsoluteUri;
                        styleText = replaceFontReference(styleText, fontsName, url);
                    }
                }
            }

            using (var fileStream = GenerateStreamFromString(styleText))
            {
                var blockBlob = blobRef.GetBlockBlobReference(ReferenceName);
                blockBlob.Properties.ContentType = "text/css";
                blockBlob.UploadFromStream(fileStream);
                blockBlob.SetProperties();
                ReferenceName = blockBlob.Uri.AbsoluteUri;
            }

            blobRef = tableClient.GetContainerReference("htmls");
            blobRef.CreateIfNotExists();

            // Create home page
            var Http = new HttpDownloader($"{model.BaseURL}{model.HomePageURL}");
            var rawText = Http.GetPage();
            var replacedText = replaceContent(rawText, model.ReplaceSections);
            replacedText = replaceLink(replacedText, model.BaseURL, model.PagesURLs);
            replacedText = replaceStyleReference(replacedText, ReferenceOldName, ReferenceName);

            using (var fileStream = GenerateStreamFromString(replacedText))
            {
                var blockBlob = blobRef.GetBlockBlobReference(HomePageName);
                blockBlob.Properties.ContentType = "text/html";
                blockBlob.UploadFromStream(fileStream);
                blockBlob.SetProperties();
            }

            // Other pages
            foreach (var item in model.PagesURLs)
            {
                Http = new HttpDownloader($"{model.BaseURL}{item}");
                rawText = Http.GetPage();
                replacedText = replaceContent(rawText, model.ReplaceSections);
                replacedText = replaceLink(replacedText, model.BaseURL, model.PagesURLs);
                replacedText = replaceStyleReference(replacedText, ReferenceOldName, ReferenceName);
                var fileName = item.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

                using (var fileStream = GenerateStreamFromString(replacedText))
                {
                    var blockBlob = blobRef.GetBlockBlobReference(fileName);
                    blockBlob.Properties.ContentType = "text/html";
                    blockBlob.UploadFromStream(fileStream);
                    blockBlob.SetProperties();
                }
            }
            
            return RedirectToAction("Index");
        }
 private ImportContentConfiguration updateDatabase(ImportContentViewModel model)
 {
     using (var dctx = new EF.MindSageDataModelsContainer())
     {
         var selectedObj = dctx.ImportContentConfigurations.FirstOrDefault(it => it.Id == model.Id);
         if (selectedObj == null)
         {
             selectedObj = new EF.ImportContentConfiguration
             {
                 BaseURL = model.BaseURL,
                 HomePageURL = model.HomePageURL,
                 PagesURLs = JsonConvert.SerializeObject(model.PagesURLs),
                 ReferenceFileURLs = JsonConvert.SerializeObject(model.ReferenceFileURLs),
                 ReplaceSections = JsonConvert.SerializeObject(model.ReplaceSections),
                 StorageInfo = JsonConvert.SerializeObject(model.StorageInfo),
                 RecLog = new EF.RecordLog { CreatedDate = DateTime.Now }
             };
             dctx.ImportContentConfigurations.Add(selectedObj);
         }
         else
         {
             selectedObj.BaseURL = model.BaseURL;
             selectedObj.HomePageURL = model.HomePageURL;
             selectedObj.PagesURLs = JsonConvert.SerializeObject(model.PagesURLs);
             selectedObj.ReferenceFileURLs = JsonConvert.SerializeObject(model.ReferenceFileURLs);
             selectedObj.ReplaceSections = JsonConvert.SerializeObject(model.ReplaceSections);
             selectedObj.StorageInfo = JsonConvert.SerializeObject(model.StorageInfo);
             selectedObj.RecLog = new EF.RecordLog { CreatedDate = DateTime.Now };
         }
         dctx.SaveChanges();
         return selectedObj;
     }
 }
 private void cleanModel(ImportContentViewModel model)
 {
     model.PagesURLs = model.PagesURLs.Where(it => !string.IsNullOrEmpty(it) && !string.IsNullOrWhiteSpace(it)).ToList();
     model.ReferenceFileURLs = model.ReferenceFileURLs.Where(it => !string.IsNullOrEmpty(it) && !string.IsNullOrWhiteSpace(it)).ToList();
     model.ReplaceSections = model.ReplaceSections.Where(it => it != null
         && !string.IsNullOrEmpty(it.Original) && !string.IsNullOrWhiteSpace(it.Original))
         .ToList();
 }