从Google Books API下载之前确定电子书的大小

时间:2018-06-25 11:19:33

标签: c# winforms google-books

我正在使用Google Books API编写用于Google Books的桌面电子阅读器,作为练习。

我坚持尝试在下载书籍之前确定书籍的大小。我想添加一个进度条以指示下载进度。

我正在使用Google.Apis.Download.MediaDownloader进行下载,尽管我设法做到了,但是我无法在线找到有关下载前下载文件大小的任何信息。

我能找到的唯一信息是从Google云端硬盘下载时如何确定文件大小。但据我所知,该代码无法用于图书API。

以下是我的上下文下载功能的当前版本:

private async Task Download(string VolID)
{      
  UserCredential credential;
  using (FileStream stream = new FileStream(ClientSecret_Path, FileMode.Open, FileAccess.Read))
  {
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        new[] { BooksService.Scope.Books },
        "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
  }

  BooksService service = new BooksService(new BaseClientService.Initializer
  {
    HttpClientInitializer = credential,
    ApplicationName = "Desktop eReader",
  });

  Google.Apis.Books.v1.Data.Volume volume = await service.Volumes.Get(VolID).ExecuteAsync();
  string bookPath = filePath + volume.VolumeInfo.Title + ".epub";
  if (!File.Exists(bookPath)) //Only DL if not already done.
  {
    MediaDownloader downloader = new MediaDownloader(service);
    MemoryStream dlStream = new MemoryStream();

    downloader.ProgressChanged += Downloader_ProgressChanged;
    downloader.ChunkSize = 51200; //ping every 50Kbs


    /*
     * Add logic in here to determine the file size.
     * Maybe add a download confirmation pop-up.
     * 
     */

    await downloader.DownloadAsync(volume.AccessInfo.Epub.DownloadLink, dlStream);

    using (FileStream dlStream2 = new FileStream(bookPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
      dlStream.WriteTo(dlStream2);
    }
  }     

  int Chapter = 0;
  int ChapterPos = 0;
  //insert logic here to find last reading position
  ContinueBook(bookPath, Chapter, ChapterPos); //function that will later be used to display last reading position
}

0 个答案:

没有答案