Fix removing failed image

This commit is contained in:
seven332 committed 2016-02-20 16:18:34 +08:00
1 parent 77f7840cf6
commit 56c95740df
3 files changed
+31 -17

No files matched your search

@@ -77,6 +77,7 @@ public final class SpiderDen {
return mDownloadDir != null && mDownloadDir.ensureDir();
}
@Nullable
public UniFile getDownloadDir() {
return mDownloadDir != null && mDownloadDir.isDirectory() ? mDownloadDir : null;
}
@@ -97,6 +98,7 @@ public final class SpiderDen {
return String.format(Locale.US, "%08d%s", index + 1, extension);
}
@Nullable
private static UniFile findImageFile(UniFile dir, int index) {
for (String extension : GalleryProvider.SUPPORT_IMAGE_EXTENSIONS) {
String filename = generateImageFilename(index, extension);
@@ -188,35 +190,39 @@ public final class SpiderDen {
}
}
private void removeFromCache(int index) {
private boolean removeFromCache(int index) {
if (sCache == null) {
return;
return false;
}
String key = EhCacheKeyFactory.getImageKey(mGid, index);
sCache.remove(key);
return sCache.remove(key);
}
private void removeFromDownloadDir(int index) {
private boolean removeFromDownloadDir(int index) {
UniFile dir = getDownloadDir();
if (dir == null) {
return;
return false;
}
boolean result = false;
for (int i = 0, n = GalleryProvider.SUPPORT_IMAGE_EXTENSIONS.length; i < n; i++) {
String filename = generateImageFilename(index, GalleryProvider.SUPPORT_IMAGE_EXTENSIONS[i]);
UniFile file = dir.subFile(filename);
if (file != null) {
file.delete();
result |= file.delete();
}
}
return result;
}
public void remove(int index) {
removeFromCache(index);
removeFromDownloadDir(index);
public boolean remove(int index) {
boolean result = removeFromCache(index);
result |= removeFromDownloadDir(index);
return result;
}
@Nullable
private OutputStreamPipe openCacheOutputStreamPipe(int index) {
if (sCache == null) {
return null;
@@ -229,6 +235,7 @@ public final class SpiderDen {
/**
* @param extension without dot
*/
@Nullable
private OutputStreamPipe openDownloadOutputStreamPipe(int index, @Nullable String extension) {
UniFile dir = getDownloadDir();
if (dir == null) {
@@ -256,6 +263,7 @@ public final class SpiderDen {
}
@Nullable
private InputStreamPipe openCacheInputStreamPipe(int index) {
if (sCache == null) {
return null;
@@ -265,6 +273,7 @@ public final class SpiderDen {
return sCache.getInputStreamPipe(key);
}
@Nullable
public InputStreamPipe openDownloadInputStreamPipe(int index) {
UniFile dir = getDownloadDir();
if (dir == null) {
@@ -86,7 +86,7 @@ public class SpiderInfo {
}
}
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
// Ignore
} finally {
IOUtils.closeQuietly(is);
}
@@ -454,9 +454,6 @@ public class SpiderQueen implements Runnable {
UniFile downloadDir = mSpiderDen.getDownloadDir();
if (downloadDir != null) {
UniFile file = downloadDir.findFile(SPIDER_INFO_FILENAME);
Log.d("TAG", "file = " + file);
SpiderInfo spiderInfo = SpiderInfo.readFromUniFile(file);
if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid &&
spiderInfo.token.equals(mGalleryInfo.token)) {
@@ -771,11 +768,10 @@ public class SpiderQueen implements Runnable {
private GalleryPageParser.Result getImageUrl(int gid, int index, String pToken,
String skipHathKey) throws IOException, ParseException, Image509Exception {
String url = EhUrl.getPageUrl(gid, index, pToken);
Log.d(TAG, url);
if (skipHathKey != null) {
url = url + "?nl=" + skipHathKey;
}
Log.d(TAG, url);
Response response = mHttpClient.newCall(new EhRequestBuilder(url).build()).execute();
String body = response.body().string();
GalleryPageParser.Result result = GalleryPageParser.parse(body);
@@ -837,6 +833,8 @@ public class SpiderQueen implements Runnable {
// Download image
InputStream is = null;
try {
Log.d(TAG, "Start download image " + index);
Response response = mHttpClient.newCall(new EhRequestBuilder(imageUrl).build()).execute();
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
@@ -863,13 +861,14 @@ public class SpiderQueen implements Runnable {
}
os.flush();
// Check interrupted, download may be stopped by interrupting
// Check interrupted
if (Thread.currentThread().isInterrupted()) {
interrupt = true;
mSpiderDen.remove(index);
break;
}
Log.d(TAG, "Download image succeed " + index);
// Download finished
updatePageState(index, STATE_FINISHED);
return true;
@@ -879,9 +878,14 @@ public class SpiderQueen implements Runnable {
IOUtils.closeQuietly(is);
pipe.close();
pipe.release();
Log.d(TAG, "End download image " + index);
}
}
// Remove download failed image
mSpiderDen.remove(index);
updatePageState(index, STATE_FAILED, error);
return !interrupt;
}
@@ -963,6 +967,7 @@ public class SpiderQueen implements Runnable {
mWorkerLock.wait();
} catch (InterruptedException e) {
// Interrupted
Log.d(TAG, Thread.currentThread().getName() + " Interrupted");
break;
}
}