public async Task <ComicDetail> StoreAsync() { KeepingAction?.Invoke(null, null, null, null, KeepingActionTypes.BeginGetEntity); OnBeginGetEntity(); var entity = await Provider.GetChaptersAsync(Address); try { if (entity is null) { return(null); } var detail = new ComicDetail { Entity = entity }; var cwps = new List <ChapterWithPage>(entity.Chapters.Length); foreach (var item in entity.Chapters) { if (tokenSource.IsCancellationRequested) { break; } KeepingAction?.Invoke(entity, item, null, null, KeepingActionTypes.BeginStoreChapter); OnBeginStoreChapter(entity, item); try { var cwp = await StoreChapterAsync(entity, item); cwps.Add(cwp); } catch (Exception ex) { KeepingAction?.Invoke(entity, item, null, ex, KeepingActionTypes.StoreChapterException); OnStoreChapterException(entity, item, ex); } KeepingAction?.Invoke(entity, item, null, null, KeepingActionTypes.EndStoreChapter); OnEndStoreChapter(entity, item); } if (!tokenSource.IsCancellationRequested) { var streamManager = AppEngine.GetRequiredService <RecyclableMemoryStreamManager>(); var str = JsonHelper.Serialize(detail); var buffer = Encoding.UTF8.GetBytes(str); var path = ComicIndexName; using (var stream = streamManager.GetStream()) { stream.Write(buffer, 0, buffer.Length); stream.Seek(0, SeekOrigin.Begin); await storeService.SaveAsync(path, stream); } } return(detail); } finally { KeepingAction?.Invoke(entity, null, null, null, KeepingActionTypes.EndGetEntity); OnEndGetEntity(entity); } }
protected virtual async Task StorePageAsync(ComicEntity entity, ComicChapter chapter, ComicPage page) { KeepingAction?.Invoke(entity, chapter, page, null, KeepingActionTypes.BeginStorePage); OnBeginStorePage(chapter, page); try { var mode = WriteMode; if (mode == ComicStoreWriteModes.NotExists) { var ok = await storeService.ExistsAsync(page.TargetUrl); if (ok) { KeepingAction?.Invoke(entity, chapter, page, null, KeepingActionTypes.EndStorePage); OnEndStorePage(chapter, page, false); return; } } var path = page.TargetUrl; if (HasImageFolderName) { path = string.Concat(ImageFolderName, "/", page.TargetUrl); } var stream = await fetchService.GetStreamAsync(page.TargetUrl); if (stream is null) { stream = await Provider.GetImageStreamAsync(page.TargetUrl); if (fetchService != storeService) { await fetchService.SaveAsync(path, stream); } } stream.Seek(0, SeekOrigin.Begin); await storeService.SaveAsync(path, stream); } catch (Exception ex) { KeepingAction?.Invoke(entity, chapter, page, ex, KeepingActionTypes.StorePageException); OnStorePageException(chapter, page, ex); } KeepingAction?.Invoke(entity, chapter, page, null, KeepingActionTypes.EndStorePage); OnEndStorePage(chapter, page, true); }