Merge pull request #1871 from mfietz/issue/1870-403-forbidden

Delay auto download when receiving 403 Forbidden HTTP error
This commit is contained in:
Tom Hennen 2016-04-13 17:52:04 -04:00
commit 8683277627
5 changed files with 29 additions and 28 deletions

View File

@ -53,8 +53,7 @@ public class DownloadLogAdapter extends BaseAdapter {
holder.date = (TextView) convertView.findViewById(R.id.txtvDate);
holder.title = (TextView) convertView.findViewById(R.id.txtvTitle);
holder.type = (TextView) convertView.findViewById(R.id.txtvType);
holder.reason = (TextView) convertView
.findViewById(R.id.txtvReason);
holder.reason = (TextView) convertView.findViewById(R.id.txtvReason);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();

View File

@ -204,8 +204,10 @@ public class DownloadService extends Service {
FeedItem item = media.getItem();
boolean httpNotFound = status.getReason() == DownloadError.ERROR_HTTP_DATA_ERROR
&& String.valueOf(HttpURLConnection.HTTP_NOT_FOUND).equals(status.getReasonDetailed());
boolean forbidden = status.getReason() == DownloadError.ERROR_FORBIDDEN
&& String.valueOf(HttpURLConnection.HTTP_FORBIDDEN).equals(status.getReasonDetailed());
boolean notEnoughSpace = status.getReason() == DownloadError.ERROR_NOT_ENOUGH_SPACE;
if (httpNotFound || notEnoughSpace) {
if (httpNotFound || forbidden || notEnoughSpace) {
DBWriter.saveFeedItemAutoDownloadFailed(item).get();
}
// to make lists reload the failed item, we fake an item update
@ -993,8 +995,8 @@ public class DownloadService extends Service {
public void run() {
FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId());
if (media == null) {
throw new IllegalStateException(
"Could not find downloaded media object in database");
Log.e(TAG, "Could not find downloaded media object in database");
return;
}
media.setDownloaded(true);
media.setFile_url(request.getDestination());

View File

@ -124,14 +124,6 @@ public class HttpDownloader extends Downloader {
}
}
if(request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
String contentType = response.header("Content-Type");
if(!contentType.startsWith("audio/") && !contentType.startsWith("video/")) {
onFail(DownloadError.ERROR_FILE_TYPE, null);
return;
}
}
responseBody = response.body();
String contentEncodingHeader = response.header("Content-Encoding");
boolean isGzip = false;
@ -174,6 +166,9 @@ public class HttpDownloader extends Downloader {
if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
error = DownloadError.ERROR_UNAUTHORIZED;
details = String.valueOf(response.code());
} else if(response.code() == HttpURLConnection.HTTP_FORBIDDEN) {
error = DownloadError.ERROR_FORBIDDEN;
details = String.valueOf(response.code());
} else {
error = DownloadError.ERROR_HTTP_DATA_ERROR;
details = String.valueOf(response.code());
@ -187,6 +182,14 @@ public class HttpDownloader extends Downloader {
return;
}
if(request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
String contentType = response.header("Content-Type");
if(!contentType.startsWith("audio/") && !contentType.startsWith("video/")) {
onFail(DownloadError.ERROR_FILE_TYPE, null);
return;
}
}
connection = new BufferedInputStream(responseBody.byteStream());
String contentRangeHeader = (fileExists) ? response.header("Content-Range") : null;
@ -219,21 +222,18 @@ public class HttpDownloader extends Downloader {
long freeSpace = StorageUtils.getFreeSpaceAvailable();
Log.d(TAG, "Free space is " + freeSpace);
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN
&& request.getSize() > freeSpace) {
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN && request.getSize() > freeSpace) {
onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE, null);
return;
}
Log.d(TAG, "Starting download");
try {
while (!cancelled
&& (count = connection.read(buffer)) != -1) {
while (!cancelled && (count = connection.read(buffer)) != -1) {
out.write(buffer, 0, count);
request.setSoFar(request.getSoFar() + count);
request.setProgressPercent((int) (((double) request
.getSoFar() / (double) request
.getSize()) * 100));
int progressPercent = (int)(100.0 * request.getSoFar() / request.getSize());
request.setProgressPercent(progressPercent);
}
} catch(IOException e) {
Log.e(TAG, Log.getStackTraceString(e));
@ -245,12 +245,8 @@ public class HttpDownloader extends Downloader {
// written file. This check cannot be made if compression was used
if (!isGzip && request.getSize() != DownloadStatus.SIZE_UNKNOWN &&
request.getSoFar() != request.getSize()) {
onFail(DownloadError.ERROR_IO_ERROR,
"Download completed but size: " +
request.getSoFar() +
" does not equal expected size " +
request.getSize()
);
onFail(DownloadError.ERROR_IO_ERROR, "Download completed but size: " +
request.getSoFar() + " does not equal expected size " + request.getSize());
return;
} else if(request.getSize() > 0 && request.getSoFar() == 0){
onFail(DownloadError.ERROR_IO_ERROR, "Download completed, but nothing was read");
@ -294,7 +290,8 @@ public class HttpDownloader extends Downloader {
}
private void onFail(DownloadError reason, String reasonDetailed) {
Log.d(TAG, "Download failed");
Log.d(TAG, "onFail() called with: " + "reason = [" + reason + "], " +
"reasonDetailed = [" + reasonDetailed + "]");
result.setFailed(reason, reasonDetailed);
if (request.isDeleteOnFailure()) {
cleanup();

View File

@ -20,7 +20,9 @@ public enum DownloadError {
ERROR_REQUEST_ERROR(12, R.string.download_error_request_error),
ERROR_DB_ACCESS_ERROR(13, R.string.download_error_db_access),
ERROR_UNAUTHORIZED(14, R.string.download_error_unauthorized),
ERROR_FILE_TYPE(15, R.string.download_error_file_type_type);
ERROR_FILE_TYPE(15, R.string.download_error_file_type_type),
ERROR_FORBIDDEN(16, R.string.download_error_forbidden);
private final int code;
private final int resId;

View File

@ -177,6 +177,7 @@
<string name="download_error_unknown_host">Unknown Host</string>
<string name="download_error_unauthorized">Authentication Error</string>
<string name="download_error_file_type_type">File Type Error</string>
<string name="download_error_forbidden">Forbidden</string>
<string name="cancel_all_downloads_label">Cancel all downloads</string>
<string name="download_canceled_msg">Download canceled</string>
<string name="download_canceled_autodownload_enabled_msg">Download canceled\nDisabled <i>Auto Download</i> for this item</string>