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
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ChunkFromFileRequestBody(
}
} catch (exception: Exception) {
Timber.e(exception, "Transferred " + alreadyTransferred + " bytes from a total of " + file.length())
throw exception
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,29 @@ class CreateTusUploadRemoteOperation(
Base64.encodeToString(bytes, Base64.NO_WRAP)
}

override fun run(client: OpenCloudClient): RemoteOperationResult<CreationResult> = try {
// Determine TUS endpoint URL based on provided parameters
val targetFileUrl = if (!tusUrl.isNullOrBlank()) {
tusUrl
} else {
val baseCollection = (collectionUrlOverride
?: client.userFilesWebDavUri.toString()).trim()
// Remove trailing slash - OpenCloud expects no slash on space endpoints
val resolvedCollection = buildCollectionUrl(baseCollection, remotePath).trimEnd('/')
Timber.d("TUS resolved collection: %s", resolvedCollection)
resolvedCollection
}
override fun run(client: OpenCloudClient): RemoteOperationResult<CreationResult> {
var creationUploadFile: RandomAccessFile? = null
return try {
// Determine TUS endpoint URL based on provided parameters
val targetFileUrl = if (!tusUrl.isNullOrBlank()) {
tusUrl
} else {
val baseCollection = (collectionUrlOverride
?: client.userFilesWebDavUri.toString()).trim()
// Remove trailing slash - OpenCloud expects no slash on space endpoints
val resolvedCollection = buildCollectionUrl(baseCollection, remotePath).trimEnd('/')
Timber.d("TUS resolved collection: %s", resolvedCollection)
resolvedCollection
}

Timber.d("TUS Creation URL: %s", targetFileUrl)

// Prepare request body first
val postBody: RequestBody = if (useCreationWithUpload && (firstChunkSize ?: 0L) > 0L) {
// creation-with-upload: include first chunk
// Don't use .use{} here - the channel must stay open for OkHttp to read
// Keep the channel open until OkHttp finishes reading the body.
val raf = RandomAccessFile(file, "r")
creationUploadFile = raf
val channel: FileChannel = raf.channel
ChunkFromFileRequestBody(
file = file,
Expand Down Expand Up @@ -158,10 +161,17 @@ class CreateTusUploadRemoteOperation(
Timber.w("TUS creation failed with status: %d", status)
RemoteOperationResult<CreationResult>(postMethod).apply { data = null }
}
} catch (e: Exception) {
val result = RemoteOperationResult<CreationResult>(e)
Timber.e(e, "TUS creation operation failed")
result
} catch (e: Exception) {
val result = RemoteOperationResult<CreationResult>(e)
Timber.e(e, "TUS creation operation failed")
result
} finally {
try {
creationUploadFile?.close()
} catch (e: Exception) {
Timber.w(e, "Failed to close TUS creation upload file")
}
}
}

private fun isSuccess(status: Int) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ import eu.opencloud.android.lib.common.OpenCloudAccount
import eu.opencloud.android.lib.common.OpenCloudClient
import eu.opencloud.android.lib.common.accounts.AccountUtils
import eu.opencloud.android.lib.common.authentication.OpenCloudCredentialsFactory
import eu.opencloud.android.lib.common.network.ChunkFromFileRequestBody
import eu.opencloud.android.lib.common.operations.RemoteOperationResult
import eu.opencloud.android.lib.resources.files.tus.CreateTusUploadRemoteOperation.Base64Encoder
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import okio.Buffer
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.io.File
import java.io.RandomAccessFile
import java.nio.channels.ClosedChannelException
import java.util.Base64

@RunWith(RobolectricTestRunner::class)
Expand Down Expand Up @@ -245,6 +249,32 @@ class TusIntegrationTest {
// creation-with-upload sends Content-Type and Content-Length for the chunk
assertEquals("application/offset+octet-stream", postReq.getHeader("Content-Type"))
assertEquals(firstChunkSize.toString(), postReq.getHeader("Content-Length"))
assertTrue("Local file should be deletable after TUS creation-with-upload", localFile.delete())
}

@Test
fun chunk_body_propagates_channel_failures() {
val localFile = File.createTempFile("tus", ".bin").apply {
writeBytes(ByteArray(10) { it.toByte() })
}
val raf = RandomAccessFile(localFile, "r")
val body = ChunkFromFileRequestBody(
file = localFile,
contentType = null,
channel = raf.channel,
chunkSize = 5
)

raf.close()

try {
body.writeTo(Buffer())
fail("Expected closed channel failure")
} catch (expected: ClosedChannelException) {
// Expected failure must reach OkHttp so the upload can fail.
} finally {
localFile.delete()
}
}

@Test
Expand Down
Loading