From 64c66cdde588009c20c42d03761563072e429087 Mon Sep 17 00:00:00 2001 From: Sunguk Lee Date: Mon, 6 May 2019 17:46:35 +0900 Subject: [PATCH] Check file to finish downloading --- .../muzei/pixivsource/PixivArtWorker.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/pixiv/muzei/pixivsource/PixivArtWorker.java b/src/main/java/com/pixiv/muzei/pixivsource/PixivArtWorker.java index 5f51caf..b096cd5 100644 --- a/src/main/java/com/pixiv/muzei/pixivsource/PixivArtWorker.java +++ b/src/main/java/com/pixiv/muzei/pixivsource/PixivArtWorker.java @@ -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); @@ -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());