/// <summary> /// Deserializes all Files in .../podcasts/ path at startup /// </summary> /// <returns>A <see cref="List{PodcastModel}"/>Returns a List with all deserialized Podcasts if <see cref="true"/>, else a new empty List</returns> public static ObservableCollection <PodcastModel> Deserialize() { var feedList = new ObservableCollection <PodcastModel>(); string podcastPath = MainViewModel.ApplicationSettings.GetPodcastPath(); string[] files = Directory.GetFiles(podcastPath, "*.json", SearchOption.TopDirectoryOnly); Parallel.ForEach(files, file => { if (!Guid.TryParse(Path.GetFileNameWithoutExtension(file), out Guid id)) { return; } string json = File.ReadAllText(file); PodcastModel feed = JsonConvert.DeserializeObject <PodcastModel>(json); feedList.Add(feed); }); if (files.Length <= 0) { return(new ObservableCollection <PodcastModel>()); } return(feedList); }
/// <summary> /// Check if localpath and webpath ar valid and saves image /// </summary> /// <param name="podcastModel"></param> private static async void SaveImage(this PodcastModel podcastModel) { string imagePath = MainViewModel.ApplicationSettings.GetImagePath() + podcastModel.Id + ".jpg"; bool isWebPath = Uri.TryCreate(podcastModel.ImagePath, UriKind.Absolute, out Uri webPath) && (webPath.Scheme == Uri.UriSchemeHttp || webPath.Scheme == Uri.UriSchemeHttps); if (isWebPath) { podcastModel.SetImagePath(await FileController.DownloadImageAndSave(webPath.AbsoluteUri, imagePath)); } }
/// <summary> /// Serializes one <see cref="PodcastModel"/>. ID will be set if ID is equal to <see cref="Guid.Empty"/> /// </summary> /// <param name="podcastModel"></param> public static void Serialize(this PodcastModel podcastModel) { if (podcastModel.Id == Guid.Empty.ToString()) { podcastModel.SetID(); } SaveImage(podcastModel); string json = JsonConvert.SerializeObject(podcastModel, Formatting.Indented); string path = MainViewModel.ApplicationSettings.GetPodcastPath() + podcastModel.Id + ".json"; File.WriteAllText(path, json); }