mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Fix crash(es) upon upload failure. (#2699)
* Fix crash(es) upon upload failure. * Further improve Rx structure.
This commit is contained in:
parent
a32ba452ec
commit
013e1a5430
3 changed files with 71 additions and 75 deletions
|
|
@ -944,7 +944,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
long dataLength,
|
||||
Uri fileUri,
|
||||
Uri contentProviderUri,
|
||||
ProgressListener progressListener) throws IOException {
|
||||
ProgressListener progressListener) {
|
||||
return Single.fromCallable(() -> {
|
||||
CustomApiResult result = api.uploadToStash(filename, file, dataLength, getEditToken(), progressListener::onProgress);
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import java.io.InputStream;
|
|||
import java.util.List;
|
||||
|
||||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.campaigns.CampaignResponseDTO;
|
||||
import fr.free.nrw.commons.notification.Notification;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Single;
|
||||
|
|
@ -53,7 +52,7 @@ public interface MediaWikiApi {
|
|||
@NonNull
|
||||
Single<UploadStash> uploadFile(String filename, InputStream file,
|
||||
long dataLength, Uri fileUri, Uri contentProviderUri,
|
||||
final ProgressListener progressListener) throws IOException;
|
||||
final ProgressListener progressListener);
|
||||
|
||||
@NonNull
|
||||
Single<UploadResult> uploadFileFinalize(String filename, String filekey,
|
||||
|
|
|
|||
|
|
@ -195,6 +195,7 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0));
|
||||
}
|
||||
|
||||
@SuppressLint("CheckResult")
|
||||
private void uploadContribution(Contribution contribution) {
|
||||
InputStream fileInputStream;
|
||||
Uri localUri = contribution.getLocalUri();
|
||||
|
|
@ -222,8 +223,13 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
|
||||
String filename = contribution.getFilename();
|
||||
|
||||
try {
|
||||
NotificationUpdateProgressListener notificationUpdater = new NotificationUpdateProgressListener(notificationTag,
|
||||
getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()),
|
||||
getString(R.string.upload_progress_notification_title_finishing, contribution.getDisplayTitle()),
|
||||
contribution
|
||||
);
|
||||
|
||||
Single.fromCallable(() -> {
|
||||
if (!mwApi.validateLogin()) {
|
||||
// Need to revalidate!
|
||||
if (sessionManager.revalidateAuthToken()) {
|
||||
|
|
@ -231,23 +237,27 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
} else {
|
||||
Timber.d("Unable to revalidate :(");
|
||||
stopForeground(true);
|
||||
Toast failureToast = Toast.makeText(this, R.string.authentication_failed, Toast.LENGTH_LONG);
|
||||
failureToast.show();
|
||||
sessionManager.forceLogin(this);
|
||||
return;
|
||||
sessionManager.forceLogin(UploadService.this);
|
||||
throw new RuntimeException(getString(R.string.authentication_failed));
|
||||
}
|
||||
}
|
||||
NotificationUpdateProgressListener notificationUpdater = new NotificationUpdateProgressListener(notificationTag,
|
||||
getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()),
|
||||
getString(R.string.upload_progress_notification_title_finishing, contribution.getDisplayTitle()),
|
||||
contribution
|
||||
);
|
||||
String stashFilename = "Temp_" + contribution.hashCode() + filename;
|
||||
mwApi.uploadFile(
|
||||
return "Temp_" + contribution.hashCode() + filename;
|
||||
}).flatMap(stashFilename -> mwApi.uploadFile(
|
||||
stashFilename, fileInputStream, contribution.getDataLength(),
|
||||
localUri, contribution.getContentProviderUri(), notificationUpdater)
|
||||
localUri, contribution.getContentProviderUri(), notificationUpdater))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(Schedulers.io())
|
||||
.doFinally(() -> {
|
||||
if (filename != null) {
|
||||
unfinishedUploads.remove(filename);
|
||||
}
|
||||
toUpload--;
|
||||
if (toUpload == 0) {
|
||||
// Sync modifications right after all uploads are processed
|
||||
ContentResolver.requestSync(sessionManager.getCurrentAccount(), BuildConfig.MODIFICATION_AUTHORITY, new Bundle());
|
||||
stopForeground(true);
|
||||
}
|
||||
})
|
||||
.flatMap(uploadStash -> {
|
||||
notificationManager.cancel(NOTIFICATION_UPLOAD_IN_PROGRESS);
|
||||
|
||||
|
|
@ -292,23 +302,10 @@ public class UploadService extends HandlerService<Contribution> {
|
|||
contributionDao.save(contribution);
|
||||
}
|
||||
}, throwable -> {
|
||||
throw new RuntimeException(throwable);
|
||||
});
|
||||
} catch (IOException e) {
|
||||
Timber.w(e,"IOException during upload");
|
||||
Timber.w(throwable, "Exception during upload");
|
||||
notificationManager.cancel(NOTIFICATION_UPLOAD_IN_PROGRESS);
|
||||
showFailedNotification(contribution);
|
||||
} finally {
|
||||
if (filename != null) {
|
||||
unfinishedUploads.remove(filename);
|
||||
}
|
||||
toUpload--;
|
||||
if (toUpload == 0) {
|
||||
// Sync modifications right after all uplaods are processed
|
||||
ContentResolver.requestSync(sessionManager.getCurrentAccount(), BuildConfig.MODIFICATION_AUTHORITY, new Bundle());
|
||||
stopForeground(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressLint("StringFormatInvalid")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue