-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move downloadManagerProvider to the constructor level in LcpService
- Loading branch information
Showing
6 changed files
with
169 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...rc/main/java/org/readium/r2/shared/util/downloads/foreground/ForegroundDownloadManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2023 Readium Foundation. All rights reserved. | ||
* Use of this source code is governed by the BSD-style license | ||
* available in the top-level LICENSE file of the project. | ||
*/ | ||
|
||
package org.readium.r2.shared.util.downloads.foreground | ||
|
||
import java.io.File | ||
import java.util.UUID | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import org.readium.r2.shared.util.ThrowableError | ||
import org.readium.r2.shared.util.Try | ||
import org.readium.r2.shared.util.downloads.DownloadManager | ||
import org.readium.r2.shared.util.http.HttpClient | ||
import org.readium.r2.shared.util.http.HttpException | ||
import org.readium.r2.shared.util.http.HttpRequest | ||
|
||
public class ForegroundDownloadManager( | ||
private val httpClient: HttpClient, | ||
private val listener: DownloadManager.Listener | ||
) : DownloadManager { | ||
|
||
private val coroutineScope: CoroutineScope = | ||
MainScope() | ||
|
||
private val jobs: MutableMap<DownloadManager.RequestId, Job> = | ||
mutableMapOf() | ||
|
||
override suspend fun submit(request: DownloadManager.Request): DownloadManager.RequestId { | ||
val requestId = DownloadManager.RequestId(UUID.randomUUID().toString()) | ||
jobs[requestId] = coroutineScope.launch { doRequest(request, requestId) } | ||
return requestId | ||
} | ||
|
||
private suspend fun doRequest(request: DownloadManager.Request, id: DownloadManager.RequestId) { | ||
val response = httpClient.fetch( | ||
HttpRequest( | ||
url = request.url.toString(), | ||
headers = request.headers.mapValues { it.value.joinToString(",") } | ||
) | ||
) | ||
|
||
val dottedExtension = request.url.extension | ||
?.let { ".$it" } | ||
.orEmpty() | ||
|
||
when (response) { | ||
is Try.Success -> { | ||
withContext(Dispatchers.IO) { | ||
try { | ||
val dest = File.createTempFile( | ||
UUID.randomUUID().toString(), | ||
dottedExtension | ||
) | ||
dest.writeBytes(response.value.body) | ||
} catch (e: Exception) { | ||
val error = DownloadManager.Error.FileError(ThrowableError(e)) | ||
listener.onDownloadFailed(id, error) | ||
} | ||
} | ||
} | ||
is Try.Failure -> { | ||
val error = mapError(response.value) | ||
listener.onDownloadFailed(id, error) | ||
} | ||
} | ||
} | ||
|
||
private fun mapError(httpException: HttpException): DownloadManager.Error { | ||
val httpError = ThrowableError(httpException) | ||
return when (httpException.kind) { | ||
HttpException.Kind.MalformedRequest -> | ||
DownloadManager.Error.Unknown(httpError) | ||
HttpException.Kind.MalformedResponse -> | ||
DownloadManager.Error.HttpData(httpError) | ||
HttpException.Kind.Timeout -> | ||
DownloadManager.Error.Unreachable(httpError) | ||
HttpException.Kind.BadRequest -> | ||
DownloadManager.Error.Unknown(httpError) | ||
HttpException.Kind.Unauthorized -> | ||
DownloadManager.Error.Forbidden(httpError) | ||
HttpException.Kind.Forbidden -> | ||
DownloadManager.Error.Forbidden(httpError) | ||
HttpException.Kind.NotFound -> | ||
DownloadManager.Error.NotFound(httpError) | ||
HttpException.Kind.ClientError -> | ||
DownloadManager.Error.HttpData(httpError) | ||
HttpException.Kind.ServerError -> | ||
DownloadManager.Error.Server(httpError) | ||
HttpException.Kind.Offline -> | ||
DownloadManager.Error.Unreachable(httpError) | ||
HttpException.Kind.Cancelled -> | ||
DownloadManager.Error.Unknown(httpError) | ||
HttpException.Kind.Other -> | ||
DownloadManager.Error.Unknown(httpError) | ||
} | ||
} | ||
|
||
override suspend fun cancel(requestId: DownloadManager.RequestId) { | ||
jobs.remove(requestId)?.cancel() | ||
} | ||
|
||
override suspend fun close() { | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...java/org/readium/r2/shared/util/downloads/foreground/ForegroundDownloadManagerProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2023 Readium Foundation. All rights reserved. | ||
* Use of this source code is governed by the BSD-style license | ||
* available in the top-level LICENSE file of the project. | ||
*/ | ||
|
||
package org.readium.r2.shared.util.downloads.foreground | ||
|
||
import org.readium.r2.shared.util.downloads.DownloadManager | ||
import org.readium.r2.shared.util.downloads.DownloadManagerProvider | ||
import org.readium.r2.shared.util.http.HttpClient | ||
|
||
public class ForegroundDownloadManagerProvider( | ||
private val httpClient: HttpClient | ||
) : DownloadManagerProvider { | ||
|
||
override fun createDownloadManager( | ||
listener: DownloadManager.Listener, | ||
name: String | ||
): DownloadManager { | ||
return ForegroundDownloadManager(httpClient, listener) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters