/* * EXIFTemplate.java * * Brazil project web application toolkit, * export version: 2.3 * Copyright (c) 2008 Sun Microsystems, Inc. * * Sun Public License Notice * * The contents of this file are subject to the Sun Public License Version * 1.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is included as the file "license.terms", * and also available at http://www.sun.com/ * * The Original Code is from: * Brazil project web application toolkit release 2.3. * The Initial Developer of the Original Code is: suhler. * Portions created by suhler are Copyright (C) Sun Microsystems, Inc. * All Rights Reserved. * * Contributor(s): suhler. * * Version: 1.1 * Created by suhler on 08/08/14 * Last modified by suhler on 08/08/14 16:02:32 * * Version Histories: * * 1.2 70/01/01-00:00:02 (Codemgr) * SunPro Code Manager data about conflicts, renames, etc... * Name history : 1 0 sunlabs-external/EXIFTemplate.java * * 1.1 08/08/14-16:02:32 (suhler) * date and time created 08/08/14 16:02:32 by suhler * */ package sunlabs.brazil.exif; import com.drew.imaging.jpeg.JpegMetadataReader; import com.drew.imaging.jpeg.JpegProcessingException; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.MetadataException; import com.drew.metadata.Tag; import com.drew.metadata.exif.ExifDirectory; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.Properties; import sunlabs.brazil.server.FileHandler; import sunlabs.brazil.template.RewriteContext; import sunlabs.brazil.template.Template; import sunlabs.brazil.util.regexp.Regexp; /** * Template for extracting EXIF data out of JPEG image files. * This uses the EXIF library at: * {@link http://www.drewnoakes.com/code/exif/}. It has been tested * with version 2.3.1. *

* Attributes: *

*
file
* The name of the file to analyze and extract meta information for. * If "file" starts with "/" it is resolved relative to the document * root. Otherwise it is resolved relative to the directory implied * by the URL, unless overridden by "DirectoryName". *
(This is probably not the proper behavior). *
directoryName
* Specify the directory to find the "file" in. Defaults to the * directory implied by the URL, and is always relative to the document root. *
prepend
* All the meta properties extracted from the file are * placed in the request properties as: *
prepend.directory.property
* where "prepend" defaults to the template prefix, and "directory" and * "property" are extracted from the file. Errors (if any) are reported * as prepend.error *
thumb
* The name of a file to write the thumbnail to, if any. Must not already * exist. *
directoryThumb
* The directory for the thumbnail. See "directoryName" *
*/ public class EXIFTemplate extends Template { public void tag_exif(RewriteContext hr) { String name = hr.get("file"); String prepend = hr.get("prepend", hr.prefix); if (!prepend.endsWith(".")) { prepend += "."; } File file; hr.killToken(); debug(hr); if (name == null) { debug(hr,"Missing file attribute"); return; } /* find the file name */ file = findFile(hr, hr.get("directoryName"), name); Metadata meta; try { meta = JpegMetadataReader.readMetadata(file); } catch (JpegProcessingException e) { hr.request.props.put(prepend + "error", file + ": " + e.getMessage()); return; } Iterator dirs = meta.getDirectoryIterator(); while(dirs.hasNext()) { Directory dir = (Directory)dirs.next(); Iterator tags = dir.getTagIterator(); while(tags.hasNext()) { Tag tag = (Tag)tags.next(); String item = prepend + "error"; String value = "error"; try { name = prepend + tag.getDirectoryName() + "." + scrunch(tag.getTagName()); item = tag.getDescription(); } catch (MetadataException e) { value = e.getMessage(); } hr.request.props.put(name,item); } } // create thumbnail as required String thumb = hr.get("thumb"); if (thumb != null) { String dir = hr.get("directoryName"); file = findFile(hr, hr.get("directoryThumb",dir), thumb); if (file.exists()) { hr.request.props.put(prepend + "error", "thumbnail file " + file + " already exists"); // System.out.println("already exists: " + file); return; } ExifDirectory exifDirectory = (ExifDirectory) meta.getDirectory(ExifDirectory.class); try { exifDirectory.writeThumbnail(file.getCanonicalPath()); } catch (MetadataException e) { System.out.println("Oops: " + e.getMessage()); } catch (IOException e) { System.out.println("Oops: " + e.getMessage()); } } } /* Find the file */ File findFile(RewriteContext hr, String dir, String name) { File file = null; Properties props = hr.request.props; String root = props.getProperty(hr.prefix + "root", props.getProperty("root", ".")); if (name.startsWith("/")) { file = new File(root, name.substring(1)); } else { if (dir != null) { file = new File(dir, name); } else { dir = FileHandler.urlToPath(hr.request.url); file = new File(root,dir); if (!file.isDirectory()) { file = new File(file.getParent()); } file = new File(file, name); } } return file; } /* Java likes CamelCase, remove white space in tag names */ static final Regexp re = new Regexp(" +"); String scrunch(String s) { return re.subAll(s, ""); } }