-
Notifications
You must be signed in to change notification settings - Fork 4
/
metadata_printer.js
66 lines (60 loc) · 1.45 KB
/
metadata_printer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2018 Scriptim (see LICENSE.md)
const nodeExiftool = require('node-exiftool')
const exiftool = new nodeExiftool.ExiftoolProcess(require('dist-exiftool'))
const chalk = require('chalk')
const metatags = [
[
'FileType',
'FileSize',
'FileModifyDate',
'FileAccessDate',
'FilePermissions',
'PDFVersion',
'PageCount',
'Linearized'
],
[
'Title',
'Author',
'Subject',
'CreateDate',
'FileModifyDate',
'Creator',
'Producer',
'Keywords'
]
]
const useFile = filename => {
process.stdout.write(`Printing metadata of file ${chalk.bold(filename)}\n`)
exiftool
.open()
.then(() => filename)
.then(readMetadata)
.then(printMetadata)
.then(() => exiftool.close())
.catch(err => {
process.stderr.write(chalk.red.bold(err.message, '\n'))
process.exit(1)
})
}
const readMetadata = filename => {
return metadata = exiftool.readMetadata(filename, metatags)
.then(metadata => {
if (metadata.error !== null) {
throw new Error(metadata.error)
}
if (metadata.data[0].FileType !== 'PDF') {
throw new Error(`File is not a pdf file: ${filename}`)
}
return metadata.data[0]
})
}
const printMetadata = metadata => {
for (let tagGroup of metatags) {
process.stdout.write('\n')
for (let tag of tagGroup) {
process.stdout.write(`${tag}: ${chalk.blue(metadata[tag])}\n`)
}
}
}
module.exports = { useFile }