exifreader 4.9.2 → 4.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/exif-reader.d.ts CHANGED
@@ -53,6 +53,28 @@ interface PngFileTags {
53
53
  }
54
54
  }
55
55
 
56
+ interface PngTag {
57
+ description: string,
58
+ value: string | number
59
+ }
60
+
61
+ interface PngTags extends PngFileTags {
62
+ 'Pixels Per Unit X'?: NumberFileTag
63
+ 'Pixels Per Unit Y'?: NumberFileTag
64
+ 'Pixel Units'?: 'meters' | 'Unknown'
65
+ 'Modify Date'?: NumberArrayFileTag
66
+ [name: string]: PngTag
67
+ }
68
+
69
+ interface PngTextTag {
70
+ description: string,
71
+ value: string
72
+ }
73
+
74
+ interface PngTextTags {
75
+ [name: string]: PngTextTag
76
+ }
77
+
56
78
  interface NumberFileTag {
57
79
  description: string,
58
80
  value: number
@@ -129,6 +151,8 @@ interface ExpandedTags {
129
151
  file?: FileTags,
130
152
  jfif?: JfifTags,
131
153
  pngFile?: PngFileTags,
154
+ pngText?: PngTextTags,
155
+ png?: PngTags,
132
156
  exif?: Tags,
133
157
  iptc?: Tags,
134
158
  xmp?: { _raw: string } & XmpTags,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.9.2",
3
+ "version": "4.11.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -8,7 +8,7 @@
8
8
  */
9
9
  /* global Buffer, __non_webpack_require__ */
10
10
 
11
- import {objectAssign} from './utils.js';
11
+ import {objectAssign, dataUriToBuffer} from './utils.js';
12
12
  import DataViewWrapper from './dataview.js';
13
13
  import Constants from './constants.js';
14
14
  import {getStringValueFromArray} from './utils.js';
@@ -21,6 +21,8 @@ import IptcTags from './iptc-tags.js';
21
21
  import XmpTags from './xmp-tags.js';
22
22
  import IccTags from './icc-tags.js';
23
23
  import PngFileTags from './png-file-tags.js';
24
+ import PngTextTags from './png-text-tags.js';
25
+ import PngTags from './png-tags.js';
24
26
  import Thumbnail from './thumbnail.js';
25
27
  import exifErrors from './errors.js';
26
28
 
@@ -55,6 +57,10 @@ function loadFile(filename, options) {
55
57
  return nodeFetchRemoteFile(filename, options);
56
58
  }
57
59
 
60
+ if (isDataUri(filename)) {
61
+ return Promise.resolve(dataUriToBuffer(filename));
62
+ }
63
+
58
64
  return loadLocalFile(filename, options);
59
65
  }
60
66
 
@@ -99,6 +105,10 @@ function requireNodeGet(url) {
99
105
  return __non_webpack_require__('http').get;
100
106
  }
101
107
 
108
+ function isDataUri(filename) {
109
+ return /^data:[^;,]*(;base64)?,/.test(filename);
110
+ }
111
+
102
112
  function loadLocalFile(filename, {length} = {}) {
103
113
  return new Promise((resolve, reject) => {
104
114
  const fs = requireNodeFs();
@@ -193,7 +203,9 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
193
203
  xmpChunks,
194
204
  iccChunks,
195
205
  mpfDataOffset,
196
- pngHeaderOffset
206
+ pngHeaderOffset,
207
+ pngTextChunks,
208
+ pngChunkOffsets
197
209
  } = ImageHeader.parseAppMarkers(dataView);
198
210
 
199
211
  if (Constants.USE_JPEG && Constants.USE_FILE && hasFileData(fileDataOffset)) {
@@ -313,12 +325,34 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
313
325
  foundMetaData = true;
314
326
  const readTags = PngFileTags.read(dataView, pngHeaderOffset);
315
327
  if (expanded) {
328
+ tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
316
329
  tags.pngFile = readTags;
317
330
  } else {
318
331
  tags = objectAssign({}, tags, readTags);
319
332
  }
320
333
  }
321
334
 
335
+ if (Constants.USE_PNG && hasPngTextData(pngTextChunks)) {
336
+ foundMetaData = true;
337
+ const readTags = PngTextTags.read(dataView, pngTextChunks);
338
+ if (expanded) {
339
+ tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
340
+ tags.pngText = readTags;
341
+ } else {
342
+ tags = objectAssign({}, tags, readTags);
343
+ }
344
+ }
345
+
346
+ if (Constants.USE_PNG && hasPngData(pngChunkOffsets)) {
347
+ foundMetaData = true;
348
+ const readTags = PngTags.read(dataView, pngChunkOffsets);
349
+ if (expanded) {
350
+ tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
351
+ } else {
352
+ tags = objectAssign({}, tags, readTags);
353
+ }
354
+ }
355
+
322
356
  const thumbnail = (Constants.USE_JPEG || Constants.USE_WEBP)
323
357
  && Constants.USE_EXIF
324
358
  && Constants.USE_THUMBNAIL
@@ -408,3 +442,11 @@ function hasMpfData(mpfDataOffset) {
408
442
  function hasPngFileData(pngFileDataOffset) {
409
443
  return pngFileDataOffset !== undefined;
410
444
  }
445
+
446
+ function hasPngTextData(pngTextChunks) {
447
+ return pngTextChunks !== undefined;
448
+ }
449
+
450
+ function hasPngData(pngChunkOffsets) {
451
+ return pngChunkOffsets !== undefined;
452
+ }
@@ -2,6 +2,8 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
+ // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
+
5
7
  import {getStringFromDataView} from './utils.js';
6
8
  import Constants from './constants.js';
7
9
 
@@ -12,11 +14,16 @@ export default {
12
14
 
13
15
  const PNG_ID = '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a';
14
16
  const PNG_CHUNK_LENGTH_SIZE = 4;
15
- const PNG_CHUNK_TYPE_SIZE = 4;
16
- const PNG_CHUNK_LENGTH_OFFSET = 0;
17
- const PNG_CHUNK_TYPE_OFFSET = PNG_CHUNK_LENGTH_SIZE;
18
- const PNG_CHUNK_DATA_OFFSET = PNG_CHUNK_LENGTH_SIZE + PNG_CHUNK_TYPE_SIZE;
17
+ export const PNG_CHUNK_TYPE_SIZE = 4;
18
+ export const PNG_CHUNK_LENGTH_OFFSET = 0;
19
+ export const PNG_CHUNK_TYPE_OFFSET = PNG_CHUNK_LENGTH_SIZE;
20
+ export const PNG_CHUNK_DATA_OFFSET = PNG_CHUNK_LENGTH_SIZE + PNG_CHUNK_TYPE_SIZE;
19
21
  const PNG_XMP_PREFIX = 'XML:com.adobe.xmp\x00';
22
+ export const TYPE_TEXT = 'tEXt';
23
+ export const TYPE_ITXT = 'iTXt';
24
+ export const TYPE_PHYS = 'pHYs';
25
+ export const TYPE_TIME = 'tIME';
26
+ export const TYPE_EXIF = 'eXIf';
20
27
 
21
28
  function isPngFile(dataView) {
22
29
  return !!dataView && getStringFromDataView(dataView, 0, PNG_ID.length) === PNG_ID;
@@ -44,6 +51,26 @@ function findPngOffsets(dataView) {
44
51
  length: dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET) - (dataOffset - (offset + PNG_CHUNK_DATA_OFFSET))
45
52
  }];
46
53
  }
54
+ } else if (isPngTextChunk(dataView, offset)) {
55
+ offsets.hasAppMarkers = true;
56
+ const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
57
+ if (!offsets.pngTextChunks) {
58
+ offsets.pngTextChunks = [];
59
+ }
60
+ offsets.pngTextChunks.push({
61
+ length: dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET),
62
+ type: chunkType,
63
+ offset: offset + PNG_CHUNK_DATA_OFFSET
64
+ });
65
+ } else if (isPngExifChunk(dataView, offset)) {
66
+ offsets.hasAppMarkers = true;
67
+ offsets.tiffHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
68
+ } else if (isPngChunk(dataView, offset)) {
69
+ offsets.hasAppMarkers = true;
70
+ if (!offsets.pngChunkOffsets) {
71
+ offsets.pngChunkOffsets = [];
72
+ }
73
+ offsets.pngChunkOffsets.push(offset + PNG_CHUNK_LENGTH_OFFSET);
47
74
  }
48
75
 
49
76
  offset += dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET)
