Use local file size if episode is downloaded

This commit is contained in:
Martin Fietz 2015-07-12 21:17:22 +02:00
parent 5608d228be
commit 65611129e1
1 changed files with 21 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import android.app.IntentService;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
@ -36,20 +37,27 @@ public class FeedMediaSizeService extends IntentService {
return; return;
} }
long size = Integer.MIN_VALUE; long size = Integer.MIN_VALUE;
HttpURLConnection conn = null; if(media.isDownloaded()) {
try { File mediaFile = new File(media.getLocalMediaUrl());
URL url = new URL(media.getDownload_url()); if(mediaFile.exists()) {
conn = (HttpURLConnection) url.openConnection(); size = mediaFile.length();
conn.setRequestProperty( "Accept-Encoding", "" ); }
conn.setRequestMethod("HEAD"); } else {
size = conn.getContentLength(); HttpURLConnection conn = null;
conn.disconnect(); try {
} catch (IOException e) { URL url = new URL(media.getDownload_url());
Log.d(TAG, media.getDownload_url()); conn = (HttpURLConnection) url.openConnection();
e.printStackTrace(); conn.setRequestProperty("Accept-Encoding", "");
} finally { conn.setRequestMethod("HEAD");
if(conn != null) { size = conn.getContentLength();
conn.disconnect(); conn.disconnect();
} catch (IOException e) {
Log.d(TAG, media.getDownload_url());
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
} }
} }
media.setSize(size); media.setSize(size);