comparison src/name/blackcap/exifwasher/Test2.kt @ 2:efd9fe2d70d7

Rationalize exceptions, code whitelist, add command-line tool.
author David Barts <n5jrn@me.com>
date Wed, 01 Apr 2020 14:23:54 -0700
parents
children
comparison
equal deleted inserted replaced
1:42277ce58ace 2:efd9fe2d70d7
1 /*
2 * A basic test of the library: try to use it to print out the EXIF
3 * data.
4 */
5 package name.blackcap.exifwasher
6
7 import java.io.File
8 import java.io.FileInputStream
9 import java.io.FileOutputStream
10 import name.blackcap.exifwasher.exiv2.*
11
12 /* entry point */
13 fun main(args: Array<String>) {
14 /* must have a file name */
15 if (args.size != 1) {
16 System.err.println("expecting a file name")
17 System.exit(1)
18 }
19
20 /* copy the file; we don't want to scribble on the original */
21 val (name, ext) = splitext(args[0])
22 val newName = "${name}_washed${ext}"
23 FileInputStream(args[0]).use { source ->
24 FileOutputStream(newName).use { target ->
25 source.copyTo(target)
26 }
27 }
28
29 /* load the whitelist and image data */
30 val white = Whitelist.parse(PROPERTIES.getProperty("whitelist"))
31 val image = Image(newName)
32
33 /* do the washing */
34 val meta = image.metadata
35 val keys = meta.keys
36 val keysin = keys.size
37 var deleted = 0
38 keys.forEach {
39 if (!white.contains(it)) {
40 meta.erase(it)
41 deleted += 1
42 }
43 }
44
45 /* save and summarize */
46 image.store()
47 println("${keysin} in - ${deleted} deleted = ${keysin - deleted} out")
48 }
49
50 fun splitext(s: String): Pair<String, String> {
51 val pos = s.lastIndexOf('.')
52 if (pos == -1) {
53 return Pair(s, "")
54 }
55 return Pair(s.substring(0, pos), s.substring(pos))
56 }