view src/name/blackcap/exifwasher/Test2.kt @ 26:985d5b82a882

Make needed dirs.
author davidb
date Thu, 16 Apr 2020 19:50:00 -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))
}