Skip to content

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

Merged
merged 7 commits into from
Jul 22, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import org.junit.jupiter.api.Test
import uniffi.wp_api.MediaCreateParams
import uniffi.wp_api.MediaListParams
import uniffi.wp_api.SparseMediaFieldWithEditContext
import uniffi.wp_api.WpAuthenticationProvider
import java.io.File
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

private const val MEDIA_ID_611: Long = 611

class MediaEndpointTest {
private val client = defaultApiClient()
private val client = mediaApiClient()

@Test
fun testMediaListRequest() = runTest {
Expand Down Expand Up @@ -74,4 +76,29 @@ class MediaEndpointTest {
assertEquals(title, response.title.rendered)
restoreTestServer()
}

fun mediaApiClient(): WpApiClient {
val testCredentials = TestCredentials.INSTANCE
val authProvider = WpAuthenticationProvider.staticWithUsernameAndPassword(
username = testCredentials.adminUsername, password = testCredentials.adminPassword
)
val requestExecutor = WpRequestExecutor(
fileResolver = FileResolverMock()
)
return WpApiClient(
wpOrgSiteApiRootUrl = testCredentials.apiRootUrl,
authProvider = authProvider,
requestExecutor = requestExecutor
)
}

class FileResolverMock: FileResolver {
// in order to properly resolve the file from the test assets, we need to do it in the following way
override fun getFile(path: String): File? =
WpAuthenticationProvider::class.java.classLoader?.getResource(path)?.file?.let {
File(
it
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rs.wordpress.api.kotlin

import java.io.File

class DefaultFileResolver : FileResolver {
override fun getFile(path: String): File? = File(path)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rs.wordpress.api.kotlin

import java.io.File

interface FileResolver {
fun getFile(path: String): File?
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const val USER_AGENT_HEADER_NAME = "User-Agent"

class WpRequestExecutor(
private val httpClient: WpHttpClient = WpHttpClient.DefaultHttpClient(),
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
private val dispatcher: CoroutineDispatcher = Dispatchers.IO,
private val fileResolver: FileResolver = DefaultFileResolver()
) : RequestExecutor {
override suspend fun execute(request: WpNetworkRequest): WpNetworkResponse =
withContext(dispatcher) {
Expand Down Expand Up @@ -88,13 +89,10 @@ class WpRequestExecutor(
mediaUploadRequest.mediaParams().forEach { (k, v) ->
multipartBodyBuilder.addFormDataPart(k, v)
}
val filePath = mediaUploadRequest.filePath()
val file =
WpRequestExecutor::class.java.classLoader?.getResource(filePath)?.file?.let {
File(
it
)
} ?: throw MediaUploadRequestExecutionException.MediaFileNotFound(filePath)
val file = fileResolver.getFile(mediaUploadRequest.filePath())
if (file == null || !file.canBeUploaded()) {
throw MediaUploadRequestExecutionException.MediaFileNotFound(mediaUploadRequest.filePath())
}
multipartBodyBuilder.addFormDataPart(
name = "file",
filename = file.name,
Expand Down Expand Up @@ -124,6 +122,8 @@ class WpRequestExecutor(
override suspend fun sleep(millis: ULong) {
delay(millis.toLong())
}

private fun File.canBeUploaded() = exists() && isFile && canRead()
}

private fun RequestExecutionErrorReason.Companion.unknownHost(e: UnknownHostException) =
Expand Down