@@ -66,6 +93,23 @@ function isPngXmpChunk(dataView, offset) {
66
93
  && (getStringFromDataView(dataView, offset + PNG_CHUNK_DATA_OFFSET, PNG_XMP_PREFIX.length) === PNG_XMP_PREFIX);
67
94
  }
68
95
 
96
+ function isPngTextChunk(dataView, offset) {
97
+ const PNG_CHUNK_TYPE_TEXT = 'tEXt';
98
+ const PNG_CHUNK_TYPE_ITXT = 'iTXt';
99
+ const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
100
+ return chunkType === PNG_CHUNK_TYPE_TEXT || chunkType === PNG_CHUNK_TYPE_ITXT;
101
+ }
102
+
103
+ function isPngExifChunk(dataView, offset) {
104
+ return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_EXIF;
105
+ }
106
+
107
+ function isPngChunk(dataView, offset) {
108
+ const SUPPORTED_PNG_CHUNK_TYPES = [TYPE_PHYS, TYPE_TIME];
109
+ const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
110
+ return SUPPORTED_PNG_CHUNK_TYPES.includes(chunkType);
111
+ }
112
+
69
113
  function getPngXmpDataOffset(dataView, offset) {
70
114
  const COMPRESSION_FLAG_SIZE = 1;
71
115
  const COMPRESSION_METHOD_SIZE = 1;
@@ -0,0 +1,106 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ import Types from './types.js';
6
+ import {PNG_CHUNK_LENGTH_OFFSET, PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_DATA_OFFSET, PNG_CHUNK_TYPE_SIZE, TYPE_PHYS, TYPE_TIME} from './image-header-png.js';
7
+ import {getStringFromDataView} from './utils.js';
8
+
9
+ export default {
10
+ read
11
+ };
12
+
13
+ function read(dataView, chunkOffsets) {
14
+ const tags = {};
15
+
16
+ for (let i = 0; i < chunkOffsets.length; i++) {
17
+ const chunkLength = Types.getLongAt(dataView, chunkOffsets[i] + PNG_CHUNK_LENGTH_OFFSET);
18
+ const chunkType = getStringFromDataView(dataView, chunkOffsets[i] + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
19
+
20
+ if (chunkType === TYPE_PHYS) {
21
+ tags['Pixels Per Unit X'] = getPixelsPerUnitX(dataView, chunkOffsets[i], chunkLength);
22
+ tags['Pixels Per Unit Y'] = getPixelsPerUnitY(dataView, chunkOffsets[i], chunkLength);
23
+ tags['Pixel Units'] = getPixelUnits(dataView, chunkOffsets[i], chunkLength);
24
+ } else if (chunkType === TYPE_TIME) {
25
+ tags['Modify Date'] = getModifyDate(dataView, chunkOffsets[i], chunkLength);
26
+ }
27
+ }
28
+
29
+ return tags;
30
+ }
31
+
32
+ function getPixelsPerUnitX(dataView, chunkOffset, chunkLength) {
33
+ const TAG_OFFSET = 0;
34
+ const TAG_SIZE = 4;
35
+
36
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, TAG_OFFSET, TAG_SIZE)) {
37
+ return undefined;
38
+ }
39
+
40
+ const value = Types.getLongAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + TAG_OFFSET);
41
+
42
+ return {
43
+ value,
44
+ description: '' + value
45
+ };
46
+ }
47
+
48
+ function getPixelsPerUnitY(dataView, chunkOffset, chunkLength) {
49
+ const TAG_OFFSET = 4;
50
+ const TAG_SIZE = 4;
51
+
52
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, TAG_OFFSET, TAG_SIZE)) {
53
+ return undefined;
54
+ }
55
+
56
+ const value = Types.getLongAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + TAG_OFFSET);
57
+
58
+ return {
59
+ value,
60
+ description: '' + value
61
+ };
62
+ }
63
+
64
+ function getPixelUnits(dataView, chunkOffset, chunkLength) {
65
+ const TAG_OFFSET = 8;
66
+ const TAG_SIZE = 1;
67
+
68
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, TAG_OFFSET, TAG_SIZE)) {
69
+ return undefined;
70
+ }
71
+
72
+ const value = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + TAG_OFFSET);
73
+
74
+ return {
75
+ value,
76
+ description: value === 1 ? 'meters' : 'Unknown'
77
+ };
78
+ }
79
+
80
+ function getModifyDate(dataView, chunkOffset, chunkLength) {
81
+ const TIME_TAG_SIZE = 7;
82
+
83
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, 0, TIME_TAG_SIZE)) {
84
+ return undefined;
85
+ }
86
+
87
+ const year = Types.getShortAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET);
88
+ const month = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 2);
89
+ const day = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 3);
90
+ const hours = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 4);
91
+ const minutes = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 5);
92
+ const seconds = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 6);
93
+
94
+ return {
95
+ value: [year, month, day, hours, minutes, seconds],
96
+ description: `${pad(year, 4)}-${pad(month, 2)}-${pad(day, 2)} ${pad(hours, 2)}:${pad(minutes, 2)}:${pad(seconds, 2)}`
97
+ };
98
+ }
99
+
100
+ function tagFitsInBuffer(dataView, chunkOffset, chunkLength, tagOffset, tagSize) {
101
+ return tagOffset + tagSize <= chunkLength && chunkOffset + PNG_CHUNK_DATA_OFFSET + tagOffset + tagSize <= dataView.byteLength;
102
+ }
103
+
104
+ function pad(number, size) {
105
+ return `${'0'.repeat(size - ('' + number).length)}${number}`;
106
+ }
@@ -0,0 +1,92 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ import {getStringValueFromArray} from './utils.js';
6
+ import TagDecoder from './tag-decoder.js';
7
+ import {TYPE_TEXT, TYPE_ITXT} from './image-header-png.js';
8
+
9
+ export default {
10
+ read
11
+ };
12
+
13
+ const STATE_KEYWORD = 'STATE_KEYWORD';
14
+ const STATE_COMPRESSION = 'STATE_COMPRESSION';
15
+ const STATE_LANG = 'STATE_LANG';
16
+ const STATE_TRANSLATED_KEYWORD = 'STATE_TRANSLATED_KEYWORD';
17
+ const STATE_TEXT = 'STATE_TEXT';
18
+ const COMPRESSION_SECTION_LENGTH = 2;
19
+
20
+ function read(dataView, pngTextChunks) {
21
+ return pngTextChunks.reduce((tags, {offset, length, type}) => {
22
+ const {name, value, description} = getNameAndValue(dataView, offset, length, type);
23
+ if (name) {
24
+ tags[name] = {
25
+ value,
26
+ description
27
+ };
28
+ }
29
+ return tags;
30
+ }, {});
31
+ }
32
+
33
+ function getNameAndValue(dataView, offset, length, type) {
34
+ const keywordChars = [];
35
+ const langChars = [];
36
+ const translatedKeywordChars = [];
37
+ const valueChars = [];
38
+ let parsingState = STATE_KEYWORD;
39
+
40
+ for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
41
+ if (parsingState === STATE_COMPRESSION) {
42
+ const compressionFlag = dataView.getUint8(offset + i);
43
+ if (compressionFlag !== 0) {
44
+ return {}; // We don't support compression.
45
+ }
46
+ i += COMPRESSION_SECTION_LENGTH;
47
+ parsingState = moveToNextState(type, parsingState);
48
+ }
49
+ const byte = dataView.getUint8(offset + i);
50
+ if (byte === 0) {
51
+ parsingState = moveToNextState(type, parsingState);
52
+ } else if (parsingState === STATE_KEYWORD) {
53
+ keywordChars.push(byte);
54
+ } else if (parsingState === STATE_LANG) {
55
+ langChars.push(byte);
56
+ } else if (parsingState === STATE_TRANSLATED_KEYWORD) {
57
+ translatedKeywordChars.push(byte);
58
+ } else {
59
+ valueChars.push(byte);
60
+ }
61
+ }
62
+
63
+ const textValue = getStringValueFromArray(valueChars);
64
+
65
+ return {
66
+ name: getName(type, langChars, keywordChars),
67
+ value: textValue,
68
+ description: type === TYPE_ITXT ? TagDecoder.decode('UTF-8', valueChars) : textValue
69
+ };
70
+ }
71
+
72
+ function moveToNextState(type, parsingState) {
73
+ if (parsingState === STATE_KEYWORD && type === TYPE_ITXT) {
74
+ return STATE_COMPRESSION;
75
+ }
76
+ if (parsingState === STATE_COMPRESSION) {
77
+ return STATE_LANG;
78
+ }
79
+ if (parsingState === STATE_LANG) {
80
+ return STATE_TRANSLATED_KEYWORD;
81
+ }
82
+ return STATE_TEXT;
83
+ }
84
+
85
+ function getName(type, langChars, keywordChars) {
86
+ const name = getStringValueFromArray(keywordChars);
87
+ if (type === TYPE_TEXT || langChars.length === 0) {
88
+ return name;
89
+ }
90
+ const lang = getStringValueFromArray(langChars);
91
+ return `${name} (${lang})`;
92
+ }
package/src/utils.js CHANGED
@@ -73,3 +73,29 @@ export function getBase64Image(image) {
73
73
  }
