Skip to content

Commit 7b5b916

Browse files
albendzneslihanturan
authored andcommitted
Fix preview crash on local files during upload (#1696)
* Try to get the localized version of the wikipedia article before defaulting to the English version. Tested with Spanish on physical Android device. Other notes: Difficulties building with gradle due to dexcount plugin: KeepSafe/dexcount-gradle-plugin#234. In testing, disabled the plugin. * Update article fetch to not include unnecessary SERVICE line * Add checks for local file, check for string length
1 parent 369fe31 commit 7b5b916

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

app/src/main/java/fr/free/nrw/commons/MediaWikiImageView.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,16 @@ public void setMedia(Media media) {
5151
return;
5252
}
5353

54-
if (thumbnailUrlCache.get(media.getFilename()) != null) {
55-
setImageUrl(thumbnailUrlCache.get(media.getFilename()));
56-
} else {
57-
setImageUrl(null);
54+
if(media.getFilename() != null) {
55+
if (thumbnailUrlCache.get(media.getFilename()) != null) {
56+
setImageUrl(thumbnailUrlCache.get(media.getFilename()));
57+
} else {
58+
setImageUrl(null);
59+
currentThumbnailTask = new ThumbnailFetchTask(media, mwApi);
60+
currentThumbnailTask.execute(media.getFilename());
61+
}
62+
} else { // local image
63+
setImageUrl(media.getLocalUri().toString());
5864
currentThumbnailTask = new ThumbnailFetchTask(media, mwApi);
5965
currentThumbnailTask.execute(media.getFilename());
6066
}

app/src/main/java/fr/free/nrw/commons/Utils.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ public static String urlEncode(String url) {
7676
* @return string with capitalized first character
7777
*/
7878
public static String capitalize(String string) {
79-
return string.substring(0, 1).toUpperCase(Locale.getDefault()) + string.substring(1);
79+
if(string.length() > 0) {
80+
return string.substring(0, 1).toUpperCase(Locale.getDefault()) + string.substring(1);
81+
} else {
82+
return string;
83+
}
8084
}
8185

8286
/**

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

+4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ protected void onPreExecute() {
245245

246246
@Override
247247
protected Boolean doInBackground(Void... voids) {
248+
// Local files have no filename yet
249+
if(media.getFilename() == null) {
250+
return Boolean.FALSE;
251+
}
248252
try {
249253
extractor.fetch(media.getFilename(), licenseList);
250254
return Boolean.TRUE;

app/src/test/kotlin/fr/free/nrw/commons/UtilsTest.kt

+4
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ class UtilsTest {
2828
@Test fun `capitalize - pass Japanase characters`() {
2929
Assert.assertThat(Utils.capitalize("こんにちは"), _is("こんにちは"))
3030
}
31+
32+
@Test fun `capitalize does not fail on empty string`() {
33+
Assert.assertThat(Utils.capitalize(""), _is(""))
34+
}
3135
}

0 commit comments

Comments
 (0)