Skip to content
Open
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
16 changes: 16 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/upload/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,20 @@ object FileUtils {
}
return true
}
// list of supported MIME types
private val imageAllowList = listOf(
"image/jpeg",
"image/jpg",
"image/png",
"image/svg+xml"
)

fun isSupportedFileType(context: Context, uri: Uri): Boolean {
val mimeType = getMimeType(context, uri)
return if (mimeType == null) {
false
} else {
imageAllowList.contains(mimeType.lowercase(Locale.getDefault()))
}
}
}
49 changes: 44 additions & 5 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,20 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
receiveInternalSharedItems()
}

if (uploadableFiles.isEmpty()) {
handleNullMedia()
} else {
//Show thumbnails
// Filter the files using the correct property

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make that comment more understandable to someone who is not familiar with the context?

val originalSize = uploadableFiles.size
uploadableFiles = uploadableFiles.filter { file ->
file.contentUri?.let { uri ->
FileUtils.isSupportedFileType(this, uri)
} ?: false
}.toMutableList()

fun proceedWithSetup() {
if (uploadableFiles.isEmpty()) {
handleNullMedia()
return
}

if (uploadableFiles.size > 1) {
if (!defaultKvStore.getBoolean("hasAlreadyLaunchedCategoriesDialog")) {
// If there is only file, no need to show the image thumbnails
Expand Down Expand Up @@ -700,7 +710,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C

uploadImagesAdapter!!.fragments = fragments!!
binding.vpUpload.offscreenPageLimit = fragments!!.size
}

// Saving size of uploadableFiles
store!!.putInt(KEY_FOR_CURRENT_UPLOAD_IMAGE_SIZE, uploadableFiles.size)
}
Expand All @@ -713,6 +723,35 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
* @param index Index of image to be removed
* @param maxSize Max size of the `uploadableFiles`
*/


if (uploadableFiles.isEmpty() && originalSize > 0) {
showAlertDialog(
this,
getString(R.string.unsupported_format_title),
getString(R.string.unsupported_format_desc),
getString(R.string.ok)
) {
finish()
}
return
}

if (uploadableFiles.size < originalSize) {
showAlertDialog(
this,
getString(R.string.unsupported_files_skipped_title),
getString(R.string.unsupported_files_skipped_desc),
getString(R.string.ok)
) {
proceedWithSetup()
}
return
}

proceedWithSetup()
}

override fun highlightNextImageOnCancelledImage(index: Int, maxSize: Int) {
if (index < maxSize) {
binding.vpUpload.setCurrentItem(index + 1, false)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -904,4 +904,9 @@ Upload your first media by tapping on the add button.</string>
<string name="license_cc0_long_description">This license allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, with no conditions.</string>
<string name="license_cc_by_long_description">This license allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, even for commercial purposes, as long as attribution is given to the creator.</string>
<string name="license_cc_by_sa_long_description">This license allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, even for commercial purposes, as long as attribution is given to the creator and the new work is licensed under identical terms.</string>

<string name="unsupported_format_title">Unsupported Format</string>
<string name="unsupported_format_desc">The selected files could not be opened. Wikimedia Commons strictly requires JPEG, PNG, or SVG formats.</string>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem: When we add a new supported format, this will need to be re-localized in dozens of languages.

Is there any way to store "JPEG", "PNG", "SVG" as a list, and have the app use this list?

Please note that , is not universal (for instance Japan uses instead) so either find a localization method that allows this, or display each format on a new line after the explanation.

This list should be used by unsupported_files_skipped_desc too.

<string name="unsupported_files_skipped_title">Unsupported Files Skipped</string>
<string name="unsupported_files_skipped_desc">Some files were skipped due to an unsupported format. The app will continue uploading your remaining JPEG, PNG, or SVG files.</string>
</resources>
Loading