mirror of https://github.com/thumbsup/thumbsup
Read metadata from Picasa.ini if present
parent
3e64d2ab38
commit
0b27d37881
@ -0,0 +1,70 @@
|
||||
/*
|
||||
--------------------------------------------------------------------------------
|
||||
Provides Picasa metadata based on <picasa.ini> files in the input folder
|
||||
--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
const ini = require('ini')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
class Picasa {
|
||||
constructor () {
|
||||
this.folders = {}
|
||||
}
|
||||
album (dir) {
|
||||
if (!this.folders[dir]) {
|
||||
this.folders[dir] = loadPicasa(dir)
|
||||
}
|
||||
// album metadata is stored in a section called [Picasa]
|
||||
const entry = this.folders[dir]
|
||||
return entry.Picasa || null
|
||||
}
|
||||
file (filepath) {
|
||||
const dir = path.dirname(filepath)
|
||||
if (!this.folders[dir]) {
|
||||
this.folders[dir] = loadPicasa(dir)
|
||||
}
|
||||
// file metadata is stored in a section called [FileName.ext]
|
||||
const entry = this.folders[dir]
|
||||
const filename = path.basename(filepath)
|
||||
const fileParts = filename.split('.')
|
||||
return getIniValue(entry, fileParts)
|
||||
}
|
||||
}
|
||||
|
||||
function loadPicasa (dirname) {
|
||||
const inipath = path.join(dirname, 'picasa.ini')
|
||||
const content = loadIfExists(inipath)
|
||||
if (!content) {
|
||||
// return an empty hash, as if the picasa.ini file existed but was empty
|
||||
return {}
|
||||
} else {
|
||||
return ini.parse(content)
|
||||
}
|
||||
}
|
||||
|
||||
function loadIfExists (filepath) {
|
||||
try {
|
||||
return fs.readFileSync(filepath, 'utf-8')
|
||||
} catch (ex) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// the INI parser creates nested objects when the key contains a '.'
|
||||
// this is a problem for sections like [IMG_0001.jpg]
|
||||
// this might get fixed with https://github.com/npm/ini/issues/60
|
||||
// but for now we have to recursively get the value
|
||||
function getIniValue (iniObject, keyParts) {
|
||||
const current = iniObject[keyParts[0]]
|
||||
if (!current) {
|
||||
return null
|
||||
} else if (keyParts.length === 1) {
|
||||
return current
|
||||
} else {
|
||||
return getIniValue(current, keyParts.slice(1))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Picasa
|
@ -0,0 +1,64 @@
|
||||
const should = require('should/as-function')
|
||||
const mock = require('mock-fs')
|
||||
const Picasa = require('../../src/input/picasa.js')
|
||||
|
||||
const PICASA_INI = `
|
||||
[Picasa]
|
||||
name=My holidays
|
||||
|
||||
[IMG_0001.jpg]
|
||||
star=yes
|
||||
caption=Nice sunset
|
||||
keywords=beach,sunset
|
||||
`
|
||||
|
||||
describe('Picasa', function () {
|
||||
afterEach(function () {
|
||||
mock.restore()
|
||||
})
|
||||
it('reads album metadata', function () {
|
||||
mock({
|
||||
'holidays/picasa.ini': PICASA_INI
|
||||
})
|
||||
const picasa = new Picasa()
|
||||
const meta = picasa.album('holidays')
|
||||
should(meta).eql({
|
||||
name: 'My holidays'
|
||||
})
|
||||
})
|
||||
it('returns <null> if there is no album metadata', function () {
|
||||
const picasa = new Picasa()
|
||||
const meta = picasa.album('holidays')
|
||||
should(meta).eql(null)
|
||||
})
|
||||
it('returns raw file metadata as read from the INI file', function () {
|
||||
mock({
|
||||
'holidays/picasa.ini': PICASA_INI
|
||||
})
|
||||
const picasa = new Picasa()
|
||||
const meta = picasa.file('holidays/IMG_0001.jpg')
|
||||
should(meta).eql({
|
||||
star: 'yes',
|
||||
caption: 'Nice sunset',
|
||||
keywords: 'beach,sunset'
|
||||
})
|
||||
})
|
||||
it('can read metadata of a file with several dots in the name', function () {
|
||||
mock({
|
||||
'holidays/picasa.ini': '[IMG.0001.small.jpg]\ncaption=dots'
|
||||
})
|
||||
const picasa = new Picasa()
|
||||
const meta = picasa.file('holidays/IMG.0001.small.jpg')
|
||||
should(meta).eql({
|
||||
caption: 'dots'
|
||||
})
|
||||
})
|
||||
it('returns <null> if a file has no metadata', function () {
|
||||
mock({
|
||||
'holidays/picasa.ini': PICASA_INI
|
||||
})
|
||||
const picasa = new Picasa()
|
||||
const meta = picasa.album('holidays/IMG_0002.jpg')
|
||||
should(meta).eql(null)
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue