mirror of
https://github.com/Suwayomi/TachideskJUI.git
synced 2025-12-10 06:42:05 +01:00
Add install extension file from filesystem
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package ca.gosyer.jui.domain.extension.interactor
|
||||
|
||||
import ca.gosyer.jui.domain.extension.service.ExtensionRepository
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import me.tatarka.inject.annotations.Inject
|
||||
import okio.Path
|
||||
import org.lighthousegames.logging.logging
|
||||
|
||||
class InstallExtensionFile @Inject constructor(private val extensionRepository: ExtensionRepository) {
|
||||
|
||||
suspend fun await(path: Path) = asFlow(path)
|
||||
.catch { log.warn(it) { "Failed to install extension from $path" } }
|
||||
.collect()
|
||||
|
||||
fun asFlow(path: Path) = extensionRepository.installExtension(ExtensionRepository.buildExtensionFormData(path))
|
||||
|
||||
companion object {
|
||||
private val log = logging()
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,36 @@
|
||||
|
||||
package ca.gosyer.jui.domain.extension.service
|
||||
|
||||
import ca.gosyer.jui.core.io.SYSTEM
|
||||
import ca.gosyer.jui.domain.extension.model.Extension
|
||||
import de.jensklingenberg.ktorfit.http.GET
|
||||
import de.jensklingenberg.ktorfit.http.Multipart
|
||||
import de.jensklingenberg.ktorfit.http.POST
|
||||
import de.jensklingenberg.ktorfit.http.Part
|
||||
import de.jensklingenberg.ktorfit.http.Path
|
||||
import de.jensklingenberg.ktorfit.http.ReqBuilder
|
||||
import io.ktor.client.request.HttpRequestBuilder
|
||||
import io.ktor.client.request.forms.formData
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.Headers
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.content.PartData
|
||||
import io.ktor.utils.io.ByteReadChannel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import okio.FileSystem
|
||||
import okio.buffer
|
||||
|
||||
interface ExtensionRepository {
|
||||
@GET("api/v1/extension/list")
|
||||
fun getExtensionList(): Flow<List<Extension>>
|
||||
|
||||
@Multipart
|
||||
@POST("api/v1/extension/install")
|
||||
fun installExtension(
|
||||
@Part("") formData: List<PartData>,
|
||||
): Flow<HttpResponse>
|
||||
|
||||
@GET("api/v1/extension/install/{pkgName}")
|
||||
fun installExtension(
|
||||
@Path("pkgName") pkgName: String
|
||||
@@ -39,4 +56,17 @@ interface ExtensionRepository {
|
||||
@Path("apkName") apkName: String,
|
||||
@ReqBuilder block: HttpRequestBuilder.() -> Unit
|
||||
): Flow<ByteReadChannel>
|
||||
|
||||
companion object {
|
||||
fun buildExtensionFormData(file: okio.Path) = formData {
|
||||
append(
|
||||
"file",
|
||||
FileSystem.SYSTEM.source(file).buffer().readByteArray(),
|
||||
Headers.build {
|
||||
append(HttpHeaders.ContentType, ContentType.MultiPart.FormData.toString())
|
||||
append(HttpHeaders.ContentDisposition, "filename=file")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user