public void ExtractAllFilesFromEpub(EpubBook epubBook, string folder) { if (!Directory.Exists(Path.GetDirectoryName(folder))) { Directory.CreateDirectory(Path.GetDirectoryName(folder)); } else { Directory.Delete(Path.GetDirectoryName(folder), true); Directory.CreateDirectory(Path.GetDirectoryName(folder)); } foreach (var file in epubBook.Content.AllFiles) { var fileName = file.Key; var fullPath = BookEnvironment.CombinePath(folder, fileName); if (!Directory.Exists(Path.GetDirectoryName(fullPath))) { Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); } switch (file.Value) { case EpubTextContentFile text: File.WriteAllText(fullPath, text.Content); break; case EpubByteContentFile binFile: File.WriteAllBytes(fullPath, binFile.Content); break; } } }
private void renderHtml() { if (epubBook == null) { return; } var url = BookEnvironment.AddSourceTempPath(epubBook.Content.Html.ElementAt(index).Key); this.ContentWebView.Source = url; /* * var webviewSource = new HtmlWebViewSource(); * var html = epubBook.Content.Html.ElementAt(index).Value.Content; * string css = ""; * foreach(var localCss in epubBook.Content.Css) * { * css += localCss.Value.Content; * } * html.Replace("<head>", $"<head><style>{css}</style>"); * webviewSource.Html = html; * webviewSource.BaseUrl = baseUrl; * this.ContentWebView.Source = webviewSource;*/ }
public ItemDetailViewModel(Item item = null) { Title = item?.Text; Item = item; var book = contentService.ExtractEpubFromId(item.AssetId); Source = BookEnvironment.AddSourceTempPath(book.Content.Html.First().Key); }
//public void AddOrUpdate<T>(this IBarrel barrel, string key, T value, TimeSpan timeSpan) //{ // if (barrel.Exists(key)) // barrel.Empty(key); // barrel.Add<T>(key, value, timeSpan); //} public EpubBook ExtractEpubFromId(string assetId) { var asset = Barrel.Current.Get <EpubAsset>(assetId); var epubBook = EpubReader.ReadBook(asset.Path); var tempFolder = BookEnvironment.GetTempLocalPathForEpub(); ExtractAllFilesFromEpub(epubBook, tempFolder); return(epubBook); }
async void Button_Clicked(System.Object sender, System.EventArgs e) { var fileData = await CrossFilePicker.Current.PickFile(); if (fileData == null) { return; // user canceled file picking } epubBook = EpubReader.ReadBook(fileData.FilePath); //SaveImages(); Item.Text = epubBook.Title; try { var coverFileName = ""; if (Device.RuntimePlatform == "UWP") { coverFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "tempCover.jpg"); } else { coverFileName = Path.Combine(baseUrl, "tempCover.jpg"); } File.WriteAllBytes(coverFileName, epubBook.CoverImage ?? epubBook.Content.Images.FirstOrDefault().Value.Content); this.CoverImage.Source = coverFileName; } catch (Exception ex) { Console.WriteLine(ex.Message); } this.OnPropertyChanged("Item"); //SaveHtmlCssFiles(); contentService.ExtractAllFilesFromEpub(epubBook, BookEnvironment.GetTempLocalPathForEpub()); renderHtml(); }
public async Task SyncContentAsync() { var httpClient = new HttpClient(); var barrel = Barrel.Current; var res = string.IsNullOrWhiteSpace(nextSyncUrl) ? await Client.SyncInitial() : await Client.SyncNextResult(nextSyncUrl); nextSyncUrl = res.NextSyncUrl; barrel.Add(nameof(nextSyncUrl), nextSyncUrl, ExpirationSpan); var hasMore = true; while (hasMore) { foreach (var entry in res.Entries) { //if (entry.SystemProperties.con) var jObject = entry.Fields as JObject; if (ValidateEntry(jObject)) { var newItem = new Item { Name = jObject["Name"][locale].ToString(), Text = jObject["Description"][locale]?.ToString(), AssetId = jObject["File"][locale]["sys"]["id"]?.ToString() }; if (!itemEntriesKeysList.Contains(newItem.Name)) { itemEntriesKeysList.Add(newItem.Name); } System.Diagnostics.Debug.WriteLine($"Sync Entries [{newItem.Name}]"); barrel.Add <Item>(newItem.Name, newItem, ExpirationSpan); barrel.Add <List <string> >(EntriesKeyListId, itemEntriesKeysList, ExpirationSpan); } } foreach (var entry in res.Assets) { var jObject = entry.Fields as JObject; var newItem = new EpubAsset { FileName = jObject["file"][locale]["fileName"].ToString(), Title = jObject["title"][locale].ToString(), Url = jObject["file"][locale]["url"].ToString() }; newItem.Id = entry.SystemProperties.Id;// newItem.Url.Split('/')[4]; //var asset = await Client.GetAsset(newItem.Id); System.Diagnostics.Debug.WriteLine($"Sync Assets [{newItem.FileName}]"); var response = await httpClient.GetAsync("https:" + newItem.Url); var fullPath = Path.Combine(BookEnvironment.GetLocalPathForAssets(), newItem.FileName); if (!Directory.Exists(Path.GetDirectoryName(fullPath))) { Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); } File.WriteAllBytes(fullPath, await response.Content.ReadAsByteArrayAsync()); newItem.Path = fullPath; if (!itemAssetsKeysList.Contains(newItem.Id)) { itemAssetsKeysList.Add(newItem.Id); } barrel.Add <EpubAsset>(newItem.Id, newItem, ExpirationSpan); barrel.Add <List <string> >(AssetsKeyListId, itemAssetsKeysList, ExpirationSpan); } var nextUrl = res.NextPageUrl; if (string.IsNullOrEmpty(nextUrl)) { hasMore = false; } else { res = await Client.SyncNextResult(nextUrl); } } }