Esempio n. 1
0
  // Get the image from the filesystem if it exists or download from network
  private Observable<Page> getOrDownloadImage(final Page page, Download download) {
    // If the image URL is empty, do nothing
    if (page.getImageUrl() == null) return Observable.just(page);

    String filename = getImageFilename(page);
    File imagePath = new File(download.directory, filename);

    // If the image is already downloaded, do nothing. Otherwise download from network
    Observable<Page> pageObservable =
        isImageDownloaded(imagePath)
            ? Observable.just(page)
            : downloadImage(page, download.source, download.directory, filename);

    return pageObservable
        // When the image is ready, set image path, progress (just in case) and status
        .doOnNext(
            p -> {
              page.setImagePath(imagePath.getAbsolutePath());
              page.setProgress(100);
              download.downloadedImages++;
              page.setStatus(Page.READY);
            })
        // Mark this page as error and allow to download the remaining
        .onErrorResumeNext(
            e -> {
              page.setProgress(0);
              page.setStatus(Page.ERROR);
              return Observable.just(page);
            });
  }
Esempio n. 2
0
  // Public method to get the image from the filesystem. It does NOT provide any way to download the
  // image
  public Observable<Page> getDownloadedImage(final Page page, File chapterDir) {
    if (page.getImageUrl() == null) {
      page.setStatus(Page.ERROR);
      return Observable.just(page);
    }

    File imagePath = new File(chapterDir, getImageFilename(page));

    // When the image is ready, set image path, progress (just in case) and status
    if (isImageDownloaded(imagePath)) {
      page.setImagePath(imagePath.getAbsolutePath());
      page.setProgress(100);
      page.setStatus(Page.READY);
    } else {
      page.setStatus(Page.ERROR);
    }
    return Observable.just(page);
  }