/// <summary> /// Load sitemap /// </summary> /// <param name="physicalPath">Filepath to load a sitemap</param> /// <returns>A task that represents the asynchronous operation</returns> public virtual async Task LoadFromAsync(string physicalPath) { var filePath = _fileProvider.MapPath(physicalPath); var content = await _fileProvider.ReadAllTextAsync(filePath, Encoding.UTF8); if (!string.IsNullOrEmpty(content)) { var doc = new XmlDocument(); using (var sr = new StringReader(content)) { using var xr = XmlReader.Create(sr, new XmlReaderSettings { CloseInput = true, IgnoreWhitespace = true, IgnoreComments = true, IgnoreProcessingInstructions = true }); doc.Load(xr); } if ((doc.DocumentElement != null) && doc.HasChildNodes) { var xmlRootNode = doc.DocumentElement.FirstChild; await IterateAsync(RootNode, xmlRootNode); } } }
/// <summary> /// Get system names of installed plugins from obsolete file /// </summary> /// <returns>List of plugin system names</returns> protected virtual async Task <IList <string> > GetObsoleteInstalledPluginNamesAsync() { //check whether file exists var filePath = _fileProvider.MapPath(NopPluginDefaults.InstalledPluginsFilePath); if (!_fileProvider.FileExists(filePath)) { //if not, try to parse the file that was used in previous nopCommerce versions filePath = _fileProvider.MapPath(NopPluginDefaults.ObsoleteInstalledPluginsFilePath); if (!_fileProvider.FileExists(filePath)) { return(new List <string>()); } //get plugin system names from the old txt file var pluginSystemNames = new List <string>(); using (var reader = new StringReader(await _fileProvider.ReadAllTextAsync(filePath, Encoding.UTF8))) { string pluginName; while ((pluginName = await reader.ReadLineAsync()) != null) { if (!string.IsNullOrWhiteSpace(pluginName)) { pluginSystemNames.Add(pluginName.Trim()); } } } //and delete the old one _fileProvider.DeleteFile(filePath); return(pluginSystemNames); } var text = await _fileProvider.ReadAllTextAsync(filePath, Encoding.UTF8); if (string.IsNullOrEmpty(text)) { return(new List <string>()); } //delete the old file _fileProvider.DeleteFile(filePath); //get plugin system names from the JSON file return(JsonConvert.DeserializeObject <IList <string> >(text)); }
/// <summary> /// Save app settings to the file /// </summary> /// <param name="appSettings">App settings</param> /// <param name="fileProvider">File provider</param> public static async Task SaveAppSettingsAsync(AppSettings appSettings, INopFileProvider fileProvider = null) { Singleton <AppSettings> .Instance = appSettings ?? throw new ArgumentNullException(nameof(appSettings)); fileProvider ??= CommonHelper.DefaultFileProvider; //create file if not exists var filePath = fileProvider.MapPath(NopConfigurationDefaults.AppSettingsFilePath); fileProvider.CreateFile(filePath); //check additional configuration parameters var additionalData = JsonConvert.DeserializeObject <AppSettings>(await fileProvider.ReadAllTextAsync(filePath, Encoding.UTF8))?.AdditionalData; appSettings.AdditionalData = additionalData; //save app settings to the file var text = JsonConvert.SerializeObject(appSettings, Formatting.Indented); await fileProvider.WriteAllTextAsync(filePath, text, Encoding.UTF8); }
/// <summary> /// Parse the JSON file /// </summary> /// <param name="file">Path to the file</param> /// <returns>Collection of keys and values from the parsed file</returns> protected virtual async Task <Dictionary <string, string> > ParseJsonAsync(string file) { var result = new Dictionary <string, string>(); var json = string.Empty; try { json = (await _fileProvider.ReadAllTextAsync(file, Encoding.UTF8))?.Trim(); } catch { //ignore any exception } if (string.IsNullOrEmpty(json)) { return(result); } if (json.StartsWith("{")) { json = json[1..^ 1];
/// <summary> /// Get robots.txt file /// </summary> /// <returns>Robots.txt file as string</returns> public virtual async Task <string> PrepareRobotsTextFileAsync() { var sb = new StringBuilder(); //if robots.custom.txt exists, let's use it instead of hard-coded data below var robotsFilePath = _fileProvider.Combine(_fileProvider.MapPath("~/"), "robots.custom.txt"); if (_fileProvider.FileExists(robotsFilePath)) { //the robots.txt file exists var robotsFileContent = await _fileProvider.ReadAllTextAsync(robotsFilePath, Encoding.UTF8); sb.Append(robotsFileContent); } else { //doesn't exist. Let's generate it (default behavior) var disallowPaths = new List <string> { "/admin", "/bin/", "/files/", "/files/exportimport/", "/country/getstatesbycountryid", "/install", "/setproductreviewhelpfulness", }; var localizableDisallowPaths = new List <string> { "/addproducttocart/catalog/", "/addproducttocart/details/", "/backinstocksubscriptions/manage", "/boards/forumsubscriptions", "/boards/forumwatch", "/boards/postedit", "/boards/postdelete", "/boards/postcreate", "/boards/topicedit", "/boards/topicdelete", "/boards/topiccreate", "/boards/topicmove", "/boards/topicwatch", "/cart$", "/changecurrency", "/changelanguage", "/changetaxtype", "/checkout", "/checkout/billingaddress", "/checkout/completed", "/checkout/confirm", "/checkout/shippingaddress", "/checkout/shippingmethod", "/checkout/paymentinfo", "/checkout/paymentmethod", "/clearcomparelist", "/compareproducts", "/compareproducts/add/*", "/customer/avatar", "/customer/activation", "/customer/addresses", "/customer/changepassword", "/customer/checkusernameavailability", "/customer/downloadableproducts", "/customer/info", "/deletepm", "/emailwishlist", "/eucookielawaccept", "/inboxupdate", "/newsletter/subscriptionactivation", "/onepagecheckout", "/order/history", "/orderdetails", "/passwordrecovery/confirm", "/poll/vote", "/privatemessages", "/returnrequest", "/returnrequest/history", "/rewardpoints/history", "/search?", "/sendpm", "/sentupdate", "/shoppingcart/*", "/storeclosed", "/subscribenewsletter", "/topic/authenticate", "/viewpm", "/uploadfilecheckoutattribute", "/uploadfileproductattribute", "/uploadfilereturnrequest", "/wishlist", }; const string newLine = "\r\n"; //Environment.NewLine sb.Append("User-agent: *"); sb.Append(newLine); //sitemaps if (_sitemapXmlSettings.SitemapXmlEnabled) { if (_localizationSettings.SeoFriendlyUrlsForLanguagesEnabled) { //URLs are localizable. Append SEO code foreach (var language in await _languageService.GetAllLanguagesAsync(storeId: (await _storeContext.GetCurrentStoreAsync()).Id)) { sb.AppendFormat("Sitemap: {0}{1}/sitemap.xml", _webHelper.GetStoreLocation(), language.UniqueSeoCode); sb.Append(newLine); } } else { //localizable paths (without SEO code) sb.AppendFormat("Sitemap: {0}sitemap.xml", _webHelper.GetStoreLocation()); sb.Append(newLine); } } //host sb.AppendFormat("Host: {0}", _webHelper.GetStoreLocation()); sb.Append(newLine); //usual paths foreach (var path in disallowPaths) { sb.AppendFormat("Disallow: {0}", path); sb.Append(newLine); } //localizable paths (without SEO code) foreach (var path in localizableDisallowPaths) { sb.AppendFormat("Disallow: {0}", path); sb.Append(newLine); } if (_localizationSettings.SeoFriendlyUrlsForLanguagesEnabled) { //URLs are localizable. Append SEO code foreach (var language in await _languageService.GetAllLanguagesAsync(storeId: (await _storeContext.GetCurrentStoreAsync()).Id)) { foreach (var path in localizableDisallowPaths) { sb.AppendFormat("Disallow: /{0}{1}", language.UniqueSeoCode, path); sb.Append(newLine); } } } //load and add robots.txt additions to the end of file. var robotsAdditionsFile = _fileProvider.Combine(_fileProvider.MapPath("~/"), "robots.additions.txt"); if (_fileProvider.FileExists(robotsAdditionsFile)) { var robotsFileContent = await _fileProvider.ReadAllTextAsync(robotsAdditionsFile, Encoding.UTF8); sb.Append(robotsFileContent); } } return(sb.ToString()); }