Skip to content

send video in room return 413 Payload Too Large #17

@mohammadfrh

Description

@mohammadfrh

with this code i try to send vide :

private fun setVideoMessage(uri: Uri, file: DocumentFile?) {
    val uploadStateTracker = DefaultContentUploadStateTracker()

    uploadStateTracker.track("video_upload", object : ContentUploadStateTracker.UpdateListener {
        override fun onUpdate(state: ContentUploadStateTracker.State) {
            when (state) {
                is ContentUploadStateTracker.State.Idle -> {
                    Log.d("UploadStateTracker", "Idle state")
                }

                is ContentUploadStateTracker.State.EncryptingThumbnail -> {
                    Log.d("UploadStateTracker", "Encrypting thumbnail")
                }

                is ContentUploadStateTracker.State.CompressingImage -> {
                    Log.d("UploadStateTracker", "Compressing image")
                }

                is ContentUploadStateTracker.State.CompressingVideo -> {
                    val percent = state.percent
                    Log.d("UploadStateTracker", "Compressing video: $percent%")
                }

                is ContentUploadStateTracker.State.UploadingThumbnail -> {
                    val current = state.current
                    val total = state.total
                    Log.d("UploadStateTracker", "Uploading thumbnail: $current/$total")
                }

                is ContentUploadStateTracker.State.Encrypting -> {
                    val current = state.current
                    val total = state.total
                    Log.d("UploadStateTracker", "Encrypting video: $current/$total")
                }

                is ContentUploadStateTracker.State.Uploading -> {
                    val current = state.current
                    val total = state.total
                    Log.d("UploadStateTracker", "Uploading video: $current/$total")

                    // Calculate progress percentage
                    val progressPercentage = (current.toFloat() / total.toFloat()) * 100

                    // Update the progress in your UI
                    Log.d(
                        "UploadStateTracker",
                        "Uploading video: progressPercentage : $progressPercentage"
                    )
                }

                is ContentUploadStateTracker.State.Success -> {
                    Log.d("UploadStateTracker", "Upload completed successfully")

                    // Prepare the content data
                    val content = prepareContentData(uri)

                    // Send the content using the sendMedia method
                    room?.sendService()?.sendMedia(
                        content,
                        compressBeforeSending = false,
                        roomIds = setOf(roomId)
                    )

                    // Clear the upload state tracker
                    uploadStateTracker.clear()
                }

                is ContentUploadStateTracker.State.Failure -> {
                    val throwable = state.throwable
                    Log.e("UploadStateTracker", "Upload failed: ${throwable.message}")
                    // Clear the upload state tracker
                    uploadStateTracker.clear()
                }
            }
        }
    })
    lifecycleScope.launch {
        try {
            showSnackBar("Uploading Video, please wait...")
            val fileSize: Long = file!!.length()

            // Update the state when necessary
            uploadStateTracker.updateState(
                ContentUploadStateTracker.State.Uploading(
                    0,
                    fileSize
                )
            )

            // Simulate video upload progress
            val bufferSize = 1024
            val buffer = ByteArray(bufferSize)
            var current: Long = 0

            contentResolver.openInputStream(uri)?.use { inputStream ->
                var bytesRead = inputStream.read(buffer)
                while (bytesRead != -1) {
                    // Update the state with the current progress
                    current += bytesRead
                    uploadStateTracker.updateState(
                        ContentUploadStateTracker.State.Uploading(
                            current,
                            fileSize
                        )
                    )

                    bytesRead = inputStream.read(buffer)
                }
            }

            // Update the state to indicate a successful upload
            uploadStateTracker.updateState(ContentUploadStateTracker.State.Success)
        } catch (failure: Throwable) {
            Log.e("UploadStateTracker", "Upload failed: $failure")

            // Update the state with the failure
            uploadStateTracker.updateState(ContentUploadStateTracker.State.Failure(failure))
        } catch (failure: Exception) {
            Log.e("UploadStateTracker", "Upload failed: $failure")

            // Update the state with the failure
            uploadStateTracker.updateState(ContentUploadStateTracker.State.Failure(failure))
        }
    }

}

the file size is 15MB and server max upload size is 50MB but i get this error :

<-- 413 Payload Too Large https://im.encrym.com/_matrix/media/r0/upload (14986ms, unknown-length body)
2023-06-07 14:29:01.955 24505-24748 W The error returned by the server is not a MatrixError
2023-06-07 14:29:01.960 24505-24748 E ## ERROR java.lang.RuntimeException: HTTP 413:
2023-06-07 14:29:01.960 24505-24748 E <title>413 Request Entity Too Large</title>
2023-06-07 14:29:01.960 24505-24748 E
2023-06-07 14:29:01.960 24505-24748 E

413 Request Entity Too Large


2023-06-07 14:29:01.960 24505-24748 E
nginx
2023-06-07 14:29:01.960 24505-24748 E <script>(function(){var js =

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions