Skip to content

Commit

Permalink
implement ReadRawTask. Fixes #44. Fixes #45.
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Feb 27, 2019
1 parent b37c4a0 commit aeff9a7
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ vendored versions of ExifTool match the version they vendor.

## Version history

### v8.2.0

- ✨ Implemented `ExifTool.readRaw`. If you decide to use this method, please
take care to read the method's documentation. Addresses both
[#44](https://github.com/mceachen/exiftool-vendored.js/issues/44) and
[#45](https://github.com/mceachen/exiftool-vendored.js/issues/45).

### v8.1.0

- ✨ Added support for EXIF dates that include both the UTC offset as well as the
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@types/globule": "^1.1.3",
"@types/luxon": "^1.11.1",
"@types/mocha": "^5.2.6",
"@types/node": "^11.9.4",
"@types/node": "^11.9.5",
"@types/pify": "^3.0.2",
"@types/progress": "^2.0.3",
"@types/rimraf": "^2.0.2",
Expand All @@ -82,7 +82,7 @@
"decompress-zip": "^0.3.1",
"fs-extra": "^7.0.1",
"globule": "^1.2.1",
"mocha": "^6.0.0",
"mocha": "^6.0.2",
"npm-run-all": "^4.1.5",
"nyc": "^13.3.0",
"pify": "^4.0.1",
Expand All @@ -94,7 +94,7 @@
"tar-fs": "^2.0.0",
"tmp": "^0.0.33",
"typedoc": "^0.14.2",
"typescript": "^3.3.3",
"typescript": "^3.3.3333",
"xmldom": "^0.1.27",
"xpath": "^0.0.27"
},
Expand Down
8 changes: 8 additions & 0 deletions src/ExifTool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe("ExifTool", function() {
)
})

it("returns raw tag values", async () => {
return expect(et.readRaw(img, ["-Make", "-Model"])).to.eventually.eql({
Make: "Apple",
Model: "iPhone 7 Plus",
SourceFile: img
}) // and nothing else
})

it("returns expected results for a given file with non-english filename", async function() {
this.slow(500)
return expect(
Expand Down
32 changes: 30 additions & 2 deletions src/ExifTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RewriteAllTagsTask } from "./RewriteAllTagsTask"
import { Tags } from "./Tags"
import { VersionTask } from "./VersionTask"
import { WriteTask } from "./WriteTask"
import { ReadRawTask } from "./ReadRawTask"

export { Tags } from "./Tags"
export { ExifDate } from "./ExifDate"
Expand Down Expand Up @@ -231,8 +232,9 @@ export class ExifTool {
*
* @param {string} file the file to extract metadata tags from
* @param {string[]} [args] any additional ExifTool arguments, like "-fast" or
* "-fast2". Note that the default is "-fast", so if you want ExifTool to read
* the entire file for metadata, you should pass an empty array as the second
* "-fast2". **Most other arguments will require you to use `readRaw`.**
* Note that the default is "-fast", so if you want ExifTool to read the
* entire file for metadata, you should pass an empty array as the second
* parameter. See https://sno.phy.queensu.ca/~phil/exiftool/#performance for
* more information about `-fast` and `-fast2`.
* @returns {Promise<Tags>} A resolved Tags promise. If there are errors
Expand All @@ -245,6 +247,32 @@ export class ExifTool {
)
}

/**
* Read the tags from `file`, without any post-processing of ExifTool values.
*
* **You probably don't want this method. You want `read`. READ THE REST OF THIS CAREFULLY.**
*
* If you want to extract specific tag values from a file, you may want to use
* this, but all data validation and error recovery heuristics provided by
* `read` will be skipped.
*
* Note that performance will be very similar to `read`, and will actually be
* worse if you don't include `-fast` or `-fast2` (as the most expensive bit
* is the perl interpreter and scanning the file on disk).
*
* @param args **all ExifTool arguments**, except for the file path. If
* `-charset` or `filename=utf8` are missing, and you have non-ascii tag
* values, you're going to have a bad day. The resolved pathname will be
* appended to the args array for you, and if `-json` is missing from `args`,
* that will be prepended, as it's a prerequisite to parsing the result.
*
* @return Note that the values **might** match `Tags`, but they might not.
* You're off the reservation here.
*/
readRaw(file: string, args: string[]): Promise<Tags> {
return this.enqueueTask(() => ReadRawTask.for(file, args))
}

/**
* Write the given `tags` to `file`.
*
Expand Down
32 changes: 32 additions & 0 deletions src/ReadRawTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { logger } from "batch-cluster"
import * as _path from "path"

import { ExifToolTask } from "./ExifToolTask"
import { Tags } from "./Tags"

export class ReadRawTask extends ExifToolTask<Tags> {
static for(filename: string, exiftoolArgs: string[] = []): ReadRawTask {
const args = [...exiftoolArgs]
if (!args.includes("-json")) args.unshift("-json")
const sourceFile = _path.resolve(filename)
args.push(sourceFile)
return new ReadRawTask(sourceFile, args)
}

private constructor(readonly sourceFile: string, readonly args: string[]) {
super(args)
}

toString(): string {
return "ReadRawTask" + this.sourceFile + ")"
}

protected parse(data: string, err?: Error): Tags {
try {
return JSON.parse(data)[0]
} catch (jsonError) {
logger().error("ExifTool.ReadRawTask(): Invalid JSON", { data })
throw err || jsonError
}
}
}
23 changes: 14 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,16 @@
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.6.tgz#b8622d50557dd155e9f2f634b7d68fd38de5e94b"
integrity sha512-1axi39YdtBI7z957vdqXI4Ac25e7YihYQtJa+Clnxg1zTJEaIRbndt71O3sP4GAMgiAm0pY26/b9BrY4MR/PMw==

"@types/node@*", "@types/node@^11.9.4":
"@types/node@*":
version "11.9.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.9.4.tgz#ceb0048a546db453f6248f2d1d95e937a6f00a14"
integrity sha512-Zl8dGvAcEmadgs1tmSPcvwzO1YRsz38bVJQvH1RvRqSR9/5n61Q1ktcDL0ht3FXWR+ZpVmXVwN1LuH4Ax23NsA==

"@types/node@^11.9.5":
version "11.9.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.9.5.tgz#011eece9d3f839a806b63973e228f85967b79ed3"
integrity sha512-vVjM0SVzgaOUpflq4GYBvCpozes8OgIIS5gVXVka+OfK3hvnkC1i93U8WiY2OtNE4XUWyyy/86Kf6e0IHTQw1Q==

"@types/pify@^3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@types/pify/-/pify-3.0.2.tgz#1bc75dac43e31dba981c37e0a08edddc1b49cd39"
Expand Down Expand Up @@ -1763,10 +1768,10 @@ mkpath@^0.1.0:
resolved "https://registry.yarnpkg.com/mkpath/-/mkpath-0.1.0.tgz#7554a6f8d871834cc97b5462b122c4c124d6de91"
integrity sha1-dVSm+Nhxg0zJe1RisSLEwSTW3pE=

mocha@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-6.0.0.tgz#b558da6245a09581aa4a1c6aee9e0fa6ad0e1767"
integrity sha512-A7g9k3yr8oJaXn2IItFnfgjyxFc/LTe6Wwv7FczP+e8G74o9xYNSbMYmCf1ouldRojLrFcOb+z75P6Ak0GX6ug==
mocha@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-6.0.2.tgz#cdc1a6fdf66472c079b5605bac59d29807702d2c"
integrity sha512-RtTJsmmToGyeTznSOMoM6TPEk1A84FQaHIciKrRqARZx+B5ccJ5tXlmJzEKGBxZdqk9UjpRsesZTUkZmR5YnuQ==
dependencies:
ansi-colors "3.2.3"
browser-stdout "1.3.1"
Expand Down Expand Up @@ -2793,10 +2798,10 @@ typescript@3.2.x:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.4.tgz#c585cb952912263d915b462726ce244ba510ef3d"
integrity sha512-0RNDbSdEokBeEAkgNbxJ+BLwSManFy9TeXz8uW+48j/xhEXv1ePME60olyzw2XzUqUBNAYFeJadIqAgNqIACwg==

typescript@^3.3.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.3.3.tgz#f1657fc7daa27e1a8930758ace9ae8da31403221"
integrity sha512-Y21Xqe54TBVp+VDSNbuDYdGw0BpoR/Q6wo/+35M8PAU0vipahnyduJWirxxdxjsAkS7hue53x2zp8gz7F05u0A==
typescript@^3.3.3333:
version "3.3.3333"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.3.3333.tgz#171b2c5af66c59e9431199117a3bcadc66fdcfd6"
integrity sha512-JjSKsAfuHBE/fB2oZ8NxtRTk5iGcg6hkYXMnZ3Wc+b2RSqejEqTaem11mHASMnFilHrax3sLK0GDzcJrekZYLw==

tz-lookup@^6.1.8:
version "6.1.8"
Expand Down

0 comments on commit aeff9a7

Please sign in to comment.