diff --git a/server/src/main/kotlin/ir/armor/tachidesk/Main.kt b/server/src/main/kotlin/ir/armor/tachidesk/Main.kt index bc941a30..64925fea 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/Main.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/Main.kt @@ -146,7 +146,7 @@ class Main { fun downloadApk(apkName: String): Int { val extensionRecord = getExtensionList(true).first { it.apkName == apkName } 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 val dexPath = "${Config.extensionsRoot}/$fileNameWithoutType.jar" @@ -168,7 +168,9 @@ class Main { // dex -> jar Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force") + // clean up File(apkFilePath).delete() + File(dexFilePath).delete() // update sources of the extension val child = URLClassLoader(arrayOf(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("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 { + 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 fun main(args: Array) { @@ -264,6 +304,10 @@ class Main { downloadApk(apkName) ) } + app.get("/api/v1/sources/") { ctx -> + ctx.json(getSourceList()) + } + } } diff --git a/server/src/main/kotlin/ir/armor/tachidesk/database/model/SourcesTable.kt b/server/src/main/kotlin/ir/armor/tachidesk/database/model/SourcesTable.kt index 03fdf13a..4bceec75 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/database/model/SourcesTable.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/database/model/SourcesTable.kt @@ -9,13 +9,21 @@ import org.jetbrains.exposed.sql.Table object SourcesTable : IntIdTable() { val sourceId = long("source_id") - val name= varchar("name", 128) - val lang= varchar("lang", 5) + val name = varchar("name", 128) + val lang = varchar("lang", 5) val extension = reference("extension", ExtensionsTable) val partOfFactorySource = bool("part_of_factory_source").default(false) 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) : IntEntity(id) { companion object : IntEntityClass(SourcesTable)