public void TryParseTourFile(string tourFile, ICollection <BackOfficeTourFile> result) { var fileName = Path.GetFileNameWithoutExtension(tourFile); if (fileName == null) { return; } try { var contents = File.ReadAllText(tourFile); var tours = JsonConvert.DeserializeObject <BackOfficeTour[]>(contents); var tour = new BackOfficeTourFile { FileName = Path.GetFileNameWithoutExtension(tourFile), PluginName = string.Empty, Tours = tours.ToArray() }; result.Add(tour); } catch (IOException e) { throw new IOException("Error while trying to read file: " + tourFile, e); } catch (JsonReaderException e) { throw new JsonReaderException("Error while trying to parse content as tour data: " + tourFile, e); } }
public HttpResponseMessage SaveTourFile(BackOfficeTourFile tourfile) { // filename may not empty if (tourfile == null || string.IsNullOrEmpty(tourfile.FileName)) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } // can not contain invalid chars if (tourfile.FileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { return(this.Request.CreateNotificationValidationErrorResponse("File name contains invalid characters")); } var toursFolder = Path.Combine(IOHelper.MapPath(SystemDirectories.Config), "BackOfficeTours"); // file should exist var filePath = Path.Combine(toursFolder, tourfile.FileName + ".json"); if (!File.Exists(filePath)) { return(this.Request.CreateNotificationValidationErrorResponse("A file with this name does not exist")); } try { var content = JsonConvert.SerializeObject(tourfile.Tours, Formatting.Indented); File.WriteAllText(filePath, content); return(this.Request.CreateNotificationSuccessResponse("Tour file saved succesfully")); } catch (Exception e) { this.Logger.Error <TourEditorApiController>("Error saving tour file", e); return(this.Request.CreateNotificationValidationErrorResponse("Error saving tour file")); } }
private void TryParseTourFile(string tourFile, ICollection <BackOfficeTourFile> result, List <BackOfficeTourFilter> filters, List <BackOfficeTourFilter> aliasOnlyFilters, string pluginName = null) { var fileName = Path.GetFileNameWithoutExtension(tourFile); if (fileName == null) { return; } //get the filters specific to this file var fileFilters = filters.Where(x => x.TourFileName != null && x.TourFileName.IsMatch(fileName)).ToList(); //If there is any filter applied to match the file only (no tour alias) then ignore the file entirely var isFileFiltered = fileFilters.Any(x => x.TourAlias == null); if (isFileFiltered) { return; } //now combine all aliases to filter below var aliasFilters = aliasOnlyFilters.Concat(filters.Where(x => x.TourAlias != null)) .Select(x => x.TourAlias) .ToList(); try { var contents = File.ReadAllText(tourFile); var tours = JsonConvert.DeserializeObject <BackOfficeTour[]>(contents); var backOfficeTours = tours.Where(x => aliasFilters.Count == 0 || aliasFilters.All(filter => filter.IsMatch(x.Alias)) == false); var localizedTours = backOfficeTours.Where(x => string.IsNullOrWhiteSpace(x.Culture) || x.Culture.Equals(Security.CurrentUser.Language, StringComparison.InvariantCultureIgnoreCase)).ToList(); var tour = new BackOfficeTourFile { FileName = Path.GetFileNameWithoutExtension(tourFile), PluginName = pluginName, Tours = localizedTours }; //don't add if all of the tours are filtered if (tour.Tours.Any()) { result.Add(tour); } } catch (IOException e) { throw new IOException("Error while trying to read file: " + tourFile, e); } catch (JsonReaderException e) { throw new JsonReaderException("Error while trying to parse content as tour data: " + tourFile, e); } }