-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix SVG file processing and upload validation #6665
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
Open
Roniscend
wants to merge
1
commit into
commons-app:main
Choose a base branch
from
Roniscend:fix_svg_files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import androidx.core.net.toUri | |
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.exifinterface.media.ExifInterface | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import coil.load | ||
| import fr.free.nrw.commons.databinding.ActivityEditBinding | ||
| import fr.free.nrw.commons.utils.applyEdgeToEdgeBottomInsets | ||
| import fr.free.nrw.commons.utils.applyEdgeToEdgeTopPaddingInsets | ||
|
|
@@ -104,9 +105,12 @@ class EditActivity : AppCompatActivity() { | |
| ExifInterface.TAG_GPS_LONGITUDE_REF, | ||
| ExifInterface.TAG_GPS_PROCESSING_METHOD, | ||
| ExifInterface.TAG_GPS_TIMESTAMP, | ||
| ExifInterface.TAG_IMAGE_LENGTH, | ||
| ExifInterface.TAG_IMAGE_WIDTH, | ||
| ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY, | ||
| ExifInterface.TAG_MAKE, | ||
| ExifInterface.TAG_MODEL, | ||
| ExifInterface.TAG_ORIENTATION, | ||
| ExifInterface.TAG_WHITE_BALANCE, | ||
| ExifInterface.WHITE_BALANCE_AUTO, | ||
| ExifInterface.WHITE_BALANCE_MANUAL, | ||
|
|
@@ -116,6 +120,11 @@ class EditActivity : AppCompatActivity() { | |
| sourceExifAttributeList.add(Pair(tag.toString(), attribute)) | ||
| } | ||
|
|
||
| if (imageUri.substringBefore("?").endsWith(".svg", ignoreCase = true)) { | ||
| Toast.makeText(this, "SVG files cannot be edited", Toast.LENGTH_SHORT).show() | ||
| finish() | ||
| return | ||
| } | ||
| init() | ||
| } | ||
|
|
||
|
|
@@ -124,16 +133,28 @@ class EditActivity : AppCompatActivity() { | |
| * | ||
| * This function sets up the ImageView for displaying an image, adjusts its view bounds, | ||
| * and scales the initial image to fit within the ImageView. It also sets click listeners | ||
| * for the "Rotate", "Crop" and "Save" buttons. | ||
| * for the "Rotate" and "Save" buttons. | ||
| */ | ||
| private fun init() { | ||
| binding.iv.adjustViewBounds = true | ||
| binding.iv.scaleType = ImageView.ScaleType.MATRIX | ||
| binding.iv.post { | ||
| val options = BitmapFactory.Options() | ||
| options.inJustDecodeBounds = true | ||
| BitmapFactory.decodeFile(imageUri, options) | ||
|
|
||
| if (imageUri.substringBefore("?").endsWith(".svg", ignoreCase = true)) { | ||
| binding.rotateBtn.isEnabled = false | ||
| binding.iv.load(imageUri) { | ||
| listener( | ||
| onSuccess = { _, result -> | ||
| val drawable = result.drawable | ||
| val scale = binding.iv.measuredWidth.toFloat() / drawable.intrinsicWidth.toFloat() | ||
| binding.iv.layoutParams.height = (scale * drawable.intrinsicHeight).toInt() | ||
| binding.iv.imageMatrix = scaleMatrix(scale, scale) | ||
| } | ||
| ) | ||
| } | ||
| } else { | ||
| binding.iv.post { | ||
| val options = BitmapFactory.Options() | ||
| options.inJustDecodeBounds = true | ||
| BitmapFactory.decodeFile(imageUri, options) | ||
| val bitmapWidth = options.outWidth | ||
| val bitmapHeight = options.outHeight | ||
|
|
||
|
|
@@ -193,7 +214,7 @@ class EditActivity : AppCompatActivity() { | |
| binding.iv.imageMatrix = matrix | ||
| } | ||
|
|
||
| } | ||
| }} | ||
| binding.rotateBtn.setOnClickListener { | ||
| // Allow rotation while in crop mode - overlay will update after animation | ||
| animateImageHeight() | ||
|
|
@@ -416,6 +437,16 @@ class EditActivity : AppCompatActivity() { | |
|
|
||
| var imageRotation = 0 | ||
|
|
||
| /** | ||
| * Data class representing crop coordinates. | ||
| */ | ||
| private data class CropCoordinates( | ||
| val left: Int, | ||
| val top: Int, | ||
| val width: Int, | ||
| val height: Int | ||
| ) | ||
|
|
||
| /** | ||
| * Animates the height, rotation, and scale of an ImageView to provide a smooth | ||
| * transition effect when rotating an image by 90 degrees. | ||
|
|
@@ -452,28 +483,24 @@ class EditActivity : AppCompatActivity() { | |
|
|
||
| when (rotation) { | ||
| 0, 180 -> { | ||
| imageScale = min(viewWidth / drawableWidth, maxAvailableHeight / drawableHeight) | ||
| val fitW = viewWidth / drawableHeight | ||
| val fitH = maxAvailableHeight / drawableWidth | ||
| newImageScale = min(fitW, fitH) | ||
| newViewHeight = min((drawableWidth * newImageScale).toInt(), maxAvailableHeight.toInt()) | ||
| imageScale = viewWidth / drawableWidth | ||
| newImageScale = viewWidth / drawableHeight | ||
| newViewHeight = (drawableWidth * newImageScale).toInt() | ||
| } | ||
| 90, 270 -> { | ||
| imageScale = min(viewWidth / drawableHeight, maxAvailableHeight / drawableWidth) | ||
| val fitW = viewWidth / drawableWidth | ||
| val fitH = maxAvailableHeight / drawableHeight | ||
| newImageScale = min(fitW, fitH) | ||
| newViewHeight = min((drawableHeight * newImageScale).toInt(), maxAvailableHeight.toInt()) | ||
| imageScale = viewWidth / drawableHeight | ||
| newImageScale = viewWidth / drawableWidth | ||
| newViewHeight = (drawableHeight * newImageScale).toInt() | ||
| } | ||
| else -> { | ||
| throw | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add catch instead |
||
| UnsupportedOperationException( | ||
| "rotation can 0, 90, 180 or 270. \${rotation} is unsupported" | ||
| "rotation can 0, 90, 180 or 270. $rotation is unsupported" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| val animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500L) | ||
| val animator = ValueAnimator.ofFloat(0f, 1f).setDuration(1000L) | ||
|
|
||
| animator.interpolator = AccelerateDecelerateInterpolator() | ||
|
|
||
|
|
@@ -486,12 +513,6 @@ class EditActivity : AppCompatActivity() { | |
| override fun onAnimationEnd(animation: Animator) { | ||
| imageRotation = newRotation % 360 | ||
| binding.rotateBtn.setEnabled(true) | ||
|
|
||
| // If crop mode is active, update the overlay bounds for new rotation | ||
| // Use post{} to wait for the layout pass triggered by requestLayout() | ||
| if (isCropMode) { | ||
| binding.iv.post { updateCropOverlayBounds() } | ||
| } | ||
| } | ||
|
|
||
| override fun onAnimationCancel(animation: Animator) { | ||
|
|
@@ -533,6 +554,35 @@ class EditActivity : AppCompatActivity() { | |
| animator.start() | ||
| } | ||
|
|
||
| /** | ||
| * Rotates and edits the current image, copies EXIF data, and returns the edited image path. | ||
| * | ||
| * This function retrieves the path of the current image specified by `imageUri`, | ||
| * rotates it based on the `imageRotation` angle using the `rotateImage` method | ||
| * from the `vm`, and updates the EXIF attributes of the | ||
| * rotated image based on the `sourceExifAttributeList`. It then copies the EXIF data | ||
| * using the `copyExifData` method, creates an Intent to return the edited image's file path | ||
| * as a result, and finishes the current activity. | ||
| */ | ||
| fun getRotatedImage() { | ||
| val filePath = imageUri.toUri().path | ||
| val file = filePath?.let { File(it) } | ||
|
|
||
| val rotatedImage = file?.let { vm.rotateImage(imageRotation, it, applicationContext.cacheDir) } | ||
| if (rotatedImage == null) { | ||
| Toast.makeText(this, "Failed to rotate to image", Toast.LENGTH_LONG).show() | ||
| } | ||
| val editedImageExif: ExifInterface? | ||
| if (rotatedImage?.path != null) { | ||
| editedImageExif = ExifInterface(rotatedImage.path) | ||
| copyExifData(editedImageExif) | ||
| } | ||
| val resultIntent = Intent() | ||
| resultIntent.putExtra("editedImageFilePath", rotatedImage?.toUri()?.path ?: "Error") | ||
| setResult(RESULT_OK, resultIntent) | ||
| finish() | ||
| } | ||
|
|
||
| /** | ||
| * Copies EXIF data from sourceExifAttributeList to the provided ExifInterface object. | ||
| * | ||
|
|
@@ -585,13 +635,3 @@ class EditActivity : AppCompatActivity() { | |
| return scaleFactor | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Data class to hold crop coordinates. | ||
| */ | ||
| private data class CropCoordinates( | ||
| val left: Int, | ||
| val top: Int, | ||
| val width: Int, | ||
| val height: Int | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will review this today, thanks for contributing @Roniscend. Can I know is there any reason we can't edit svg images?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neeldoshii Not sure but based on one of the issues I’m working on, the reason might be that SVG files don’t contain EXIF headers, which could be causing the problem :)
Comment link : #6642 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, our current editing flow relies on EXIF metadata, which svg do not have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The app exists because smartphones have become the main way people take pictures. By contrast, creating SVG on smartphone is an extremely niche use case. In my experience monitoring the app's uploads, most non-photography files uploaded from the app are copyright violations. So I think it makes sense to focus our limited resources on photography formats. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in that case how about we convert svg to png first? Not sure if its possible or not and how much workaround is there will check it once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have the bandwidth to maintain any SVG or PNG editing features.
We can grey out the edit button and when tapped, it could say something like "To edit non-JPG files, use third-party tools".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolas-raoul this was recently fixed in #6653 as part of #6642 ,and now the
edit imagebutton will be disabled for the non JPG files :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one sounds better actually. It's more transparent for our users, otherwise they'll keep wondering when that Edit button appears. I'll create a follow-up issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. Also just an idea for suggestion not sure it good or not. How about we provide third party tool link and user can click and go to the website (externally, in app is very hard to maintain as we don't know when to close) and once the user has converted the image to jpg, user can finally upload jpg image.