mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 13:23:58 +01:00
Stash upload (#2505)
* Introduce Single<UploadResult> * Two stage upload process - split upload process and use stash - resolve filename conflict after upload not before - use NotificationManagerCompat; add notification tag; assign temporaty stash file name
This commit is contained in:
parent
341f9614a7
commit
ee7af37d00
5 changed files with 213 additions and 72 deletions
|
|
@ -853,35 +853,65 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
|
||||
@Override
|
||||
@NonNull
|
||||
public UploadResult uploadFile(String filename,
|
||||
@NonNull InputStream file,
|
||||
long dataLength,
|
||||
String pageContents,
|
||||
String editSummary,
|
||||
Uri fileUri,
|
||||
Uri contentProviderUri,
|
||||
final ProgressListener progressListener) throws IOException {
|
||||
public Single<UploadStash> uploadFile(
|
||||
String filename,
|
||||
@NonNull InputStream file,
|
||||
long dataLength,
|
||||
Uri fileUri,
|
||||
Uri contentProviderUri,
|
||||
ProgressListener progressListener) throws IOException {
|
||||
return Single.fromCallable(() -> {
|
||||
CustomApiResult result = api.uploadToStash(filename, file, dataLength, getEditToken(), progressListener::onProgress);
|
||||
|
||||
CustomApiResult result = api.upload(filename, file, dataLength, pageContents, editSummary, getEditToken(), progressListener::onProgress);
|
||||
Timber.wtf("Result: " + result.toString());
|
||||
|
||||
Timber.d("Result: %s", result.toString());
|
||||
|
||||
String resultStatus = result.getString("/api/upload/@result");
|
||||
|
||||
if (!resultStatus.equals("Success")) {
|
||||
String errorCode = result.getString("/api/error/@code");
|
||||
Timber.e(errorCode);
|
||||
|
||||
if (errorCode.equals(ERROR_CODE_BAD_TOKEN)) {
|
||||
ViewUtil.showLongToast(context, R.string.bad_token_error_proposed_solution);
|
||||
String resultStatus = result.getString("/api/upload/@result");
|
||||
if (!resultStatus.equals("Success")) {
|
||||
String errorCode = result.getString("/api/error/@code");
|
||||
Timber.e(errorCode);
|
||||
|
||||
if (errorCode.equals(ERROR_CODE_BAD_TOKEN)) {
|
||||
ViewUtil.showLongToast(context, R.string.bad_token_error_proposed_solution);
|
||||
}
|
||||
return new UploadStash(errorCode, resultStatus, filename, "");
|
||||
} else {
|
||||
String filekey = result.getString("/api/upload/@filekey");
|
||||
return new UploadStash("", resultStatus, filename, filekey);
|
||||
}
|
||||
return new UploadResult(resultStatus, errorCode);
|
||||
} else {
|
||||
Date dateUploaded = parseMWDate(result.getString("/api/upload/imageinfo/@timestamp"));
|
||||
String canonicalFilename = "File:" + result.getString("/api/upload/@filename").replace("_", " "); // Title vs Filename
|
||||
String imageUrl = result.getString("/api/upload/imageinfo/@url");
|
||||
return new UploadResult(resultStatus, dateUploaded, canonicalFilename, imageUrl);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Single<UploadResult> uploadFileFinalize(
|
||||
String filename,
|
||||
String filekey,
|
||||
String pageContents,
|
||||
String editSummary) throws IOException {
|
||||
return Single.fromCallable(() -> {
|
||||
CustomApiResult result = api.uploadFromStash(
|
||||
filename, filekey, pageContents, editSummary,
|
||||
getEditToken());
|
||||
|
||||
Timber.d("Result: %s", result.toString());
|
||||
|
||||
String resultStatus = result.getString("/api/upload/@result");
|
||||
if (!resultStatus.equals("Success")) {
|
||||
String errorCode = result.getString("/api/error/@code");
|
||||
Timber.e(errorCode);
|
||||
|
||||
if (errorCode.equals(ERROR_CODE_BAD_TOKEN)) {
|
||||
ViewUtil.showLongToast(context, R.string.bad_token_error_proposed_solution);
|
||||
}
|
||||
return new UploadResult(resultStatus, errorCode);
|
||||
} else {
|
||||
Date dateUploaded = parseMWDate(result.getString("/api/upload/imageinfo/@timestamp"));
|
||||
String canonicalFilename = "File:" + result.getString("/api/upload/@filename").replace("_", " "); // Title vs Filename
|
||||
String imageUrl = result.getString("/api/upload/imageinfo/@url");
|
||||
return new UploadResult(resultStatus, dateUploaded, canonicalFilename, imageUrl);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -131,22 +131,13 @@ public class CustomMwApi {
|
|||
}
|
||||
}
|
||||
|
||||
public CustomApiResult upload(String filename, InputStream file, long length, String text, String comment, String token) throws IOException {
|
||||
return this.upload(filename, file, length, text, comment, token, null);
|
||||
}
|
||||
|
||||
public CustomApiResult upload(String filename, InputStream file, String text, String comment, String token) throws IOException {
|
||||
return this.upload(filename, file, -1, text, comment, token, null);
|
||||
}
|
||||
|
||||
public CustomApiResult upload(String filename, InputStream file, long length, String text, String comment, String token, ProgressListener uploadProgressListener) throws IOException {
|
||||
Timber.d("Initiating upload for file %s", filename);
|
||||
Http.HttpRequestBuilder builder = Http.multipart(apiURL)
|
||||
public CustomApiResult uploadToStash(String filename, InputStream file, long length, String token, ProgressListener uploadProgressListener) throws IOException {
|
||||
Timber.d("Initiating upload for file %s", filename);
|
||||
Http.HttpRequestBuilder builder = Http.multipart(apiURL)
|
||||
.data("action", "upload")
|
||||
.data("stash", "1")
|
||||
.data("token", token)
|
||||
.data("text", text)
|
||||
.data("ignorewarnings", "1")
|
||||
.data("comment", comment)
|
||||
.data("filename", filename)
|
||||
.sendProgressListener(uploadProgressListener);
|
||||
if (length != -1) {
|
||||
|
|
@ -155,7 +146,20 @@ public class CustomMwApi {
|
|||
builder.file("file", filename, file);
|
||||
}
|
||||
|
||||
return CustomApiResult.fromRequestBuilder("uploadFile", builder, client);
|
||||
return CustomApiResult.fromRequestBuilder("uploadToStash", builder, client);
|
||||
}
|
||||
|
||||
public CustomApiResult uploadFromStash(String filename, String filekey, String text, String comment, String token) throws IOException {
|
||||
Http.HttpRequestBuilder builder = Http.multipart(apiURL)
|
||||
.data("action", "upload")
|
||||
.data("token", token)
|
||||
.data("ignorewarnings", "1")
|
||||
.data("text", text)
|
||||
.data("comment", comment)
|
||||
.data("filename", filename)
|
||||
.data("filekey", filekey);
|
||||
|
||||
return CustomApiResult.fromRequestBuilder("uploadFromStash", builder, client);
|
||||
}
|
||||
|
||||
public void logout() throws IOException {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import java.util.List;
|
|||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.notification.Notification;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Single;
|
||||
|
||||
public interface MediaWikiApi {
|
||||
String getUserAgent();
|
||||
|
|
@ -49,8 +50,13 @@ public interface MediaWikiApi {
|
|||
List<String> searchCategory(String title, int offset);
|
||||
|
||||
@NonNull
|
||||
UploadResult uploadFile(String filename, InputStream file, long dataLength, String pageContents, String editSummary, Uri fileUri, Uri contentProviderUri, ProgressListener progressListener) throws IOException;
|
||||
Single<UploadStash> uploadFile(String filename, InputStream file,
|
||||
long dataLength, Uri fileUri, Uri contentProviderUri,
|
||||
final ProgressListener progressListener) throws IOException;
|
||||
|
||||
@NonNull
|
||||
Single<UploadResult> uploadFileFinalize(String filename, String filekey,
|
||||
String pageContents, String editSummary) throws IOException;
|
||||
@Nullable
|
||||
String edit(String editToken, String processedPageContent, String filename, String summary) throws IOException;
|
||||
|
||||
|
|
|
|||
70
app/src/main/java/fr/free/nrw/commons/mwapi/UploadStash.java
Normal file
70
app/src/main/java/fr/free/nrw/commons/mwapi/UploadStash.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package fr.free.nrw.commons.mwapi;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class UploadStash {
|
||||
@NonNull
|
||||
private String errorCode;
|
||||
@NonNull
|
||||
private String resultStatus;
|
||||
@NonNull
|
||||
private String filename;
|
||||
@NonNull
|
||||
private String filekey;
|
||||
|
||||
@NonNull
|
||||
public final String getErrorCode() {
|
||||
return this.errorCode;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public final String getResultStatus() {
|
||||
return this.resultStatus;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public final String getFilename() {
|
||||
return this.filename;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public final String getFilekey() {
|
||||
return this.filekey;
|
||||
}
|
||||
|
||||
public UploadStash(@NonNull String errorCode, @NonNull String resultStatus, @NonNull String filename, @NonNull String filekey) {
|
||||
this.errorCode = errorCode;
|
||||
this.resultStatus = resultStatus;
|
||||
this.filename = filename;
|
||||
this.filekey = filekey;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "UploadStash(errorCode=" + this.errorCode + ", resultStatus=" + this.resultStatus + ", filename=" + this.filename + ", filekey=" + this.filekey + ")";
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return ((this.errorCode.hashCode() * 31 + this.resultStatus.hashCode()
|
||||
) * 31 + this.filename.hashCode()
|
||||
) * 31 + this.filekey.hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this != obj) {
|
||||
if (obj instanceof UploadStash) {
|
||||
UploadStash that = (UploadStash)obj;
|
||||
if (this.errorCode.equals(that.errorCode)
|
||||
&& this.resultStatus.equals(that.resultStatus)
|
||||
&& this.filename.equals(that.filename)
|
||||
&& this.filekey.equals(that.filekey)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue