view src/name/blackcap/exifwasher/Test2.kt @ 13:a59d84674fb0

Make it seamlessly work on IPTC and XMP metadata, too.
author David Barts <davidb@stashtea.com>
date Sat, 11 Apr 2020 09:14:31 -0700
parents efd9fe2d70d7
children
line wrap: on
line source

/*
 * 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))
}