2
0
mirror of https://github.com/thumbsup/thumbsup synced 2024-11-03 15:40:14 +00:00

Try to infer dates from the filename if there is no "date" metadata

This commit is contained in:
Romain 2017-03-31 23:20:02 +11:00
parent 8b66a16134
commit 3531f1e533
2 changed files with 42 additions and 3 deletions

View File

@ -3,6 +3,13 @@ const moment = require('moment')
const path = require('path')
const EXIF_DATE_FORMAT = 'YYYY:MM:DD HH:mm:ssZ'
// infer dates from files with a date-looking filename
const FILENAME_DATE_REGEX = /\d{4}[_\-.\s]?(\d{2}[_\-.\s]?){5}\..{3,4}/
// moment ignores non-numeric characters when parsing
const FILENAME_DATE_FORMAT = 'YYYYMMDD HHmmss'
var index = 0
/*
@ -13,7 +20,7 @@ function Media (file) {
this.file = file
this.filename = path.basename(file.path)
this.urls = _.mapValues(file.output, o => o.path)
this.date = exifDate(file)
this.date = getDate(file)
this.caption = caption(file)
this.isVideo = (file.type === 'video')
this.isAnimated = animated(file)
@ -26,13 +33,17 @@ function Media (file) {
// ]
}
function exifDate (file) {
function getDate (file) {
const date = tagValue(file, 'EXIF', 'DateTimeOriginal') ||
tagValue(file, 'H264', 'DateTimeOriginal') ||
tagValue(file, 'QuickTime', 'CreationDate')
if (date) {
return moment(date, EXIF_DATE_FORMAT).valueOf()
} else {
if (FILENAME_DATE_REGEX.test(file.path)) {
const namedate = moment(file.path, FILENAME_DATE_FORMAT)
if (namedate.isValid()) return namedate.valueOf()
}
return file.date
}
}

View File

@ -27,7 +27,35 @@ describe('Media', function () {
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())
})
it('defaults to the file date if there is no date in the metadata', function () {
it('infers the date from the filename (Android format)', function () {
const file = fixtures.file({path: 'folder/VID_20170220_114006.mp4'})
const media = new Media(file)
should(media.date).eql(fixtures.date('2017-02-20 11:40:06').getTime())
})
it('infers the date from the filename (Dropbox format)', function () {
const file = fixtures.file({path: 'folder/2017-03-24 19.42.30.jpg'})
const media = new Media(file)
should(media.date).eql(fixtures.date('2017-03-24 19:42:30').getTime())
})
it('only infers dates from valid formats', function () {
const file = fixtures.file({
path: 'folder/IMG_1234.jpg',
date: '2016-10-28 17:34:58'
})
const media = new Media(file)
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())
})
it('does not look at the file name if it already has EXIF data', function () {
const file = fixtures.file({path: '2017-03-24 19.42.30.jpg'})
file.meta.EXIF.DateTimeOriginal = '2016:10:28 17:34:58'
const media = new Media(file)
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())
})
it('defaults to the file date if there is no other date', function () {
const file = fixtures.file({date: '2016-10-28 17:34:58'})
const media = new Media(file)
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())