Skip to content

Commit 4c476e7

Browse files
neslihanturanmisaochan
authored andcommitted
Hotfix for overwrite issue in 2.8.0 (#1838)
* This solution is an hotfix for overrite issue came back on 2.8.0 version. What I did is checking the extension, and if it is null, adding .jpg suffix. Because commons files always have suffixes, and we should compare file names after adding suffixes. Othervise overrides are possible. * Check if file title includes an extension already, by checking if is there any dot in it. * Fix logic error * Add uncovered tests * Remove unecessary line breaks * Make Javadocs more explicit
1 parent 11c3772 commit 4c476e7

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static int licenseNameFor(String license) {
111111
}
112112

113113
/**
114-
* Fixing incorrect extension
114+
* Adds extension to filename. Converts to .jpg if system provides .jpeg, adds .jpg if no extension detected
115115
* @param title File name
116116
* @param extension Correct extension
117117
* @return File with correct extension
@@ -128,6 +128,15 @@ public static String fixExtension(String title, String extension) {
128128
.endsWith("." + extension.toLowerCase(Locale.ENGLISH))) {
129129
title += "." + extension;
130130
}
131+
132+
// If extension is still null, make it jpg. (Hotfix for https://github.com/commons-app/apps-android-commons/issues/228)
133+
// If title has an extension in it, if won't be true
134+
// FIXME: .png uploads fail when uploaded via Share
135+
if (extension == null && title.lastIndexOf(".")<=0) {
136+
extension = "jpg";
137+
title += "." + extension;
138+
}
139+
131140
return title;
132141
}
133142

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,14 @@ class UtilsFixExtensionTest {
6565
fun inWordJpegToJpgResultsInJpg() {
6666
assertEquals("X.jpeg.SAMPLE.jpg", fixExtension("X.jpeg.SAMPLE", "jpg"))
6767
}
68+
69+
@Test
70+
fun noExtensionShouldResultInJpg() {
71+
assertEquals("Sample.jpg", fixExtension("Sample", null))
72+
}
73+
74+
@Test
75+
fun extensionAlreadyInTitleShouldRemain() {
76+
assertEquals("Sample.jpg", fixExtension("Sample.jpg", null))
77+
}
6878
}

0 commit comments

Comments
 (0)