Mercurial > cgi-bin > hgweb.cgi > ImagePrep
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e0efe7848130 |
---|---|
1 /* | |
2 * For dealing with files. | |
3 */ | |
4 package name.blackcap.imageprep | |
5 | |
6 import java.io.BufferedReader | |
7 import java.io.File | |
8 import java.io.FileInputStream | |
9 import java.io.InputStreamReader | |
10 import java.util.Properties | |
11 import java.util.logging.FileHandler | |
12 import java.util.logging.Level | |
13 import java.util.logging.Logger | |
14 import java.util.logging.SimpleFormatter | |
15 | |
16 /* OS Type */ | |
17 | |
18 enum class OS { | |
19 MAC, UNIX, WINDOWS, OTHER; | |
20 companion object { | |
21 private val rawType = System.getProperty("os.name")?.toLowerCase() | |
22 val type = if (rawType == null) { | |
23 OTHER | |
24 } else if (rawType.contains("win")) { | |
25 WINDOWS | |
26 } else if (rawType.contains("mac")) { | |
27 MAC | |
28 } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) { | |
29 UNIX | |
30 } else { | |
31 OTHER | |
32 } | |
33 } | |
34 } | |
35 | |
36 /* joins path name components to java.io.File */ | |
37 | |
38 fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File) | |
39 | |
40 /* file names */ | |
41 | |
42 private val SHORTNAME = "imageprep" | |
43 private val LONGNAME = "name.blackcap." + SHORTNAME | |
44 private val HOME = System.getenv("HOME") | |
45 val PF_DIR = when (OS.type) { | |
46 OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) | |
47 OS.WINDOWS -> joinPath(System.getenv("APPDATA"), LONGNAME) | |
48 else -> joinPath(HOME, "." + SHORTNAME) | |
49 } | |
50 val LF_DIR = when (OS.type) { | |
51 OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) | |
52 OS.WINDOWS -> joinPath(System.getenv("LOCALAPPDATA"), LONGNAME) | |
53 else -> joinPath(HOME, "." + SHORTNAME) | |
54 } | |
55 val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties") | |
56 val LOG_FILE = File(LF_DIR, SHORTNAME + ".log") | |
57 | |
58 /* make some needed directories */ | |
59 | |
60 private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() } | |
61 | |
62 /* make some usable objects */ | |
63 | |
64 val CHARSET = "UTF-8" | |
65 | |
66 private val DPNAME = "default.properties" | |
67 val DPROPERTIES = Properties().apply { | |
68 val rawStream = OS::class.java.getResourceAsStream(DPNAME) | |
69 BufferedReader(InputStreamReader(rawStream, CHARSET)).use { | |
70 load(it) | |
71 } | |
72 } | |
73 | |
74 val PROPERTIES = Properties(DPROPERTIES).apply { | |
75 PF_DIR.makeIfNeeded() | |
76 if (PROP_FILE.exists()) { | |
77 BufferedReader(InputStreamReader(FileInputStream(PROP_FILE), CHARSET)).use { | |
78 load(it) | |
79 } | |
80 } else { | |
81 FileOutputStream(PROP_FILE).use { output -> | |
82 OS::class.java.getResourceAsStream(DPNAME).use { input -> | |
83 input.copyTo(output) | |
84 } | |
85 } | |
86 } | |
87 } | |
88 | |
89 val LOGGER = run { | |
90 System.setProperty("java.util.logging.SimpleFormatter.format", | |
91 "%1\$tFT%1\$tT%1\$tz %2\$s%n%4\$s: %5\$s%6\$s%n") | |
92 LF_DIR.makeIfNeeded() | |
93 Logger.getLogger(LONGNAME).apply { | |
94 addHandler(FileHandler(LOG_FILE.toString()).apply { | |
95 formatter = SimpleFormatter() }) | |
96 level = Level.CONFIG | |
97 useParentHandlers = false | |
98 } | |
99 } |