diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/name/blackcap/exifwasher/Test2.kt	Wed Apr 01 14:23:54 2020 -0700
@@ -0,0 +1,56 @@
+/*
+ * A basic test of the library: try to use it to print out the EXIF
+ * data.
+ */
+package name.blackcap.exifwasher
+
+import java.io.File
+import java.io.FileInputStream
+import java.io.FileOutputStream
+import name.blackcap.exifwasher.exiv2.*
+
+/* entry point */
+fun main(args: Array<String>) {
+    /* must have a file name */
+    if (args.size != 1) {
+        System.err.println("expecting a file name")
+        System.exit(1)
+    }
+
+    /* copy the file; we don't want to scribble on the original */
+    val (name, ext) = splitext(args[0])
+    val newName = "${name}_washed${ext}"
+    FileInputStream(args[0]).use { source ->
+        FileOutputStream(newName).use { target ->
+            source.copyTo(target)
+        }
+    }
+
+    /* load the whitelist and image data */
+    val white = Whitelist.parse(PROPERTIES.getProperty("whitelist"))
+    val image = Image(newName)
+
+    /* do the washing */
+    val meta = image.metadata
+    val keys = meta.keys
+    val keysin = keys.size
+    var deleted = 0
+    keys.forEach {
+        if (!white.contains(it)) {
+            meta.erase(it)
+            deleted += 1
+        }
+    }
+
+    /* save and summarize */
+    image.store()
+    println("${keysin} in - ${deleted} deleted = ${keysin - deleted} out")
+}
+
+fun splitext(s: String): Pair<String, String> {
+    val pos = s.lastIndexOf('.')
+    if (pos == -1) {
+        return Pair(s, "")
+    }
+    return Pair(s.substring(0, pos), s.substring(pos))
+}