74
74
  return (new Buffer(image)).toString('base64'); // eslint-disable-line no-undef
75
75
  }
76
+
77
+ export function dataUriToBuffer(dataUri) {
78
+ const data = dataUri.substring(dataUri.indexOf(',') + 1);
79
+
80
+ if (dataUri.indexOf(';base64') !== -1) {
81
+ if (typeof atob !== 'undefined') {
82
+ return Uint8Array.from(atob(data), (char) => char.charCodeAt(0)).buffer;
83
+ }
84
+ if (typeof Buffer === 'undefined') {
85
+ return undefined;
86
+ }
87
+ if (typeof Buffer.from !== undefined) { // eslint-disable-line no-undef
88
+ return Buffer.from(data, 'base64'); // eslint-disable-line no-undef
89
+ }
90
+ return new Buffer(data, 'base64'); // eslint-disable-line no-undef
91
+ }
92
+
93
+ const decodedData = decodeURIComponent(data);
94
+ if (typeof Buffer !== 'undefined') {
95
+ if (typeof Buffer.from !== undefined) { // eslint-disable-line no-undef
96
+ return Buffer.from(decodedData); // eslint-disable-line no-undef
97
+ }
98
+ return new Buffer(decodedData); // eslint-disable-line no-undef
99
+ }
100
+ return Uint8Array.from(decodedData, (char) => char.charCodeAt(0)).buffer;
101
+ }