Skip to content

Fix uploads getting stuck #2309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private void updateContributionFragmentTabContent(boolean isContributionsListFra

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (IntentUtils.shouldContributionsListHandle(requestCode, resultCode, data)) {
if (IntentUtils.shouldContributionsHandle(requestCode, resultCode, data)) {
List<Image> images = ImagePicker.getImages(data);
Intent shareIntent = controller.handleImagesPicked(ImageUtils.getUriListFromImages(images), requestCode);
startActivity(shareIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import android.app.Activity;
import android.content.ContentProviderClient;
import android.content.Context;
import android.net.Uri;
import android.support.v4.util.LruCache;
import android.view.inputmethod.InputMethodManager;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -29,6 +33,8 @@
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.upload.UploadController;
import fr.free.nrw.commons.utils.ConfigUtils;
import fr.free.nrw.commons.utils.UriDeserializer;
import fr.free.nrw.commons.utils.UriSerializer;
import fr.free.nrw.commons.wikidata.WikidataEditListener;
import fr.free.nrw.commons.wikidata.WikidataEditListenerImpl;

Expand Down Expand Up @@ -154,8 +160,8 @@ public BasicKvStore providesCategoryKvStore(Context context) {

@Provides
@Named("direct_nearby_upload_prefs")
public JsonKvStore providesDirectNearbyUploadKvStore(Context context) {
return new JsonKvStore(context, "direct_nearby_upload_prefs");
public JsonKvStore providesDirectNearbyUploadKvStore(Context context, Gson gson) {
return new JsonKvStore(context, "direct_nearby_upload_prefs", gson);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.free.nrw.commons.di;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;

import com.google.gson.Gson;
Expand All @@ -18,6 +19,8 @@
import fr.free.nrw.commons.kvstore.BasicKvStore;
import fr.free.nrw.commons.mwapi.ApacheHttpClientMediaWikiApi;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.utils.UriDeserializer;
import fr.free.nrw.commons.utils.UriSerializer;
import okhttp3.Cache;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -63,7 +66,10 @@ public HttpUrl provideMwUrl() {
@Provides
@Singleton
public Gson provideGson() {
return new GsonBuilder().create();
return new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();
}

}
11 changes: 7 additions & 4 deletions app/src/main/java/fr/free/nrw/commons/kvstore/JsonKvStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@
import javax.annotation.Nullable;

public class JsonKvStore extends BasicKvStore {
private final Gson gson = new Gson();
private final Gson gson;

public JsonKvStore(Context context, String storeName) {
public JsonKvStore(Context context, String storeName, Gson gson) {
super(context, storeName);
this.gson = gson;
}

public JsonKvStore(Context context, String storeName, int version) {
public JsonKvStore(Context context, String storeName, int version, Gson gson) {
super(context, storeName, version);
this.gson = gson;
}

public JsonKvStore(Context context, String storeName, int version, boolean clearAllOnUpgrade) {
public JsonKvStore(Context context, String storeName, int version, boolean clearAllOnUpgrade, Gson gson) {
super(context, storeName, version, clearAllOnUpgrade);
this.gson = gson;
}

public <T> void putAllJsons(Map<String, T> jsonMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ public UploadResult uploadFile(String filename,

CustomApiResult result = api.upload(filename, file, dataLength, pageContents, editSummary, getCentralAuthToken(), getEditToken(), progressListener::onProgress);

Timber.wtf("Result: " + result.toString());
Timber.d("Result: %s", result.toString());

String resultStatus = result.getString("/api/upload/@result");

Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/fr/free/nrw/commons/mwapi/CustomApiResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,32 @@ public class CustomApiResult {
this.evaluator = XPathFactory.newInstance().newXPath();
}

static CustomApiResult fromRequestBuilder(Http.HttpRequestBuilder builder, HttpClient client) throws IOException {

static CustomApiResult fromRequestBuilder(String requestIdentifier, Http.HttpRequestBuilder builder, HttpClient client) throws IOException {
try {
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(builder.use(client).charset("utf-8").data("format", "xml").asResponse().getEntity().getContent());
printStringFromDocument(doc);
printStringFromDocument(requestIdentifier, doc);
return new CustomApiResult(doc);
} catch (ParserConfigurationException e) {
// I don't know wtf I can do about this on...
Timber.e(e, "Error occurred while parsing the response for method %s", requestIdentifier);
throw new RuntimeException(e);
} catch (IllegalStateException e) {
// So, this should never actually happen - since we assume MediaWiki always generates valid json
// So the only thing causing this would be a network truncation
// Sooo... I can throw IOError
// Thanks Java, for making me spend significant time on shit that happens once in a bluemoon
// I surely am writing Nuclear Submarine controller code
Timber.e(e, "Error occurred while parsing the response for method %s", requestIdentifier);
throw new IOError(e);
} catch (SAXException e) {
// See Rant above
Timber.e(e, "Error occurred while parsing the response for method %s", requestIdentifier);
throw new IOError(e);
}
}

public static void printStringFromDocument(Document doc)
public static void printStringFromDocument(String requestIdentifier, Document doc)
{
try
{
Expand All @@ -69,11 +71,11 @@ public static void printStringFromDocument(Document doc)
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
Timber.d("API response is\n %s", writer.toString());
Timber.d("API response for method %s is\n %s", requestIdentifier, writer.toString());
}
catch(TransformerException ex)
{
Timber.d("Error occurred in transforming", ex);
Timber.e(ex, "Error occurred in transforming response for method %s", requestIdentifier);
}
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/mwapi/CustomMwApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public CustomApiResult upload(String filename, InputStream file, String text, St
}

public CustomApiResult upload(String filename, InputStream file, long length, String text, String comment, String centralAuthToken, String token, ProgressListener uploadProgressListener) throws IOException {
Timber.d("Initiating upload for file %s", filename);
Http.HttpRequestBuilder builder = Http.multipart(apiURL)
.data("action", "upload")
.data("token", token)
Expand All @@ -155,7 +156,7 @@ public CustomApiResult upload(String filename, InputStream file, long length, St
builder.file("file", filename, file);
}

return CustomApiResult.fromRequestBuilder(builder, client);
return CustomApiResult.fromRequestBuilder("uploadFile", builder, client);
}

public void logout() throws IOException {
Expand All @@ -174,7 +175,7 @@ private CustomApiResult makeRequest(String method, HashMap<String, Object> param
builder = Http.get(apiURL);
}
builder.data(params);
return CustomApiResult.fromRequestBuilder(builder, client);
return CustomApiResult.fromRequestBuilder(apiURL, builder, client);
}
}
;
11 changes: 11 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/mwapi/UploadResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public class UploadResult {
this.imageUrl = imageUrl;
}

@Override
public String toString() {
return "UploadResult{" +
"errorCode='" + errorCode + '\'' +
", resultStatus='" + resultStatus + '\'' +
", dateUploaded='" + dateUploaded.toString() + '\'' +
", imageUrl='" + imageUrl + '\'' +
", canonicalFilename='" + canonicalFilename + '\'' +
'}';
}

/**
* Gets uploaded date
* @return Upload date
Expand Down
14 changes: 2 additions & 12 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyBaseMarker.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;

import fr.free.nrw.commons.utils.UriDeserializer;
import fr.free.nrw.commons.utils.UriSerializer;

public class NearbyBaseMarker extends BaseMarkerOptions<NearbyMarker, NearbyBaseMarker> {
Expand All @@ -33,19 +32,14 @@ public NearbyBaseMarker[] newArray(int size) {
}

private NearbyBaseMarker(Parcel in) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();

position(in.readParcelable(LatLng.class.getClassLoader()));
snippet(in.readString());
String iconId = in.readString();
Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader());
Icon icon = IconFactory.recreate(iconId, iconBitmap);
icon(icon);
title(in.readString());
String gsonString = in.readString();
place(gson.fromJson(gsonString, Place.class));
place(in.readParcelable(Place.class.getClassLoader()));
}

public NearbyBaseMarker place(Place place) {
Expand Down Expand Up @@ -74,15 +68,11 @@ public int describeContents() {

@Override
public void writeToParcel(Parcel dest, int flags) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();

dest.writeParcelable(position, flags);
dest.writeString(snippet);
dest.writeString(icon.getId());
dest.writeParcelable(icon.getBitmap(), flags);
dest.writeString(title);
dest.writeString(gson.toJson(place));
dest.writeParcelable(place, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class NearbyFragment extends CommonsDaggerSupportFragment
@Inject
@Named("application_preferences")
BasicKvStore applicationKvStore;
@Inject Gson gson;

public NearbyMapFragment nearbyMapFragment;
private NearbyListFragment nearbyListFragment;
Expand Down Expand Up @@ -293,9 +294,6 @@ public void refreshView(LocationServiceManager.LocationChangeType locationChange
progressBar.setVisibility(View.VISIBLE);

//TODO: This hack inserts curLatLng before populatePlaces is called (see #1440). Ideally a proper fix should be found
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();
String gsonCurLatLng = gson.toJson(curLatLng);
bundle.clear();
bundle.putString("CurLatLng", gsonCurLatLng);
Expand All @@ -313,9 +311,6 @@ public void refreshView(LocationServiceManager.LocationChangeType locationChange

} else if (locationChangeType
.equals(LOCATION_SLIGHTLY_CHANGED)) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();
String gsonCurLatLng = gson.toJson(curLatLng);
bundle.putString("CurLatLng", gsonCurLatLng);
updateMapFragment(false,true, null, null);
Expand Down Expand Up @@ -384,9 +379,6 @@ private void populatePlaces(NearbyController.NearbyPlacesInfo nearbyPlacesInfo)
Timber.d("Populating nearby places");
List<Place> placeList = nearbyPlacesInfo.placeList;
LatLng[] boundaryCoordinates = nearbyPlacesInfo.boundaryCoordinates;
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();
String gsonPlaceList = gson.toJson(placeList);
String gsonCurLatLng = gson.toJson(curLatLng);
String gsonBoundaryCoordinates = gson.toJson(boundaryCoordinates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ public class NearbyListFragment extends DaggerFragment {
}.getType();
private static final Type CUR_LAT_LNG_TYPE = new TypeToken<LatLng>() {
}.getType();
private static final Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();

private NearbyAdapterFactory adapterFactory;
private RecyclerView recyclerView;

@Inject ContributionController controller;
@Inject @Named("direct_nearby_upload_prefs") JsonKvStore directKvStore;
@Inject @Named("default_preferences") BasicKvStore defaultKvStore;
@Inject Gson gson;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public class NearbyMapFragment extends DaggerFragment {
@Inject @Named("direct_nearby_upload_prefs") JsonKvStore directKvStore;
@Inject @Named("default_preferences") BasicKvStore defaultKvStore;
@Inject BookmarkLocationsDao bookmarkLocationDao;
@Inject
ContributionController controller;
@Inject ContributionController controller;
@Inject Gson gson;

private static final double ZOOM_LEVEL = 14f;

Expand All @@ -148,9 +148,6 @@ public void onCreate(Bundle savedInstanceState) {
Timber.d("Nearby map fragment created");

Bundle bundle = this.getArguments();
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();
if (bundle != null) {
String gsonPlaceList = bundle.getString("PlaceList");
String gsonLatLng = bundle.getString("CurLatLng");
Expand Down Expand Up @@ -220,9 +217,6 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
public void updateMapSlightly() {
Timber.d("updateMapSlightly called, bundle is:"+ bundleForUpdates);
if (mapboxMap != null) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();
if (bundleForUpdates != null) {
String gsonLatLng = bundleForUpdates.getString("CurLatLng");
Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {}.getType();
Expand All @@ -242,10 +236,6 @@ public void updateMapSignificantlyForCurrentLocation() {
Timber.d("updateMapSignificantlyForCurrentLocation called, bundle is:"+ bundleForUpdates);
if (mapboxMap != null) {
if (bundleForUpdates != null) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriDeserializer())
.create();

String gsonPlaceList = bundleForUpdates.getString("PlaceList");
String gsonLatLng = bundleForUpdates.getString("CurLatLng");
String gsonBoundaryCoordinates = bundleForUpdates.getString("BoundaryCoord");
Expand Down
Loading