Skip to content

Commit

Permalink
Check file to finish downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m3vilurr committed May 6, 2019
1 parent e8a01ce commit 64c66cd
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/java/com/pixiv/muzei/pixivsource/PixivArtWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,26 @@ private Uri downloadOriginalImage(final JsonObject content,
throw new IOException("Couldn't get cache directory");
}

// TODO: resume download or check finished download.
// TODO: check with `If-Modified-Since`
Response resp = getOriginalImageResponse(content, referer);
if (originalFile.exists()) {
Log.d(LOG_TAG, "already exists: " + originalFile.getAbsolutePath());
return Uri.parse("file://" + originalFile.getAbsolutePath());
}
if (!resp.isSuccessful()) {
Log.d(LOG_TAG, "download fault & but already have the file: " + originalFile.getAbsolutePath());
resp.close();
return Uri.parse("file://" + originalFile.getAbsolutePath());
}

Response resp = getOriginalImageResponse(content, referer);
final int contentLength = Integer.parseInt(resp.header("content-length"), 10);
if (contentLength == originalFile.length()) {
Log.d(LOG_TAG, "already exists & downloaded: " + originalFile.getAbsolutePath());
resp.close();
return Uri.parse("file://" + originalFile.getAbsolutePath());
}
}

final int status = resp.code();
if (!resp.isSuccessful()) {
throw new IOException("Unsuccessful request: " + status);
resp.close();
throw new IOException("Unsuccessful request: " + resp.code());
}

final FileOutputStream fileStream = new FileOutputStream(originalFile);
Expand All @@ -368,6 +377,7 @@ private Uri downloadOriginalImage(final JsonObject content,
fileStream.close();
}
inputStream.close();
resp.close();
if (failed) {
originalFile.delete();
throw new IOException("download failed: " + originalFile.getAbsolutePath());
Expand Down

0 comments on commit 64c66cd

Please sign in to comment.