add basic updater implementation in gigawear
This commit is contained in:
@@ -21,6 +21,7 @@ dependencies {
|
||||
implementation(project(":libs:base"))
|
||||
implementation(project(":libs:http-client"))
|
||||
implementation(project(":feature:updater:updater-common"))
|
||||
implementation(project(":feature:updater:updater-backend"))
|
||||
implementation(project(":feature:chat:chat-common"))
|
||||
implementation(project(":feature:chat:chat-backend"))
|
||||
|
||||
|
||||
-75
@@ -1,75 +0,0 @@
|
||||
package ru.shadowsparky.vbox.backend.data
|
||||
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.util.cio.readChannel
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.koin.core.annotation.Factory
|
||||
import ru.shadowsparky.backend.data.EnvFetcher
|
||||
import ru.shadowsparky.domain.DispatcherProvider
|
||||
import ru.shadowsparky.http.domain.HttpException
|
||||
import ru.shadowsparky.updater.domain.UpdateFetcher
|
||||
import ru.shadowsparky.updater.domain.UpdateInfo
|
||||
import java.io.File
|
||||
|
||||
@Factory
|
||||
class BackendUpdateFetcherFactory(
|
||||
private val envFetcher: EnvFetcher,
|
||||
private val json: Json,
|
||||
private val dispatcherProvider: DispatcherProvider
|
||||
) {
|
||||
fun create(userId: Long): BackendUpdateFetcher {
|
||||
return BackendUpdateFetcher(envFetcher, json, dispatcherProvider, userId)
|
||||
}
|
||||
}
|
||||
|
||||
class BackendUpdateFetcher(
|
||||
private val envFetcher: EnvFetcher,
|
||||
private val json: Json,
|
||||
private val dispatcherProvider: DispatcherProvider,
|
||||
private val userId: Long
|
||||
) : UpdateFetcher {
|
||||
private val fetchMutex = Mutex()
|
||||
private val updateFileMutex = Mutex()
|
||||
|
||||
override suspend fun fetch(versionCode: Long): UpdateInfo {
|
||||
updateInfo?.let { return it }
|
||||
fetchMutex.withLock {
|
||||
updateInfo?.let { return it }
|
||||
return fetchInternal().apply { updateInfo = this }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun fetchInternal(): UpdateInfo = withContext(dispatcherProvider.io) {
|
||||
val configPath = envFetcher.get("UPDATE_CONFIG")
|
||||
if (configPath.isBlank()) {
|
||||
UpdateInfo()
|
||||
} else {
|
||||
json.decodeFromString<UpdateInfo>(File(configPath).readText())
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateFile(): File {
|
||||
updateFile?.let { return it }
|
||||
updateFileMutex.withLock {
|
||||
updateFile?.let { return it }
|
||||
return updateFileInternal().apply { updateFile = this }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateFileInternal(): File = withContext(dispatcherProvider.io) {
|
||||
val updateFilePath = envFetcher.get("UPDATE_FILE")
|
||||
if (updateFilePath.isBlank()) throw HttpException(HttpStatusCode.NotFound.value, "Update not found")
|
||||
val file = File(updateFilePath)
|
||||
file.readChannel()
|
||||
if (!file.exists()) throw HttpException(HttpStatusCode.NotFound.value, "Update not exists")
|
||||
file
|
||||
}
|
||||
|
||||
private companion object {
|
||||
var updateInfo: UpdateInfo? = null
|
||||
var updateFile: File? = null
|
||||
}
|
||||
}
|
||||
+3
-16
@@ -1,23 +1,10 @@
|
||||
package ru.shadowsparky.vbox.backend.presentation.routing
|
||||
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.response.respondFile
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.util.getOrFail
|
||||
import ru.shadowsparky.updater.domain.UpdateFetcher
|
||||
import ru.shadowsparky.vbox.backend.data.BackendUpdateFetcherFactory
|
||||
import ru.shadowsparky.vbox.backend.presentation.obtainUserId
|
||||
import ru.shadowsparky.updater.backend.data.BackendUpdateFetcherFactory
|
||||
import ru.shadowsparky.updater.backend.presentation.setupUpdates
|
||||
import ru.shadowsparky.vbox.shared.domain.DYNAMIC_PREFIX
|
||||
|
||||
fun Route.setupUpdates(updateFetcherFactory: BackendUpdateFetcherFactory) {
|
||||
get("$DYNAMIC_PREFIX/checkUpdates") {
|
||||
val versionCode = call.parameters.getOrFail(UpdateFetcher.ARG_VERSION_CODE).toLong()
|
||||
val updateFetcher = updateFetcherFactory.create(call.obtainUserId())
|
||||
call.respond(updateFetcher.fetch(versionCode))
|
||||
}
|
||||
get("$DYNAMIC_PREFIX/getApk") {
|
||||
val updateFetcher = updateFetcherFactory.create(call.obtainUserId())
|
||||
call.respondFile(updateFetcher.updateFile())
|
||||
}
|
||||
setupUpdates(updateFetcherFactory, "$DYNAMIC_PREFIX/")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user