sources API done

This commit is contained in:
Aria Moradi 2020-12-25 06:15:52 +03:30
parent 2f2b0fae7f
commit 7baca45d52
2 changed files with 55 additions and 3 deletions

View File

@ -146,7 +146,7 @@ class Main {
fun downloadApk(apkName: String): Int { fun downloadApk(apkName: String): Int {
val extensionRecord = getExtensionList(true).first { it.apkName == apkName } val extensionRecord = getExtensionList(true).first { it.apkName == apkName }
val fileNameWithoutType = apkName.substringBefore(".apk") val fileNameWithoutType = apkName.substringBefore(".apk")
val dirPathWithoutType = "${Config.extensionsRoot}/$apkName" val dirPathWithoutType = "${Config.extensionsRoot}/$fileNameWithoutType"
// check if we don't have the dex file already downloaded // check if we don't have the dex file already downloaded
val dexPath = "${Config.extensionsRoot}/$fileNameWithoutType.jar" val dexPath = "${Config.extensionsRoot}/$fileNameWithoutType.jar"
@ -168,7 +168,9 @@ class Main {
// dex -> jar // dex -> jar
Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force") Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force")
// clean up
File(apkFilePath).delete() File(apkFilePath).delete()
File(dexFilePath).delete()
// update sources of the extension // update sources of the extension
val child = URLClassLoader(arrayOf<URL>(URL("file:$jarFilePath")), this::class.java.classLoader) val child = URLClassLoader(arrayOf<URL>(URL("file:$jarFilePath")), this::class.java.classLoader)
@ -237,6 +239,44 @@ class Main {
} }
} }
fun getHttpSource(sourceId: Long): HttpSource {
return transaction {
val sourceRecord = SourceEntity.find { SourcesTable.sourceId eq sourceId }.first()
val extensionId = sourceRecord.extension.id.value
val extensionRecord = ExtensionEntity.get(extensionId)
val apkName = extensionRecord.apkName
val className = extensionRecord.classFQName
val jarName = apkName.substringBefore(".apk") + ".jar"
val jarPath = "${Config.extensionsRoot}/$jarName"
println(jarPath)
val child = URLClassLoader(arrayOf<URL>(URL("file:$jarPath")), this::class.java.classLoader)
val classToLoad = Class.forName(className, true, child)
val instance = classToLoad.newInstance()
if (sourceRecord.partOfFactorySource) {
return@transaction (instance as SourceFactory).createSources()[sourceRecord.positionInFactorySource!!] as HttpSource
} else {
return@transaction instance as HttpSource
}
}
}
fun getSourceList(): List<SourceDataClass> {
return transaction {
return@transaction SourcesTable.selectAll().map {
SourceDataClass(
it[SourcesTable.sourceId],
it[SourcesTable.name],
it[SourcesTable.lang],
ExtensionsTable.select { ExtensionsTable.id eq it[SourcesTable.extension] }.first()[ExtensionsTable.iconUrl],
getHttpSource(it[SourcesTable.sourceId]).supportsLatest
)
}
}
}
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
@ -264,6 +304,10 @@ class Main {
downloadApk(apkName) downloadApk(apkName)
) )
} }
app.get("/api/v1/sources/") { ctx ->
ctx.json(getSourceList())
}
} }
} }

View File

@ -9,13 +9,21 @@ import org.jetbrains.exposed.sql.Table
object SourcesTable : IntIdTable() { object SourcesTable : IntIdTable() {
val sourceId = long("source_id") val sourceId = long("source_id")
val name= varchar("name", 128) val name = varchar("name", 128)
val lang= varchar("lang", 5) val lang = varchar("lang", 5)
val extension = reference("extension", ExtensionsTable) val extension = reference("extension", ExtensionsTable)
val partOfFactorySource = bool("part_of_factory_source").default(false) val partOfFactorySource = bool("part_of_factory_source").default(false)
val positionInFactorySource = integer("position_in_factory_source").nullable() val positionInFactorySource = integer("position_in_factory_source").nullable()
} }
data class SourceDataClass(
val id: Long,
val name: String,
val lang: String,
val iconUrl: String,
val supportsLatest: Boolean
)
class SourceEntity(id: EntityID<Int>) : IntEntity(id) { class SourceEntity(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<SourceEntity>(SourcesTable) companion object : IntEntityClass<SourceEntity>(SourcesTable)