Mercurial > cgi-bin > hgweb.cgi > ImagePrep
diff src/name/blackcap/imageprep/Files.kt @ 0:e0efe7848130
Initial commit. Untested!
author | David Barts <davidb@stashtea.com> |
---|---|
date | Thu, 16 Jul 2020 19:57:23 -0700 |
parents | |
children | 0bded24f746e |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/name/blackcap/imageprep/Files.kt Thu Jul 16 19:57:23 2020 -0700 @@ -0,0 +1,99 @@ +/* + * For dealing with files. + */ +package name.blackcap.imageprep + +import java.io.BufferedReader +import java.io.File +import java.io.FileInputStream +import java.io.InputStreamReader +import java.util.Properties +import java.util.logging.FileHandler +import java.util.logging.Level +import java.util.logging.Logger +import java.util.logging.SimpleFormatter + +/* OS Type */ + +enum class OS { + MAC, UNIX, WINDOWS, OTHER; + companion object { + private val rawType = System.getProperty("os.name")?.toLowerCase() + val type = if (rawType == null) { + OTHER + } else if (rawType.contains("win")) { + WINDOWS + } else if (rawType.contains("mac")) { + MAC + } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) { + UNIX + } else { + OTHER + } + } +} + +/* joins path name components to java.io.File */ + +fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File) + +/* file names */ + +private val SHORTNAME = "imageprep" +private val LONGNAME = "name.blackcap." + SHORTNAME +private val HOME = System.getenv("HOME") +val PF_DIR = when (OS.type) { + OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) + OS.WINDOWS -> joinPath(System.getenv("APPDATA"), LONGNAME) + else -> joinPath(HOME, "." + SHORTNAME) +} +val LF_DIR = when (OS.type) { + OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) + OS.WINDOWS -> joinPath(System.getenv("LOCALAPPDATA"), LONGNAME) + else -> joinPath(HOME, "." + SHORTNAME) +} +val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties") +val LOG_FILE = File(LF_DIR, SHORTNAME + ".log") + +/* make some needed directories */ + +private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() } + +/* make some usable objects */ + +val CHARSET = "UTF-8" + +private val DPNAME = "default.properties" +val DPROPERTIES = Properties().apply { + val rawStream = OS::class.java.getResourceAsStream(DPNAME) + BufferedReader(InputStreamReader(rawStream, CHARSET)).use { + load(it) + } +} + +val PROPERTIES = Properties(DPROPERTIES).apply { + PF_DIR.makeIfNeeded() + if (PROP_FILE.exists()) { + BufferedReader(InputStreamReader(FileInputStream(PROP_FILE), CHARSET)).use { + load(it) + } + } else { + FileOutputStream(PROP_FILE).use { output -> + OS::class.java.getResourceAsStream(DPNAME).use { input -> + input.copyTo(output) + } + } + } +} + +val LOGGER = run { + System.setProperty("java.util.logging.SimpleFormatter.format", + "%1\$tFT%1\$tT%1\$tz %2\$s%n%4\$s: %5\$s%6\$s%n") + LF_DIR.makeIfNeeded() + Logger.getLogger(LONGNAME).apply { + addHandler(FileHandler(LOG_FILE.toString()).apply { + formatter = SimpleFormatter() }) + level = Level.CONFIG + useParentHandlers = false + } +}