mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-12-25 14:25:35 +00:00
Rewritten Settings.
This commit is contained in:
parent
e5da546386
commit
5c466131d3
@ -352,9 +352,9 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
if (tl_peer_channel == null) continue
|
||||
|
||||
var download = true
|
||||
if (settings.whitelist_channels != null) {
|
||||
if (settings.whitelist_channels.isNotEmpty()) {
|
||||
download = settings.whitelist_channels.contains(tl_channel.getId().toString())
|
||||
} else if (settings.blacklist_channels != null) {
|
||||
} else if (settings.blacklist_channels.isNotEmpty()) {
|
||||
download = !settings.blacklist_channels.contains(tl_channel.getId().toString())
|
||||
}
|
||||
val channel = Channel(id=tl_channel.getId(), access_hash=tl_channel.getAccessHash(), title=tl_channel.getTitle(), obj=tl_peer_channel, download=download)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.fabianonline.telegram_backup
|
||||
|
||||
import java.io.File
|
||||
import java.util.LinkedList
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class Settings(val file_base: String, val database: Database, val cli_settings: CommandLineOptions) {
|
||||
@ -16,27 +17,16 @@ class Settings(val file_base: String, val database: Database, val cli_settings:
|
||||
copy_sample_ini("config.sample.ini")
|
||||
}
|
||||
// Merging CLI and INI settings
|
||||
val gmaps_key = get_setting_string("gmaps_key", default=Config.SECRET_GMAPS)
|
||||
val pagination = get_setting_boolean("pagination", default=true)
|
||||
val pagination_size = get_setting_int("pagination_size", default=Config.DEFAULT_PAGINATION)
|
||||
val download_media = get_setting_boolean("download_media", default=true)
|
||||
val download_channels = get_setting_boolean("download_channels", default=false)
|
||||
val download_supergroups = get_setting_boolean("download_supergroups", default=false)
|
||||
val whitelist_channels = get_setting_list("whitelist_channels")
|
||||
val blacklist_channels = get_setting_list("blacklist_channels")
|
||||
|
||||
|
||||
private fun get_setting_string(name: String, default: String): String {
|
||||
return ini_settings[name]?.last() ?: cli_settings.values[name] ?: default
|
||||
}
|
||||
|
||||
private fun get_setting_int(name: String, default: Int): Int {
|
||||
return ini_settings[name]?.last()?.toInt() ?: cli_settings.values[name]?.toInt() ?: default
|
||||
}
|
||||
|
||||
private fun get_setting_boolean(name: String, default: Boolean): Boolean {
|
||||
return ini_settings[name]?.last()?.toBoolean() ?: cli_settings.values[name]?.toBoolean() ?: default
|
||||
}
|
||||
|
||||
val sf = SettingsFactory(ini_settings, cli_settings)
|
||||
val gmaps_key = sf.getString("gmaps_key", default=Config.SECRET_GMAPS)
|
||||
val pagination = sf.getBoolean("pagination", default=true)
|
||||
val pagination_size = sf.getInt("pagination_size", default=Config.DEFAULT_PAGINATION)
|
||||
val download_media = sf.getBoolean("download_media", default=true)
|
||||
val download_channels = sf.getBoolean("download_channels", default=false)
|
||||
val download_supergroups = sf.getBoolean("download_supergroups", default=false)
|
||||
val whitelist_channels = sf.getStringList("whitelist_channels", default=LinkedList<String>())
|
||||
val blacklist_channels = sf.getStringList("blacklist_channels", default=LinkedList<String>())
|
||||
|
||||
private fun get_setting_list(name: String): List<String>? {
|
||||
return ini_settings[name]
|
||||
@ -79,57 +69,56 @@ class Settings(val file_base: String, val database: Database, val cli_settings:
|
||||
File(filename).outputStream().use { stream.copyTo(it) }
|
||||
stream.close()
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
class DbSettings(val database: Database) {
|
||||
private fun fetchValue(name: String): String? = database.fetchSetting(name)
|
||||
private fun saveValue(name: String, value: String?) = database.saveSetting(name, value)
|
||||
|
||||
class SettingsFactory(val ini: Map<String, List<String>>, val cli: CommandLineOptions) {
|
||||
fun getInt(name: String, default: Int) = getSetting(name, listOf(default.toString())).get().toInt()
|
||||
fun getBoolean(name: String, default: Boolean) = getSetting(name, listOf(default.toString())).get().toBoolean()
|
||||
fun getString(name: String, default: String) = getSetting(name, listOf(default)).get()
|
||||
fun getStringList(name: String, default: List<String>) = getSetting(name, default).getList()
|
||||
|
||||
var pts: String?
|
||||
get() = fetchValue("pts")
|
||||
set(x: String?) = saveValue("pts", x)
|
||||
fun getSetting(name: String, default: List<String>) = Setting(ini, cli, name, default)
|
||||
}
|
||||
|
||||
|
||||
package de.fabianonline.telegram_backup
|
||||
|
||||
|
||||
|
||||
class Settings(val file_base: String) {
|
||||
val logger = LoggerFactory.getLogger(Settings::class.java)
|
||||
var settings = mutableMapOf<String, MutableList<String>>()
|
||||
class Setting(val ini: Map<String, List<String>>, val cli: CommandLineOptions, val name: String, val default: List<String>) {
|
||||
val values: List<String>
|
||||
val source: SettingSource
|
||||
val logger = LoggerFactory.getLogger(Setting::class.java)
|
||||
|
||||
init {
|
||||
loadIni(file_base + "config.ini")
|
||||
copySampleIni(file_base + "config.sample.ini")
|
||||
if (getIni(name) != null) {
|
||||
values = getIni(name)!!
|
||||
source = SettingSource.INI
|
||||
} else if (getCli(name) != null) {
|
||||
values = listOf(getCli(name)!!)
|
||||
source = SettingSource.CLI
|
||||
} else {
|
||||
values = default
|
||||
source = SettingSource.DEFAULT
|
||||
}
|
||||
|
||||
logger.debug("Setting ${name} loaded. Source: ${source}. Value: ${values.toString().anonymize()}")
|
||||
|
||||
all_settings.add(this)
|
||||
}
|
||||
fun get(): String = values.last()
|
||||
fun getList(): List<String> = values
|
||||
|
||||
fun getIni(name: String): List<String>? {
|
||||
return ini[name]
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fun println() = println(settings)
|
||||
|
||||
fun getString(key: String, default: String? = null): String? = settings.get(key)?.last() ?: default
|
||||
fun getStringList(key: String): List<String>? = settings.get(key)
|
||||
fun getInt(key: String, default: Int? = null): Int? = try { settings.get(key)?.last()?.toInt() } catch (e: NumberFormatException) { null } ?: default
|
||||
fun getBoolean(key: String, default: Boolean = false): Boolean {
|
||||
val value = settings.get(key)?.last()
|
||||
if (value==null) return default
|
||||
return value=="true"
|
||||
fun getCli(name: String): String? {
|
||||
return cli.values[name]
|
||||
}
|
||||
fun getArray(key: String): List<String> = settings.get(key) ?: listOf<String>()
|
||||
|
||||
val gmaps_key = getString("gmaps_key", default=Config.SECRET_GMAPS)!!
|
||||
val pagination = getBoolean("pagination", default=true)
|
||||
val pagination_size = getInt("pagination_size", default=Config.DEFAULT_PAGINATION)!!
|
||||
val download_media = getBoolean("download_media", default=true)
|
||||
val download_channels = getBoolean("download_channels", default=false)
|
||||
val download_supergroups = getBoolean("download_supergroups", default=false)
|
||||
val whitelist_channels = getStringList("whitelist_channels")
|
||||
val blacklist_channels = getStringList("blacklist_channels")
|
||||
companion object {
|
||||
val all_settings = LinkedList<Setting>()
|
||||
}
|
||||
}
|
||||
|
||||
enum class SettingSource {
|
||||
INI,
|
||||
CLI,
|
||||
DEFAULT
|
||||
}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user