exifreader 4.26.2 → 4.28.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.26.2",
3
+ "version": "4.28.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -37,6 +37,7 @@
37
37
  "eslint": "^8.52.0",
38
38
  "eslint-plugin-cypress": "^3.0.2",
39
39
  "husky": "^9.0.11",
40
+ "linkedom": "^0.18.9",
40
41
  "mocha": "^11.0.1",
41
42
  "npm-run-all": "^4.1.5",
42
43
  "nyc": "^17.0.0",
@@ -70,7 +71,7 @@
70
71
  "check-coverage": true,
71
72
  "statements": 93,
72
73
  "branches": 88,
73
- "functions": 98,
74
+ "functions": 97,
74
75
  "lines": 93,
75
76
  "reporter": [
76
77
  "lcov",
@@ -0,0 +1,75 @@
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
+ export default {
6
+ get,
7
+ };
8
+
9
+ function get(tags, expanded) {
10
+ const compositeTags = {};
11
+ let hasCompositeTags = false;
12
+
13
+ const focalLength = getTagValue(tags, 'FocalLength', expanded);
14
+ const focalLengthIn35mmFilm = getTagValue(tags, 'FocalLengthIn35mmFilm', expanded);
15
+
16
+ const scaleFactorTo35mmEquivalent = getScaleFactorTo35mmEquivalent(focalLength, focalLengthIn35mmFilm);
17
+ if (scaleFactorTo35mmEquivalent) {
18
+ compositeTags.ScaleFactorTo35mmEquivalent = scaleFactorTo35mmEquivalent;
19
+ hasCompositeTags = true;
20
+ }
21
+
22
+ const fieldOfView = getFieldOfView(focalLengthIn35mmFilm);
23
+ if (fieldOfView) {
24
+ compositeTags.FieldOfView = fieldOfView;
25
+ hasCompositeTags = true;
26
+ }
27
+
28
+ if (hasCompositeTags) {
29
+ return compositeTags;
30
+ }
31
+
32
+ return undefined;
33
+ }
34
+
35
+ function getTagValue(tags, tagName, expanded) {
36
+ if (expanded && tags.exif && tags.exif[tagName]) {
37
+ return tags.exif[tagName].value;
38
+ }
39
+ if (!expanded && tags[tagName]) {
40
+ return tags[tagName].value;
41
+ }
42
+ return undefined;
43
+ }
44
+
45
+ function getScaleFactorTo35mmEquivalent(focalLength, focalLengthIn35mmFilm) {
46
+ if (focalLength && focalLengthIn35mmFilm) {
47
+ try {
48
+ const value = focalLengthIn35mmFilm / (focalLength[0] / focalLength[1]);
49
+ return {
50
+ value,
51
+ description: value.toFixed(1),
52
+ };
53
+ } catch (error) {
54
+ // Ignore.
55
+ }
56
+ }
57
+ return undefined;
58
+ }
59
+
60
+ function getFieldOfView(focalLengthIn35mmFilm) {
61
+ const FULL_FRAME_SENSOR_WIDTH_MM = 36;
62
+
63
+ if (focalLengthIn35mmFilm) {
64
+ try {
65
+ const value = 2 * Math.atan(FULL_FRAME_SENSOR_WIDTH_MM / (2 * focalLengthIn35mmFilm)) * (180 / Math.PI);
66
+ return {
67
+ value,
68
+ description: value.toFixed(1) + ' deg',
69
+ };
70
+ } catch (error) {
71
+ // Ignore.
72
+ }
73
+ }
74
+ return undefined;
75
+ }
package/src/dom-parser.js CHANGED
@@ -6,7 +6,11 @@ export default {
6
6
  get
7
7
  };
8
8
 
9
- function get() {
9
+ function get(domParser) {
10
+ if (domParser) {
11
+ return domParser;
12
+ }
13
+
10
14
  if (typeof DOMParser !== 'undefined') {
11
15
  return new DOMParser();
12
16
  }
@@ -29,6 +29,7 @@ import PngTags from './png-tags.js';
29
29
  import Vp8xTags from './vp8x-tags.js';
30
30
  import GifFileTags from './gif-file-tags.js';
31
31
  import Thumbnail from './thumbnail.js';
32
+ import Composite from './composite.js';
32
33
  import exifErrors from './errors.js';
33
34
 
34
35
  export default {
@@ -200,7 +201,17 @@ function getDataView(data) {
200
201
 
201
202
  export function loadView(
202
203
  dataView,
203
- {expanded = false, async = false, includeUnknown = false} = {expanded: false, async: false, includeUnknown: false}
204
+ {
205
+ expanded = false,
206
+ async = false,
207
+ includeUnknown = false,
208
+ domParser = undefined,
209
+ } = {
210
+ expanded: false,
211
+ async: false,
212
+ includeUnknown: false,
213
+ domParser: undefined,
214
+ }
204
215
  ) {
205
216
  let foundMetaData = false;
206
217
  let tags = {};
@@ -267,7 +278,7 @@ export function loadView(
267
278
  }
268
279
 
269
280
  if (Constants.USE_TIFF && Constants.USE_XMP && readTags['ApplicationNotes'] && !hasXmpData(xmpChunks)) {
270
- const readXmpTags = XmpTags.read(getStringValueFromArray(readTags['ApplicationNotes'].value));
281
+ const readXmpTags = XmpTags.read(getStringValueFromArray(readTags['ApplicationNotes'].value), undefined, domParser);
271
282
  if (expanded) {
272
283
  tags.xmp = readXmpTags;
273
284
  } else {
@@ -330,7 +341,7 @@ export function loadView(
330
341
 
331
342
  if (Constants.USE_XMP && hasXmpData(xmpChunks)) {
332
343
  foundMetaData = true;
333
- const readTags = XmpTags.read(dataView, xmpChunks);
344
+ const readTags = XmpTags.read(dataView, xmpChunks, domParser);
334
345
  if (expanded) {
335
346
  tags.xmp = readTags;
336
347
  } else {
@@ -409,6 +420,15 @@ export function loadView(
409
420
  }
410
421
  }
411
422
 
423
+ const composite = Composite.get(tags, expanded);
424
+ if (composite) {
425
+ if (expanded) {
426
+ tags.composite = composite;
427
+ } else {
428
+ tags = objectAssign({}, tags, composite);
429
+ }
430
+ }
431
+
412
432
  const thumbnail = (Constants.USE_JPEG || Constants.USE_WEBP)
413
433
  && Constants.USE_EXIF
414
434
  && Constants.USE_THUMBNAIL
@@ -295,12 +295,12 @@ export default {
295
295
  }
296
296
  },
297
297
  0xa405: {
298
- 'name': 'FocalLengthIn35mmFilm',
298
+ 'name': 'FocalLengthIn35mmFilm', // Sometimes called FocalLengthIn35mmFormat.
299
299
  'description': (value) => {
300
300
  if (value === 0) {
301
301
  return 'Unknown';
302
302
  }
303
- return value;
303
+ return value + ' mm';
304
304
  }
305
305
  },
306
306
  0xa406: {
@@ -0,0 +1,93 @@
1
+ export function isMissingNamespaceError(error) {
2
+ const missingNamespaceStrings = [
3
+ // @xmldom/xmldom
4
+ 'prefix is non-null and namespace is null',
5
+ // Firefox
6
+ 'prefix not bound to a namespace', // en
7
+ 'prefix inte bundet till en namnrymd', // sv
8
+ // Chrome
9
+ /Namespace prefix .+ is not defined/
10
+ ];
11
+
12
+ for (let i = 0; i < missingNamespaceStrings.length; i++) {
13
+ const regexp = new RegExp(missingNamespaceStrings[i]);
14
+ if (regexp.test(error.message)) {
15
+ return true;
16
+ }
17
+ }
18
+
19
+ return false;
20
+ }
21
+
22
+ // This should fix most missing namespace errors, but it's not a complete solution.
23
+ export function addMissingNamespaces(xmlString) {
24
+ const rootTagMatch = xmlString.match(/<([A-Za-z_][A-Za-z0-9._-]*)([^>]*)>/);
25
+ if (!rootTagMatch) {
26
+ return xmlString;
27
+ }
28
+ const rootTagName = rootTagMatch[1];
29
+
30
+ const declaredPrefixes = getAllDeclaredNamespacePrefixes(xmlString);
31
+ const usedPrefixes = getUsedNamespacePrefixes(xmlString);
32
+ const missingPrefixes = usedPrefixes.filter((prefix) => declaredPrefixes.indexOf(prefix) === -1);
33
+ if (missingPrefixes.length === 0) {
34
+ return xmlString;
35
+ }
36
+
37
+ const namespaceDeclarations = createNamespaceDeclarations(missingPrefixes);
38
+ return insertDeclarationsIntoRoot(xmlString, rootTagName, namespaceDeclarations);
39
+ }
40
+
41
+ function getAllDeclaredNamespacePrefixes(xmlContent) {
42
+ const prefixes = [];
43
+ const namespaceDeclarationRegex = /xmlns:([\w-]+)=["'][^"']+["']/g;
44
+ let match;
45
+ while ((match = namespaceDeclarationRegex.exec(xmlContent)) !== null) {
46
+ if (prefixes.indexOf(match[1]) === -1) {
47
+ prefixes.push(match[1]);
48
+ }
49
+ }
50
+ return prefixes;
51
+ }
52
+
53
+ function getUsedNamespacePrefixes(xmlContent) {
54
+ const prefixes = [];
55
+ const prefixUsageRegex = /\b([A-Za-z_][A-Za-z0-9._-]*):[A-Za-z_][A-Za-z0-9._-]*\b/g;
56
+ let match;
57
+ while ((match = prefixUsageRegex.exec(xmlContent)) !== null) {
58
+ const prefix = match[1];
59
+ if (prefix === 'xmlns' || prefix === 'xml') {
60
+ continue;
61
+ }
62
+ if (prefixes.indexOf(prefix) === -1) {
63
+ prefixes.push(prefix);
64
+ }
65
+ }
66
+ return prefixes;
67
+ }
68
+
69
+ const KNOWN_NAMESPACE_URIS = {
70
+ xmp: 'http://ns.adobe.com/xap/1.0/',
71
+ tiff: 'http://ns.adobe.com/tiff/1.0/',
72
+ exif: 'http://ns.adobe.com/exif/1.0/',
73
+ dc: 'http://purl.org/dc/elements/1.1/',
74
+ xmpMM: 'http://ns.adobe.com/xap/1.0/mm/',
75
+ stEvt: 'http://ns.adobe.com/xap/1.0/sType/ResourceEvent#',
76
+ stRef: 'http://ns.adobe.com/xap/1.0/sType/ResourceRef#',
77
+ photoshop: 'http://ns.adobe.com/photoshop/1.0/'
78
+ };
79
+
80
+ function createNamespaceDeclarations(prefixes) {
81
+ const declarations = [];
82
+ for (let i = 0; i < prefixes.length; i++) {
83
+ const prefix = prefixes[i];
84
+ const uri = KNOWN_NAMESPACE_URIS[prefix] || 'http://fallback.namespace/' + prefix;
85
+ declarations.push(' xmlns:' + prefix + '="' + uri + '"');
86
+ }
87
+ return declarations.join('');
88
+ }
89
+
90
+ function insertDeclarationsIntoRoot(xmlString, rootTagName, declarations) {
91
+ const rootTagPattern = new RegExp('<' + rootTagName + '([^>]*)>');
92
+ return xmlString.replace(rootTagPattern, '<' + rootTagName + '$1' + declarations + '>');
93
+ }
package/src/xmp-tags.js CHANGED
@@ -5,25 +5,33 @@
5
5
  import {getStringFromDataView, objectAssign} from './utils.js';
6
6
  import XmpTagNames from './xmp-tag-names.js';
7
7
  import DOMParser from './dom-parser.js';
8
+ import {isMissingNamespaceError, addMissingNamespaces} from './xmp-namespaces.js';
8
9
 
9
10
  export default {
10
11
  read
11
12
  };
12
13
 
13
- function read(dataView, chunks) {
14
+ class ParseError extends Error {
15
+ constructor(message) {
16
+ super(message);
17
+ this.name = 'ParseError';
18
+ }
19
+ }
20
+
21
+ function read(dataView, chunks, domParser) {
14
22
  const tags = {};
15
23
 
16
24
  if (typeof dataView === 'string') {
17
- readTags(tags, dataView);
25
+ readTags(tags, dataView, domParser);
18
26
  return tags;
19
27
  }
20
28
 
21
29
  const [standardXmp, extendedXmp] = extractCompleteChunks(dataView, chunks);
22
30
 
23
- const hasStandardTags = readTags(tags, standardXmp);
31
+ const hasStandardTags = readTags(tags, standardXmp, domParser);
24
32
 
25
33
  if (extendedXmp) {
26
- const hasExtendedTags = readTags(tags, extendedXmp);
34
+ const hasExtendedTags = readTags(tags, extendedXmp, domParser);
27
35
 
28
36
  if (!hasStandardTags && !hasExtendedTags) {
29
37
  // Some writers are not spec-compliant in that they split an XMP
@@ -31,7 +39,7 @@ function read(dataView, chunks) {
31
39
  // XMP block. If we failed parsing both of the XMPs in the regular
32
40
  // way, we try to combine them to see if that works better.
33
41
  delete tags._raw;
34
- readTags(tags, combineChunks(dataView, chunks));
42
+ readTags(tags, combineChunks(dataView, chunks), domParser);
35
43
  }
36
44
  }
37
45
 
@@ -69,9 +77,9 @@ function combineChunks(dataView, chunks) {
69
77
  return new DataView(combinedChunks.buffer);
70
78
  }
71
79
 
72
- function readTags(tags, chunkDataView) {
80
+ function readTags(tags, chunkDataView, domParser) {
73
81
  try {
74
- const {doc, raw} = getDocument(chunkDataView);
82
+ const {doc, raw} = getDocument(chunkDataView, domParser);
75
83
  tags._raw = (tags._raw || '') + raw;
76
84
  const rdf = getRDF(doc);
77
85
 
@@ -82,19 +90,15 @@ function readTags(tags, chunkDataView) {
82
90
  }
83
91
  }
84
92
 
85
- function getDocument(chunkDataView) {
86
- const domParser = DOMParser.get();
93
+ function getDocument(chunkDataView, _domParser) {
94
+ const domParser = DOMParser.get(_domParser);
87
95
  if (!domParser) {
88
96
  console.warn('Warning: DOMParser is not available. It is needed to be able to parse XMP tags.'); // eslint-disable-line no-console
89
97
  throw new Error();
90
98
  }
91
99
 
92
100
  const xmlString = typeof chunkDataView === 'string' ? chunkDataView : getStringFromDataView(chunkDataView, 0, chunkDataView.byteLength);
93
- const doc = domParser.parseFromString(trimXmlSource(xmlString), 'application/xml');
94
-
95
- if (doc.documentElement.nodeName === 'parsererror') {
96
- throw new Error(doc.documentElement.textContent);
97
- }
101
+ const doc = parseFromString(domParser, trimXmlSource(xmlString));
98
102
 
99
103
  return {
100
104
  doc,
@@ -106,6 +110,23 @@ function trimXmlSource(xmlSource) {
106
110
  return xmlSource.replace(/^.+(<\?xpacket begin)/, '$1').replace(/(<\?xpacket end=".*"\?>).+$/, '$1');
107
111
  }
108
112
 
113
+ function parseFromString(domParser, xmlString, isRetry = false) {
114
+ try {
115
+ const doc = domParser.parseFromString(xmlString, 'application/xml');
116
+ const errors = doc.getElementsByTagName('parsererror');
117
+ if (errors.length > 0) {
118
+ throw new ParseError(errors[0].textContent);
119
+ }
120
+ return doc;
121
+ } catch (error) {
122
+ if (error.name === 'ParseError' && isMissingNamespaceError(error) && !isRetry) {
123
+ // Retry once after trying to fix the invalid XML.
124
+ return parseFromString(domParser, addMissingNamespaces(xmlString), true);
125
+ }
126
+ throw error;
127
+ }
128
+ }
129
+
109
130
  function getRDF(node) {
110
131
  for (let i = 0; i < node.childNodes.length; i++) {
111
132
  if (node.childNodes[i].tagName === 'x:xmpmeta') {