commonMain.net.folivo.trixnity.clientserverapi.client.MediaApiClient.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trixnity-clientserverapi-client Show documentation
Show all versions of trixnity-clientserverapi-client Show documentation
Multiplatform Kotlin SDK for matrix-protocol
package net.folivo.trixnity.clientserverapi.client
import io.ktor.client.plugins.*
import io.ktor.http.*
import kotlinx.coroutines.flow.MutableStateFlow
import net.folivo.trixnity.clientserverapi.model.media.*
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
interface MediaApiClient {
/**
* @see [GetMediaConfigLegacy]
*/
@Deprecated("use getConfig instead")
suspend fun getConfigLegacy(): Result
/**
* @see [GetMediaConfig]
*/
suspend fun getConfig(): Result
/**
* @see [CreateMedia]
*/
suspend fun createMedia(): Result
/**
* @see [UploadMedia]
*/
suspend fun upload(
media: Media,
progress: MutableStateFlow? = null,
timeout: Duration = Duration.INFINITE,
): Result
/**
* @see [UploadMediaByContentUri]
*/
suspend fun upload(
serverName: String,
mediaId: String,
media: Media,
progress: MutableStateFlow? = null,
timeout: Duration = Duration.INFINITE,
): Result
/**
* @see [DownloadMediaLegacy]
*/
@Deprecated("use download instead")
suspend fun downloadLegacy(
mxcUri: String,
allowRemote: Boolean? = null,
progress: MutableStateFlow? = null,
timeout: Duration = Duration.INFINITE,
downloadHandler: suspend (Media) -> Unit
): Result
/**
* @see [DownloadMedia]
*/
suspend fun download(
mxcUri: String,
progress: MutableStateFlow? = null,
timeout: Duration? = null,
downloadHandler: suspend (Media) -> Unit
): Result
/**
* @see [DownloadThumbnailLegacy]
*/
@Deprecated("use downloadThumbnail instead")
suspend fun downloadThumbnailLegacy(
mxcUri: String,
width: Long,
height: Long,
method: ThumbnailResizingMethod,
allowRemote: Boolean? = null,
progress: MutableStateFlow? = null,
timeout: Duration = Duration.INFINITE,
downloadHandler: suspend (Media) -> Unit
): Result
/**
* @see [DownloadThumbnail]
*/
suspend fun downloadThumbnail(
mxcUri: String,
width: Long,
height: Long,
method: ThumbnailResizingMethod,
animated: Boolean? = null,
progress: MutableStateFlow? = null,
timeout: Duration? = null,
downloadHandler: suspend (Media) -> Unit
): Result
/**
* @see [GetUrlPreviewLegacy]
*/
@Deprecated("use getUrlPreview instead")
suspend fun getUrlPreviewLegacy(
url: String,
timestamp: Long? = null,
): Result
/**
* @see [GetUrlPreview]
*/
suspend fun getUrlPreview(
url: String,
timestamp: Long? = null,
): Result
}
class MediaApiClientImpl(private val httpClient: MatrixClientServerApiHttpClient) : MediaApiClient {
@Deprecated("use getConfig instead")
override suspend fun getConfigLegacy(): Result =
httpClient.request(GetMediaConfigLegacy)
override suspend fun getConfig(): Result =
httpClient.request(GetMediaConfig)
override suspend fun createMedia(): Result =
httpClient.request(CreateMedia)
override suspend fun upload(
media: Media,
progress: MutableStateFlow?,
timeout: Duration,
): Result =
httpClient.request(UploadMedia(media.contentDisposition?.parameter("filename")), media) {
timeout {
requestTimeoutMillis = timeout.inWholeMilliseconds
}
if (progress != null)
onUpload { transferred, total ->
progress.value = FileTransferProgress(transferred, total)
}
}
override suspend fun upload(
serverName: String,
mediaId: String,
media: Media,
progress: MutableStateFlow?,
timeout: Duration,
): Result =
httpClient.request(
UploadMediaByContentUri(
serverName,
mediaId,
media.contentDisposition?.parameter("filename")
), media
) {
timeout {
requestTimeoutMillis = timeout.inWholeMilliseconds
}
if (progress != null)
onUpload { transferred, total ->
progress.value = FileTransferProgress(transferred, total)
}
}
@Deprecated("use download instead")
override suspend fun downloadLegacy(
mxcUri: String,
allowRemote: Boolean?,
progress: MutableStateFlow?,
timeout: Duration,
downloadHandler: suspend (Media) -> Unit
): Result {
val uri = Url(mxcUri)
if (uri.protocol.name != "mxc") return Result.failure(IllegalArgumentException("url protocol was not mxc"))
val (serverName, mediaId) = mxcUri.removePrefix("mxc://")
.let { it.substringBefore("/") to it.substringAfter("/") }
return httpClient.withRequest(
endpoint = DownloadMediaLegacy(serverName, mediaId, allowRemote, timeoutMs = timeout.inWholeMilliseconds),
requestBuilder = {
method = HttpMethod.Get
timeout {
requestTimeoutMillis = timeout.inWholeMilliseconds
}
if (progress != null)
onDownload { transferred, total ->
progress.value = FileTransferProgress(transferred, total)
}
},
responseHandler = downloadHandler
)
}
override suspend fun download(
mxcUri: String,
progress: MutableStateFlow?,
timeout: Duration?,
downloadHandler: suspend (Media) -> Unit
): Result {
val uri = Url(mxcUri)
if (uri.protocol.name != "mxc") return Result.failure(IllegalArgumentException("url protocol was not mxc"))
val (serverName, mediaId) = mxcUri.removePrefix("mxc://")
.let { it.substringBefore("/") to it.substringAfter("/") }
return httpClient.withRequest(
endpoint = DownloadMedia(serverName, mediaId, timeout?.inWholeMilliseconds),
requestBuilder = {
method = HttpMethod.Get
if (timeout != null)
timeout {
requestTimeoutMillis =
timeout.plus(10.seconds).inWholeMilliseconds
}
if (progress != null)
onDownload { transferred, total ->
progress.value = FileTransferProgress(transferred, total)
}
},
responseHandler = downloadHandler
)
}
@Deprecated("use downloadThumbnail instead")
override suspend fun downloadThumbnailLegacy(
mxcUri: String,
width: Long,
height: Long,
method: ThumbnailResizingMethod,
allowRemote: Boolean?,
progress: MutableStateFlow?,
timeout: Duration,
downloadHandler: suspend (Media) -> Unit
): Result {
val uri = Url(mxcUri)
if (uri.protocol.name != "mxc") return Result.failure(IllegalArgumentException("url protocol was not mxc"))
val (serverName, mediaId) = mxcUri.removePrefix("mxc://")
.let { it.substringBefore("/") to it.substringAfter("/") }
return httpClient.withRequest(
endpoint = DownloadThumbnailLegacy(
serverName = serverName,
mediaId = mediaId,
width = width,
height = height,
method = method,
allowRemote = allowRemote,
timeoutMs = timeout.inWholeMilliseconds,
),
requestBuilder = {
this.method = HttpMethod.Get
timeout {
requestTimeoutMillis = timeout.inWholeMilliseconds
}
if (progress != null)
onDownload { transferred, total ->
progress.value = FileTransferProgress(transferred, total)
}
},
responseHandler = downloadHandler
)
}
override suspend fun downloadThumbnail(
mxcUri: String,
width: Long,
height: Long,
method: ThumbnailResizingMethod,
animated: Boolean?,
progress: MutableStateFlow?,
timeout: Duration?,
downloadHandler: suspend (Media) -> Unit
): Result {
val uri = Url(mxcUri)
if (uri.protocol.name != "mxc") return Result.failure(IllegalArgumentException("url protocol was not mxc"))
val (serverName, mediaId) = mxcUri.removePrefix("mxc://")
.let { it.substringBefore("/") to it.substringAfter("/") }
return httpClient.withRequest(
endpoint = DownloadThumbnail(
serverName = serverName,
mediaId = mediaId,
width = width,
height = height,
method = method,
animated = animated,
timeoutMs = timeout?.inWholeMilliseconds
),
requestBuilder = {
this.method = HttpMethod.Get
if (timeout != null)
timeout {
requestTimeoutMillis = timeout.plus(10.seconds).inWholeMilliseconds
}
if (progress != null)
onDownload { transferred, total ->
progress.value = FileTransferProgress(transferred, total)
}
},
responseHandler = downloadHandler
)
}
@Deprecated("use getUrlPreview instead")
override suspend fun getUrlPreviewLegacy(
url: String,
timestamp: Long?,
): Result =
httpClient.request(GetUrlPreviewLegacy(url, timestamp))
override suspend fun getUrlPreview(
url: String,
timestamp: Long?,
): Result =
httpClient.request(GetUrlPreview(url, timestamp))
}