private void DownloadBook(DownloadItemDataModel item) { try { using (var isostore = IsolatedStorageFile.GetUserStoreForApplication()) { for (;;) { var fileLoader = _fileLoadingFactory.GetFileLoader(item.DataSourceID, item.IsTrial); if (fileLoader == null) { throw new Exception(DATA_SOURCE_NOT_FOUND_MESSAGE); } try { var file = fileLoader.LoadFile(item.Path, item.IsZip); SaveToFile(PrepareFilePath(item, isostore), file); SetItemStatus(item, DownloadStatus.Parsing); break; } catch (RestartException) { } } } } catch (Exception exception) { HandleDownloadException(item, exception); } }
private void BookDownloaderOnDownloadCompleted(object sender, DownloadItemDataModel downloadItemDataModel) { _eventAggregator.Publish(new BookDownloaded(downloadItemDataModel)); var catalogBookItemModel = new CatalogBookItemModel(); catalogBookItemModel.Id = downloadItemDataModel.CatalogItemId; catalogBookItemModel.Title = downloadItemDataModel.Name; catalogBookItemModel.Author = downloadItemDataModel.Author; catalogBookItemModel.Description = downloadItemDataModel.Description; if (!string.IsNullOrWhiteSpace(downloadItemDataModel.AcquisitionUrl)) { catalogBookItemModel.AcquisitionLink = new BookAcquisitionLinkModel { Url = downloadItemDataModel.AcquisitionUrl, Type = downloadItemDataModel.AcquisitionType, Prices = BookAcquisitionLinkModel.ParsePrices(downloadItemDataModel.AcquisitionPrices) }; } if (downloadItemDataModel.IsTrial) { catalogBookItemModel.TrialLink = new BookDownloadLinkModel { Url = downloadItemDataModel.Path, Type = downloadItemDataModel.Type }; } _notificationsService.ShowToast(UIStrings.BookDownloader_Success, downloadItemDataModel.Name, _navigationService.UriFor <ReadPageViewModel>() .WithParam(vm => vm.BookId, downloadItemDataModel.BookID.ToString()) .WithParam(vm => vm.CatalogId, downloadItemDataModel.DataSourceID) .WithParam(vm => vm.CatalogBookItemKey, TransientStorage.Put(catalogBookItemModel)) .WithParam(vm => vm.TokenOffset, 0).BuildUri()); }
private void SaveBook(DownloadItemDataModel item, BookSummary bookSummary, IBookSummaryParser previewGenerator, IsolatedStorageFile storeForApplication) { using (var imageStorageFileStream = new IsolatedStorageFileStream(CreateImagesPath(item), FileMode.Create, storeForApplication)) { previewGenerator.SaveImages(imageStorageFileStream); } previewGenerator.SaveCover(item.BookID.ToString()); var book = CreateBook(item, bookSummary); try { _bookService.Add(book); TokensTool.SaveTokens(book, previewGenerator); book.Hidden = book.Trial; _bookService.Save(book); IsolatedStorageSettings.ApplicationSettings.Save(); } catch (Exception) { _bookService.Remove(book.BookID); throw; } }
private bool ProcessBook(DownloadItemDataModel item) { if (item.Canceled) { return(false); } if (item.Status == DownloadStatus.Error) { return(false); } if (item.Status == DownloadStatus.Pending) { SetItemStatus(item, DownloadStatus.Downloading); return(ProcessBook(item)); } if (item.Status == DownloadStatus.Downloading) { DownloadBook(item); return(ProcessBook(item)); } if (item.Status == DownloadStatus.Parsing) { ParseBook(item); return(ProcessBook(item)); } return(item.Status == DownloadStatus.Completed); }
private void OnDownloadError(DownloadItemDataModel book) { var handler = DownloadError; if (handler != null) { handler(this, book); } }
private static void HandleDownloadException(DownloadItemDataModel item, Exception exception) { if (exception is ThreadAbortException) { throw exception; } SetItemStatus(item, DownloadStatus.Error); }
private void BookDownloaderOnDownloadError(object sender, DownloadItemDataModel downloadItemDataModel) { _eventAggregator.Publish(new BookDownloaded(downloadItemDataModel)); _notificationsService.ShowToast( UIStrings.BookDownloader_Error, downloadItemDataModel.Name, _navigationService.UriFor <DownloadListPageViewModel>().BuildUri()); }
private void OnDownloadCompleted(DownloadItemDataModel book) { var handler = DownloadCompleted; if (handler != null) { handler(this, book); } }
public bool DownloadBook(CatalogBookItemModel catalogBookItemModel, int catalogId, bool fullBook = true) { var link = fullBook ? GetDownloadLink(catalogBookItemModel.Links.ToArray()) : GetDownloadLink(catalogBookItemModel.TrialLink); if (link == null) { return(false); } if (!_bookDownloader.IsStarted) { _bookDownloader.Start(); } var downloadModel = new BookDownloadModel { Path = link.Url, Type = GetBookType(link.Type), IsZip = CheckIsZip(link.Type), Name = catalogBookItemModel.Title, Author = catalogBookItemModel.Author, Description = catalogBookItemModel.Description, AcquisitionUrl = catalogBookItemModel.AcquisitionLink != null ? catalogBookItemModel.AcquisitionLink.Url : null, AcquisitionType = catalogBookItemModel.AcquisitionLink != null ? catalogBookItemModel.AcquisitionLink.Type : null, AcquisitionPrices = catalogBookItemModel.AcquisitionLink != null?catalogBookItemModel.AcquisitionLink.ToString() : null, DataSourceID = catalogId, IsTrial = !fullBook, CatalogItemId = catalogBookItemModel.Id }; int oldQueueCount = _container.Count; _bookDownloadsRepository.Add(downloadModel); var downloadViewModel = new DownloadItemDataModel(downloadModel); _container.Enqueue(downloadViewModel); if (_container.Count == oldQueueCount) { int index = _container.GetDataModelIndex(downloadViewModel); if (index > -1) { var viewModel = _container.Items[index]; if (viewModel.Status == DownloadStatus.Error) { viewModel.Status = DownloadStatus.Pending; _bookDownloadsRepository.Remove(downloadModel.DownloadID); } } } return(true); }
private static void RemoveItem(DownloadItemDataModel item, IDownloadsContainer downloads) { var wait = new AutoResetEvent(false); Execute.OnUIThread((() => { downloads.Remove(item); wait.Set(); })); wait.WaitOne(); }
private static void SetItemStatus(DownloadItemDataModel item, DownloadStatus status) { var wait = new AutoResetEvent(false); Execute.OnUIThread(() => { item.Status = status; wait.Set(); }); wait.WaitOne(); }
private void HandleBookDownloading(DownloadItemDataModel downloadItemDataModel, IDownloadsContainer instance) { if (ProcessBook(downloadItemDataModel)) { RemoveItem(downloadItemDataModel, instance); _bookDownloadsRepository.Remove(downloadItemDataModel.DownloadID); OnDownloadCompleted(downloadItemDataModel); } else if (downloadItemDataModel.Status == DownloadStatus.Error) { OnDownloadError(downloadItemDataModel); } }
private static string PrepareFilePath(DownloadItemDataModel item, IsolatedStorageFile storage) { if (item.BookID == Guid.Empty || !storage.DirectoryExists(item.BookID.ToString())) { item.BookID = CreateFolder(storage); } var bookPath = CreateBookPath(item); if (storage.FileExists(bookPath)) { storage.DeleteFile(bookPath); } return(bookPath); }
private void ParseBook(DownloadItemDataModel item) { try { if (item.BookID == Guid.Empty) { throw new Exception(BOOK_HAS_NO_ID_MESSAGE); } using (var storeForApplication = IsolatedStorageFile.GetUserStoreForApplication()) { using (var bookStorageFileStream = new IsolatedStorageFileStream(CreateBookPath(item), FileMode.Open, storeForApplication)) { var previewGenerator = BookFactory.GetPreviewGenerator(item.Type, item.Name, bookStorageFileStream); var bookSummary = previewGenerator.GetBookPreview(); var trialBook = _bookService.GetTrials().SingleOrDefault(t => t.CatalogItemId == item.CatalogItemId); if (trialBook != null) { _bookService.Remove(trialBook.BookID); } if (string.IsNullOrEmpty(bookSummary.UniqueID) || _bookService.CheckUniqueId(bookSummary.UniqueID)) { SaveBook(item, bookSummary, previewGenerator, storeForApplication); } else { DeleteUnnecessaryInfo(item, bookSummary); } SetItemStatus(item, DownloadStatus.Completed); } } } catch (SharpZipBaseException) { SetItemStatus(item, DownloadStatus.Error); } catch (Exception) { SetItemStatus(item, DownloadStatus.Error); } }
private static BookModel CreateBook(DownloadItemDataModel item, BookSummary bookSummary) { var book = new BookModel { BookID = item.BookID.ToString(), Title = bookSummary.Title.SafeSubstring(1024), Author = bookSummary.AuthorName.SafeSubstring(1024), Type = item.Type, Hidden = true, Trial = item.IsTrial, Deleted = false, CreatedDate = DateTime.Now.ToFileTimeUtc(), UniqueID = bookSummary.UniqueID.SafeSubstring(1024), Description = bookSummary.Description, Language = bookSummary.Language, Url = item.Path, CatalogItemId = item.CatalogItemId }; return(book); }
private void DeleteUnnecessaryInfo(DownloadItemDataModel item, BookSummary bookSummary) { _bookService.Remove(item.BookID.ToString()); if (string.IsNullOrEmpty(bookSummary.UniqueID)) { return; } var bookModel = _bookService.GetBookByUniqueId(bookSummary.UniqueID); if (bookModel == null) { return; } try { item.BookID = Guid.Parse(bookModel.BookID); } catch (Exception) { } }
public BookDownloaded(DownloadItemDataModel book) { Book = book; }
public void Restart(DownloadItemDataModel item) { item.Status = DownloadStatus.Pending; }
private static string CreateBookPath(DownloadItemDataModel item) { return(Path.Combine(item.BookID.ToString(), ModelConstants.BOOK_FILE_DATA_PATH)); }
public void Remove(DownloadItemDataModel item) { _bookDownloadsRepository.Remove(item.DownloadID); }
private static string CreateImagesPath(DownloadItemDataModel item) { return(Path.Combine(item.BookID.ToString(), ModelConstants.BOOK_IMAGES_FILE_NAME)); }