hyperdrive-jpeg-index

1.1.0 • Public • Published

hyperdrive-jpeg-index

index jpeg metadata and thumbnails for a hyperdrive archive

example

This example will store time and orientation values from EXIF headers and width and height values from SOF headers.

var hyperdrive = require('hyperdrive')
var fs = require('fs')
var path = require('path')
var sub = require('subleveldown')
var to = require('to2')
var jpeg = require('hyperdrive-jpeg-index')

var level = require('level')
var db = level('/tmp/photos.db')

var namedArchives = require('hyperdrive-named-archives')
var named = namedArchives({
  drive: hyperdrive(sub(db, 'drive')),
  db: sub(db, 'archives')
})
var archive = named.createArchive('photos')
var photos = jpeg({
  archive: archive,
  db: sub(db, 'jpeg'),
  properties: {
    time: ['EXIF','exif','DateTimeOriginal',isoString],
    orientation: ['EXIF','image','Orientation'],
    width: ['SOF','width'],
    height: ['SOF','height']
  }
})
function isoString (date) { return date.toISOString() }

if (process.argv[2] === 'add') {
  var file = process.argv[3]
  fs.createReadStream(file)
    .pipe(archive.createFileWriteStream(path.basename(file)))
} else if (process.argv[2] === 'list') {
  photos.list().pipe(to.obj(function (img, enc, next) {
    console.log(img)
  }))
}

output:

$ node photos.js add cactus.jpg
$ node photos.js list
{ name: 'cactus.jpg',
  properties: 
   { time: '2015-06-19T16:40:52.000Z',
     orientation: 1,
     width: 600,
     height: 800 } }

api

var jpeg = require('hyperdrive-jpeg-index')

var photos = jpeg(opts)

Create a photos instance from:

  • opts.archive - a hyperdrive archive
  • opts.db - a leveldb instance to save index data
  • opts.properties - map of property keys to marker key paths
  • opts.map(entry, stream, next) - intercept the entry and binary jpeg stream

Each marker key path given in opts.properties is an array that describes the nested location into a marker object to produce a value. Key path items can be a string or a function (node) {} that take an object as input and should return the next node value to use in the traversal.

With opts.map you can calculate secondary indexes or set additional properties. Call next(err, props) with any additional properties to set on the record. You can use opts.map to do things like generate resized versions of the image to store elsewhere.

var stream = photos.list()

Return a readable objectMode stream of records. Each record has:

  • row.name - file name from hyperdrive
  • row.properties - the values generated from the marker key paths

photos.get(name, cb)

Fetch the property values generated for a file name.

install

npm install hyperdrive-jpeg-index

license

BSD

Package Sidebar

Install

npm i hyperdrive-jpeg-index

Weekly Downloads

3

Version

1.1.0

License

BSD

Last publish

Collaborators

  • nopersonsmodules