Skip to content
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

[i216] fix audio recording uploading #5066

Merged
merged 3 commits into from
Nov 14, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

## stream-chat-android-client
### 🐞 Fixed
- Fixed audio recording not being uploaded [#5066](https://github.com/GetStream/stream-chat-android/pull/5066)

### ⬆️ Improved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ internal class ClientInstrumentationTests {
runOnUi {
val client = ChatClient.Builder(apiKey, context).build()
client.getGuestToken("test-user-id", "Test name").enqueue {
client.connectUser(it.data().user, it.data().token).enqueue(initCallback)
val data = it.getOrThrow()
client.connectUser(data.user, data.token).enqueue(initCallback)
}
client.subscribe(connectedEventConsumer::onEvent)
}.andThen {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import io.getstream.result.Result
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File
import java.net.URI

internal class StreamFileUploader(
private val retrofitCdnApi: RetrofitCdnApi,
) : FileUploader {

private val filenameSanitizer = FilenameSanitizer()

override fun sendFile(
channelType: String,
channelId: String,
Expand All @@ -40,7 +41,7 @@ internal class StreamFileUploader(
callback: ProgressCallback,
): Result<UploadedFile> {
val body = file.asRequestBody(file.getMediaType())
val filename = URI(null, null, file.name, null).toASCIIString()
val filename = filenameSanitizer.sanitize(file.name)
val part = MultipartBody.Part.createFormData("file", filename, body)

return retrofitCdnApi.sendFile(
Expand All @@ -60,7 +61,7 @@ internal class StreamFileUploader(
file: File,
): Result<UploadedFile> {
val body = file.asRequestBody(file.getMediaType())
val filename = URI(null, null, file.name, null).toASCIIString()
val filename = filenameSanitizer.sanitize(file.name)
val part = MultipartBody.Part.createFormData("file", filename, body)

return retrofitCdnApi.sendFile(
Expand All @@ -81,7 +82,7 @@ internal class StreamFileUploader(
callback: ProgressCallback,
): Result<UploadedImage> {
val body = file.asRequestBody(file.getMediaType())
val filename = URI(null, null, file.name, null).toASCIIString()
val filename = filenameSanitizer.sanitize(file.name)
val part = MultipartBody.Part.createFormData("file", filename, body)

return retrofitCdnApi.sendImage(
Expand All @@ -101,7 +102,7 @@ internal class StreamFileUploader(
file: File,
): Result<UploadedImage> {
val body = file.asRequestBody(file.getMediaType())
val filename = URI(null, null, file.name, null).toASCIIString()
val filename = filenameSanitizer.sanitize(file.name)
val part = MultipartBody.Part.createFormData("file", filename, body)

return retrofitCdnApi.sendImage(
Expand Down Expand Up @@ -140,3 +141,36 @@ internal class StreamFileUploader(
).execute().toUnitResult()
}
}

private class FilenameSanitizer {
companion object {
private const val MAX_NAME_LEN = 255
private const val EMPTY = ""
}

private val allowedChars = ('a'..'z') + ('A'..'Z') + ('0'..'9') + '-' + '_'

fun sanitize(filename: String): String = try {
sanitizeInternal(filename)
} catch (_: Throwable) {
filename
}

private fun sanitizeInternal(filename: String): String {
// Separate the extension and the base name
val extension = filename.substringAfterLast(delimiter = '.', missingDelimiterValue = EMPTY)
val baseName = if (extension.isNotEmpty()) filename.removeSuffix(suffix = ".$extension") else filename

// Replace invalid characters in the base name
var sanitizedBaseName = baseName.map { if (it in allowedChars) it else '_' }.joinToString(EMPTY)

// Truncate the base name if it is too long
val maxBaseNameLength = MAX_NAME_LEN - extension.length - 1 // Adjust for the extension and dot
if (sanitizedBaseName.length > maxBaseNameLength) {
sanitizedBaseName = sanitizedBaseName.substring(0, maxBaseNameLength)
}

// Reconstruct the filename with the extension
return if (extension.isNotEmpty()) "$sanitizedBaseName.$extension" else sanitizedBaseName
}
}