-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
60 additions
and
160 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,19 @@ | ||
package li.songe.gkd.db | ||
|
||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.blankj.utilcode.util.LogUtils | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.withTimeout | ||
import li.songe.gkd.app | ||
import li.songe.gkd.appScope | ||
import li.songe.gkd.data.RawSubscription | ||
import li.songe.gkd.data.SubsItem | ||
import li.songe.gkd.util.DEFAULT_SUBS_UPDATE_URL | ||
import li.songe.gkd.util.client | ||
import li.songe.gkd.util.dbFolder | ||
import li.songe.gkd.util.launchTry | ||
import li.songe.gkd.util.updateSubscription | ||
import java.io.File | ||
|
||
object DbSet { | ||
private val appDb by lazy { | ||
Room.databaseBuilder( | ||
app, AppDb::class.java, File(dbFolder, "gkd.db").absolutePath | ||
).addCallback(createCallback()).fallbackToDestructiveMigration().build() | ||
).fallbackToDestructiveMigration().build() | ||
} | ||
val subsItemDao by lazy { appDb.subsItemDao() } | ||
val subsConfigDao by lazy { appDb.subsConfigDao() } | ||
val snapshotDao by lazy { appDb.snapshotDao() } | ||
val clickLogDao by lazy { appDb.clickLogDao() } | ||
val categoryConfigDao by lazy { appDb.categoryConfigDao() } | ||
|
||
private fun createCallback(): RoomDatabase.Callback { | ||
return object : RoomDatabase.Callback() { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
super.onCreate(db) | ||
appScope.launchTry(Dispatchers.IO) { | ||
delay(3000) | ||
if (subsItemDao.queryById(0).firstOrNull() != null) { | ||
return@launchTry | ||
} | ||
val defaultSubsItem = SubsItem( | ||
id = 0, | ||
order = 0, | ||
updateUrl = DEFAULT_SUBS_UPDATE_URL, | ||
mtime = System.currentTimeMillis() | ||
) | ||
subsItemDao.insert(defaultSubsItem) | ||
val newSubsRaw = try { | ||
withTimeout(3000) { | ||
RawSubscription.parse( | ||
client.get(defaultSubsItem.updateUrl!!).bodyAsText() | ||
) | ||
} | ||
} catch (e: Exception) { | ||
LogUtils.d("获取默认订阅失败") | ||
LogUtils.d(e) | ||
return@launchTry | ||
} | ||
updateSubscription(newSubsRaw) | ||
subsItemDao.update(defaultSubsItem.copy(mtime = System.currentTimeMillis())) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,48 @@ | ||
package li.songe.gkd.util | ||
|
||
import android.webkit.URLUtil | ||
import io.ktor.http.Url | ||
import li.songe.gkd.BuildConfig | ||
|
||
const val VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION" | ||
|
||
const val FILE_UPLOAD_URL = "https://u.gkd.li/" | ||
const val IMPORT_BASE_URL = "https://i.gkd.li/import/" | ||
|
||
const val DEFAULT_SUBS_UPDATE_URL = | ||
"https://registry.npmmirror.com/@gkd-kit/subscription/latest/files" | ||
const val UPDATE_URL = "https://registry.npmmirror.com/@gkd-kit/app/latest/files/index.json" | ||
|
||
const val SERVER_SCRIPT_URL = | ||
"https://registry-direct.npmmirror.com/@gkd-kit/config/latest/files/dist/server.js" | ||
|
||
const val REPOSITORY_URL = "https://github.com/gkd-kit/gkd" | ||
|
||
const val HOME_PAGE_URL = "https://gkd.li/" | ||
|
||
@Suppress("SENSELESS_COMPARISON") | ||
val GIT_COMMIT_URL = if (BuildConfig.GIT_COMMIT_ID != null) { | ||
"https://github.com/gkd-kit/gkd/commit/${BuildConfig.GIT_COMMIT_ID}" | ||
} else { | ||
null | ||
} | ||
|
||
val safeRemoteBaseUrls = arrayOf( | ||
private val safeRemoteBaseUrls = arrayOf( | ||
"https://registry.npmmirror.com/@gkd-kit/", | ||
"https://cdn.jsdelivr.net/npm/@gkd-kit/", | ||
"https://fastly.jsdelivr.net/npm/@gkd-kit/", | ||
"https://unpkg.com/@gkd-kit/", | ||
"https://github.com/gkd-kit/", | ||
"https://raw.githubusercontent.com/gkd-kit/" | ||
"https://raw.githubusercontent.com/gkd-kit/", | ||
HOME_PAGE_URL, | ||
) | ||
|
||
fun isSafeUrl(url: String): Boolean { | ||
if (!URLUtil.isHttpsUrl(url)) return false | ||
if (safeRemoteBaseUrls.any { u -> url.startsWith(u) }) { | ||
return true | ||
} | ||
return try { | ||
Url(url).host.endsWith(".gkd.li") | ||
} catch (e: Exception) { | ||
false | ||
} | ||
} |