-
Notifications
You must be signed in to change notification settings - Fork 3
Kotlin wrapper fix: use File direct access to fix the FileNotFound exception #810
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
Conversation
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.
Pull Request Overview
This PR fixes a FileNotFound exception in the Kotlin wrapper by changing how media files are accessed during upload requests. The change removes resource-based file loading in favor of direct file system access with proper validation.
- Replaces classloader resource loading with direct File constructor access
- Adds explicit file readability validation using
canRead()
- Simplifies the file handling logic while maintaining error handling
native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt
Outdated
Show resolved
Hide resolved
native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/api/kotlin/WpRequestExecutor.kt
Outdated
Show resolved
Hide resolved
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.
@adalpari It looks like the create media integration test is broken:
java.lang.AssertionError: Request wasn't successful: MediaFileNotFound(filePath=/test-data/test_media.jpg)
Yes, I saw it. And actually, that's the reason why the function was not working, but the test was. The function was getting the file as a resource of the project, which is available ONLY inside the project or in test build. However, in prod build we are actually sending external files paths. Do you happen to know the absolute path of the file? Maybe the test will work that way. There are other two alternatives, but since I cannot build the project I am a bit limited:
As said I am a bit limited, specially for option 2. Wdyt? |
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.
I made a minor suggestion to use interface
over open class
.
@@ -168,3 +167,7 @@ private fun requestExecutionFailedWith(reason: RequestExecutionErrorReason) = | |||
redirects = null, | |||
reason = reason | |||
) | |||
|
|||
open class FileResolver { |
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.
Shouldn't this be an interface?
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.
Good point!
@@ -80,6 +81,7 @@ class WpRequestExecutor( | |||
} | |||
} | |||
|
|||
@Suppress("ComplexCondition") |
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.
Is this due to if (file == null || !file.exists() || !file.isFile || !file.canRead())
condition?
It's fine to suppress it, but I find it weird that it's causing a warning for this 😕
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 it's because of that. I can extract the file checks though.
I've now made the changes you suggested |
Problem
The Kotlin wrapper was experiencing file access issues during media uploads, throwing MediaUploadRequestExecutionException.MediaFileNotFound exceptions when attempting to upload files. This prevented successful file uploads through the WordPress REST API implementation.
Solution
This PR resolves the file access issue by modifying the Kotlin wrapper to use direct File access instead of the previous file handling approach. The change ensures that files can be properly accessed and read during the upload process, eliminating the MediaFileNotFound exception.