exifreader 4.35.0 → 4.36.1

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.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * ExifReader
3
3
  * http://github.com/mattiasw/exifreader
4
- * Copyright (C) 2011-2023 Mattias Wallander <mattias@wallander.eu>
4
+ * Copyright (C) 2011-2026 Mattias Wallander <mattias@wallander.eu>
5
5
  * This Source Code Form is subject to the terms of the Mozilla Public
6
6
  * License, v. 2.0. If a copy of the MPL was not distributed with this
7
7
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
@@ -31,6 +31,8 @@ import Vp8xTags from './vp8x-tags.js';
31
31
  import GifFileTags from './gif-file-tags.js';
32
32
  import Thumbnail from './thumbnail.js';
33
33
  import Composite from './composite.js';
34
+ import {createTagFilter} from './tag-filter.js';
35
+ import {buildTagsFromMergeSteps, isThenable} from './loadview-pipeline.js';
34
36
  import exifErrors from './errors.js';
35
37
 
36
38
  export default {
@@ -44,10 +46,20 @@ export const errors = exifErrors;
44
46
  export function load(data, options = {}) {
45
47
  if (isFilePathOrURL(data)) {
46
48
  options.async = true;
49
+
50
+ if (typeof Promise === 'undefined') {
51
+ throw new Error('Promise is required when async mode is enabled.');
52
+ }
53
+
47
54
  return loadFile(data, options).then((fileContents) => loadFromData(fileContents, options));
48
55
  }
49
56
  if (isBrowserFileObject(data)) {
50
57
  options.async = true;
58
+
59
+ if (typeof Promise === 'undefined') {
60
+ throw new Error('Promise is required when async mode is enabled.');
61
+ }
62
+
51
63
  return loadFileObject(data, options).then((fileContents) => loadFromData(fileContents, options));
52
64
  }
53
65
  return loadFromData(data, options);
@@ -212,17 +224,26 @@ export function loadView(
212
224
  computed = false,
213
225
  includeUnknown = false,
214
226
  domParser = undefined,
227
+ includeTags = undefined,
228
+ excludeTags = undefined,
215
229
  } = {
216
230
  expanded: false,
217
231
  async: false,
218
232
  computed: false,
219
233
  includeUnknown: false,
220
234
  domParser: undefined,
235
+ includeTags: undefined,
236
+ excludeTags: undefined,
221
237
  }
222
238
  ) {
223
- let foundMetaData = false;
224
- let tags = {};
225
- const tagsPromises = [];
239
+ const tagFilter = createTagFilter({includeTags, excludeTags});
240
+ const parsedGroups = Object.create(null);
241
+ const mergeSteps = [];
242
+ const deferredResults = Object.create(null);
243
+ const deferredPromises = [];
244
+ let pngTextIsAsync = false;
245
+ let thumbnailIfdTags = undefined;
246
+ let embeddedXmpStepForFlat = undefined;
226
247
 
227
248
  const {
228
249
  fileType,
@@ -240,342 +261,718 @@ export function loadView(
240
261
  gifHeaderOffset
241
262
  } = ImageHeader.parseAppMarkers(dataView, async);
242
263
 
243
- if (Constants.USE_JPEG && Constants.USE_FILE && hasFileData(fileDataOffset)) {
244
- foundMetaData = true;
264
+ const fileHasMetaData = hasPotentialMetaData({
265
+ fileType,
266
+ fileDataOffset,
267
+ jfifDataOffset,
268
+ tiffHeaderOffset,
269
+ iptcDataOffset,
270
+ xmpChunks,
271
+ iccChunks,
272
+ mpfDataOffset,
273
+ pngHeaderOffset,
274
+ pngTextChunks,
275
+ pngChunkOffsets,
276
+ vp8xChunkOffset,
277
+ gifHeaderOffset,
278
+ });
279
+
280
+ if (
281
+ Constants.USE_JPEG
282
+ && Constants.USE_FILE
283
+ && hasFileData(fileDataOffset)
284
+ && tagFilter.shouldParseGroup('file')
285
+ && shouldReadFileTagsForFileTypeOnlyOptimization(includeTags)
286
+ ) {
245
287
  const readTags = FileTags.read(dataView, fileDataOffset);
246
- if (expanded) {
247
- tags.file = readTags;
248
- } else {
249
- tags = objectAssign({}, tags, readTags);
288
+ const parsedFileTags = filterTagsForParse('file', readTags, tagFilter);
289
+ parsedGroups.file = parsedFileTags;
290
+
291
+ if (tagFilter.shouldReturnGroup('file')) {
292
+ mergeSteps.push({
293
+ type: 'mergeGroupAssign',
294
+ groupKey: 'file',
295
+ parsedTags: parsedFileTags,
296
+ });
250
297
  }
251
298
  }
252
299
 
253
- if (Constants.USE_JPEG && Constants.USE_JFIF && hasJfifData(jfifDataOffset)) {
254
- foundMetaData = true;
300
+ if (
301
+ Constants.USE_JPEG
302
+ && Constants.USE_JFIF
303
+ && hasJfifData(jfifDataOffset)
304
+ && tagFilter.shouldParseGroup('jfif')
305
+ ) {
255
306
  const readTags = JfifTags.read(dataView, jfifDataOffset);
256
- if (expanded) {
257
- tags.jfif = readTags;
258
- } else {
259
- tags = objectAssign({}, tags, readTags);
307
+ const parsedJfifTags = filterTagsForParse('jfif', readTags, tagFilter);
308
+ parsedGroups.jfif = parsedJfifTags;
309
+
310
+ if (tagFilter.shouldReturnGroup('jfif')) {
311
+ mergeSteps.push({
312
+ type: 'mergeGroupAssign',
313
+ groupKey: 'jfif',
314
+ parsedTags: parsedJfifTags,
315
+ });
260
316
  }
261
317
  }
262
318
 
263
- if (Constants.USE_EXIF && hasExifData(tiffHeaderOffset)) {
264
- foundMetaData = true;
319
+ if (
320
+ Constants.USE_EXIF
321
+ && hasExifData(tiffHeaderOffset)
322
+ && tagFilter.shouldParseGroup('exif')
323
+ ) {
265
324
  const {tags: readTags, byteOrder} = Tags.read(
266
325
  dataView,
267
326
  tiffHeaderOffset,
268
327
  includeUnknown,
269
- computed
328
+ computed,
329
+ tagFilter
270
330
  );
271
331
  if (readTags.Thumbnail) {
272
- tags.Thumbnail = readTags.Thumbnail;
332
+ thumbnailIfdTags = readTags.Thumbnail;
273
333
  delete readTags.Thumbnail;
274
334
  }
275
335
 
276
- if (expanded) {
277
- tags.exif = readTags;
278
- addGpsGroup(tags);
279
- } else {
280
- tags = objectAssign({}, tags, readTags);
281
- }
282
-
283
- if (Constants.USE_TIFF && Constants.USE_IPTC && readTags['IPTC-NAA'] && !hasIptcData(iptcDataOffset)) {
284
- const readIptcTags = IptcTags.read(readTags['IPTC-NAA'].value, 0, includeUnknown);
285
- if (expanded) {
286
- tags.iptc = readIptcTags;
287
- } else {
288
- tags = objectAssign({}, tags, readIptcTags);
336
+ const parsedExifTags = filterTagsForParse('exif', readTags, tagFilter);
337
+ parsedGroups.exif = parsedExifTags;
338
+
339
+ if (
340
+ Constants.USE_TIFF
341
+ && Constants.USE_IPTC
342
+ && parsedExifTags['IPTC-NAA']
343
+ && !hasIptcData(iptcDataOffset)
344
+ && tagFilter.shouldParseGroup('iptc')
345
+ ) {
346
+ const readIptcTags = IptcTags.read(
347
+ parsedExifTags['IPTC-NAA'].value,
348
+ 0,
349
+ includeUnknown,
350
+ tagFilter
351
+ );
352
+ const parsedIptcTags =
353
+ filterTagsForParse('iptc', readIptcTags, tagFilter);
354
+ parsedGroups.iptc = parsedIptcTags;
355
+
356
+ if (tagFilter.shouldReturnGroup('iptc')) {
357
+ mergeSteps.push({
358
+ type: 'mergeGroupAssign',
359
+ groupKey: 'iptc',
360
+ parsedTags: parsedIptcTags,
361
+ });
289
362
  }
290
363
  }
291
364
 
292
- if (Constants.USE_TIFF && Constants.USE_XMP && readTags['ApplicationNotes'] && !hasXmpData(xmpChunks)) {
293
- const readXmpTags = XmpTags.read(getStringValueFromArray(readTags['ApplicationNotes'].value), undefined, domParser);
294
- if (expanded) {
295
- tags.xmp = readXmpTags;
296
- } else {
297
- delete readXmpTags._raw;
298
- tags = objectAssign({}, tags, readXmpTags);
365
+ if (
366
+ Constants.USE_TIFF
367
+ && Constants.USE_XMP
368
+ && parsedExifTags['ApplicationNotes']
369
+ && !hasXmpData(xmpChunks)
370
+ && tagFilter.shouldParseGroup('xmp')
371
+ ) {
372
+ const readXmpTags = XmpTags.read(
373
+ getStringValueFromArray(parsedExifTags['ApplicationNotes'].value),
374
+ undefined,
375
+ domParser
376
+ );
377
+ const parsedXmpTags = filterTagsForParse('xmp', readXmpTags, tagFilter);
378
+ parsedGroups.xmp = parsedXmpTags;
379
+
380
+ if (tagFilter.shouldReturnGroup('xmp')) {
381
+ const step = {
382
+ type: 'mergeXmpGroupAssign',
383
+ parsedTags: parsedXmpTags,
384
+ };
385
+
386
+ if (expanded) {
387
+ mergeSteps.push(step);
388
+ } else {
389
+ embeddedXmpStepForFlat = step;
390
+ }
299
391
  }
300
392
  }
301
393
 
302
- if (Constants.USE_PHOTOSHOP && readTags['ImageSourceData'] && readTags['PhotoshopSettings']) {
303
- const readPhotoshopTags = PhotoshopTags.read(readTags['PhotoshopSettings'].value, includeUnknown);
304
- if (expanded) {
305
- tags.photoshop = readPhotoshopTags;
306
- } else {
307
- tags = objectAssign({}, tags, readPhotoshopTags);
394
+ if (
395
+ Constants.USE_PHOTOSHOP
396
+ && parsedExifTags['ImageSourceData']
397
+ && parsedExifTags['PhotoshopSettings']
398
+ && tagFilter.shouldParseGroup('photoshop')
399
+ ) {
400
+ const readPhotoshopTags = PhotoshopTags.read(
401
+ parsedExifTags['PhotoshopSettings'].value,
402
+ includeUnknown,
403
+ tagFilter
404
+ );
405
+ const parsedPhotoshopTags =
406
+ filterTagsForParse('photoshop', readPhotoshopTags, tagFilter);
407
+ parsedGroups.photoshop = parsedPhotoshopTags;
408
+
409
+ if (tagFilter.shouldReturnGroup('photoshop')) {
410
+ mergeSteps.push({
411
+ type: 'mergeGroupAssign',
412
+ groupKey: 'photoshop',
413
+ parsedTags: parsedPhotoshopTags,
414
+ });
308
415
  }
309
416
  }
310
417
 
311
- if (Constants.USE_TIFF && Constants.USE_ICC && readTags['ICC_Profile'] && !hasIccData(iccChunks)) {
418
+ if (
419
+ Constants.USE_TIFF
420
+ && Constants.USE_ICC
421
+ && parsedExifTags['ICC_Profile']
422
+ && !hasIccData(iccChunks)
423
+ && tagFilter.shouldParseGroup('icc')
424
+ ) {
312
425
  const readIccTags = IccTags.read(
313
- readTags['ICC_Profile'].value,
426
+ parsedExifTags['ICC_Profile'].value,
314
427
  [{
315
428
  offset: 0,
316
- length: readTags['ICC_Profile'].value.length,
429
+ length: parsedExifTags['ICC_Profile'].value.length,
317
430
  chunkNumber: 1,
318
431
  chunksTotal: 1
319
432
  }]
320
433
  );
321
- if (expanded) {
322
- tags.icc = readIccTags;
323
- } else {
324
- tags = objectAssign({}, tags, readIccTags);
434
+ const parsedIccTags = filterTagsForParse('icc', readIccTags, tagFilter);
435
+ parsedGroups.icc = parsedIccTags;
436
+
437
+ if (tagFilter.shouldReturnGroup('icc')) {
438
+ mergeSteps.push({
439
+ type: 'mergeGroupAssign',
440
+ groupKey: 'icc',
441
+ parsedTags: parsedIccTags,
442
+ });
325
443
  }
326
444
  }
327
445
 
328
- if (Constants.USE_MAKER_NOTES && readTags['MakerNote']) {
329
- if (hasCanonData(readTags)) {
446
+ if (
447
+ Constants.USE_MAKER_NOTES
448
+ && parsedExifTags['MakerNote']
449
+ && tagFilter.shouldParseGroup('makerNotes')
450
+ ) {
451
+ if (hasCanonData(parsedExifTags)) {
330
452
  const readCanonTags = CanonTags.read(
331
453
  dataView,
332
454
  tiffHeaderOffset,
333
- readTags['MakerNote'].__offset,
455
+ parsedExifTags['MakerNote'].__offset,
334
456
  byteOrder,
335
457
  includeUnknown,
336
- computed
458
+ computed,
459
+ tagFilter
337
460
  );
338
- if (expanded) {
339
- tags.makerNotes = readCanonTags;
340
- } else {
341
- tags = objectAssign({}, tags, readCanonTags);
461
+ parsedGroups.makerNotes = readCanonTags;
462
+ if (tagFilter.shouldReturnGroup('makerNotes')) {
463
+ mergeSteps.push({
464
+ type: 'mergeGroupAssign',
465
+ groupKey: 'makerNotes',
466
+ parsedTags: readCanonTags,
467
+ });
342
468
  }
343
- } else if (hasPentaxType1Data(readTags)) {
469
+ } else if (hasPentaxType1Data(parsedExifTags)) {
344
470
  const readPentaxTags = PentaxTags.read(
345
471
  dataView,
346
472
  tiffHeaderOffset,
347
- readTags['MakerNote'].__offset,
473
+ parsedExifTags['MakerNote'].__offset,
348
474
  includeUnknown,
349
- computed
475
+ computed,
476
+ tagFilter
350
477
  );
351
- if (expanded) {
352
- tags.makerNotes = readPentaxTags;
353
- } else {
354
- tags = objectAssign({}, tags, readPentaxTags);
478
+ parsedGroups.makerNotes = readPentaxTags;
479
+ if (tagFilter.shouldReturnGroup('makerNotes')) {
480
+ mergeSteps.push({
481
+ type: 'mergeGroupAssign',
482
+ groupKey: 'makerNotes',
483
+ parsedTags: readPentaxTags,
484
+ });
355
485
  }
356
486
  }
357
487
  }
358
488
 
359
- if (readTags['MakerNote']) {
360
- delete readTags['MakerNote'].__offset;
489
+ if (parsedExifTags['MakerNote']) {
490
+ delete parsedExifTags['MakerNote'].__offset;
491
+ }
492
+
493
+ if (tagFilter.shouldReturnGroup('exif')) {
494
+ mergeSteps.push({
495
+ type: 'mergeGroupAssign',
496
+ groupKey: 'exif',
497
+ parsedTags: parsedExifTags,
498
+ });
499
+ }
500
+
501
+ if (!expanded && embeddedXmpStepForFlat) {
502
+ mergeSteps.push(embeddedXmpStepForFlat);
503
+ embeddedXmpStepForFlat = undefined;
361
504
  }
362
505
  }
363
506
 
364
- if (Constants.USE_JPEG && Constants.USE_IPTC && hasIptcData(iptcDataOffset)) {
365
- foundMetaData = true;
366
- const readTags = IptcTags.read(dataView, iptcDataOffset, includeUnknown);
367
- if (expanded) {
368
- tags.iptc = readTags;
369
- } else {
370
- tags = objectAssign({}, tags, readTags);
507
+ if (
508
+ Constants.USE_JPEG
509
+ && Constants.USE_IPTC
510
+ && hasIptcData(iptcDataOffset)
511
+ && tagFilter.shouldParseGroup('iptc')
512
+ ) {
513
+ const readTags = IptcTags.read(dataView, iptcDataOffset, includeUnknown, tagFilter);
514
+ const parsedIptcTags = filterTagsForParse('iptc', readTags, tagFilter);
515
+ parsedGroups.iptc = parsedIptcTags;
516
+
517
+ if (tagFilter.shouldReturnGroup('iptc')) {
518
+ mergeSteps.push({
519
+ type: 'mergeGroupAssign',
520
+ groupKey: 'iptc',
521
+ parsedTags: parsedIptcTags,
522
+ });
371
523
  }
372
524
  }
373
525
 
374
- if (Constants.USE_XMP && hasXmpData(xmpChunks)) {
375
- foundMetaData = true;
526
+ if (
527
+ Constants.USE_XMP
528
+ && hasXmpData(xmpChunks)
529
+ && tagFilter.shouldParseGroup('xmp')
530
+ ) {
376
531
  const readTags = XmpTags.read(dataView, xmpChunks, domParser);
377
- if (expanded) {
378
- tags.xmp = readTags;
379
- } else {
380
- delete readTags._raw;
381
- tags = objectAssign({}, tags, readTags);
532
+ const parsedXmpTags = filterTagsForParse('xmp', readTags, tagFilter);
533
+ parsedGroups.xmp = parsedXmpTags;
534
+
535
+ if (tagFilter.shouldReturnGroup('xmp')) {
536
+ mergeSteps.push({
537
+ type: 'mergeXmpGroupAssign',
538
+ parsedTags: parsedXmpTags,
539
+ });
382
540
  }
383
541
  }
384
542
 
385
- if ((Constants.USE_JPEG || Constants.USE_WEBP) && Constants.USE_ICC && hasIccData(iccChunks)) {
386
- foundMetaData = true;
543
+ if (
544
+ (Constants.USE_JPEG || Constants.USE_WEBP)
545
+ && Constants.USE_ICC
546
+ && hasIccData(iccChunks)
547
+ && tagFilter.shouldParseGroup('icc')
548
+ ) {
387
549
  const readTags = IccTags.read(dataView, iccChunks, async);
388
- if (readTags instanceof Promise) {
389
- tagsPromises.push(readTags.then(addIccTags));
550
+ if (isThenable(readTags)) {
551
+ if (!async) {
552
+ throw new Error('Promise is required when async mode is enabled.');
553
+ }
554
+
555
+ deferredPromises.push(readTags.then((resolvedTags) => {
556
+ deferredResults.iccApp = resolvedTags;
557
+ }));
558
+ mergeSteps.push({
559
+ type: 'mergeIccDeferred',
560
+ deferredKey: 'iccApp',
561
+ });
390
562
  } else {
391
- addIccTags(readTags);
563
+ const parsedIccTags = filterTagsForParse('icc', readTags, tagFilter);
564
+ parsedGroups.icc = parsedIccTags;
565
+ if (tagFilter.shouldReturnGroup('icc')) {
566
+ mergeSteps.push({
567
+ type: 'mergeGroupAssign',
568
+ groupKey: 'icc',
569
+ parsedTags: parsedIccTags,
570
+ });
571
+ }
392
572
  }
393
573
  }
394
574
 
395
- if (Constants.USE_MPF && hasMpfData(mpfDataOffset)) {
396
- foundMetaData = true;
397
- const readMpfTags = MpfTags.read(dataView, mpfDataOffset, includeUnknown, computed);
398
- if (expanded) {
399
- tags.mpf = readMpfTags;
400
- } else {
401
- tags = objectAssign({}, tags, readMpfTags);
575
+ if (
576
+ Constants.USE_MPF
577
+ && hasMpfData(mpfDataOffset)
578
+ && tagFilter.shouldParseGroup('mpf')
579
+ ) {
580
+ const readMpfTags = MpfTags.read(
581
+ dataView,
582
+ mpfDataOffset,
583
+ includeUnknown,
584
+ computed,
585
+ tagFilter
586
+ );
587
+ const parsedMpfTags = filterTagsForParse('mpf', readMpfTags, tagFilter);
588
+ parsedGroups.mpf = parsedMpfTags;
589
+
590
+ if (tagFilter.shouldReturnGroup('mpf')) {
591
+ mergeSteps.push({
592
+ type: 'mergeGroupAssign',
593
+ groupKey: 'mpf',
594
+ parsedTags: parsedMpfTags,
595
+ });
402
596
  }
403
597
  }
404
598
 
405
- if (Constants.USE_PNG && Constants.USE_PNG_FILE && hasPngFileData(pngHeaderOffset)) {
406
- foundMetaData = true;
599
+ if (
600
+ Constants.USE_PNG
601
+ && Constants.USE_PNG_FILE
602
+ && hasPngFileData(pngHeaderOffset)
603
+ && tagFilter.shouldParseGroup('png')
604
+ ) {
407
605
  const readTags = PngFileTags.read(dataView, pngHeaderOffset);
408
- if (expanded) {
409
- tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
410
- tags.pngFile = readTags;
411
- } else {
412
- tags = objectAssign({}, tags, readTags);
606
+ const parsedPngFileTags = filterTagsForParse('png', readTags, tagFilter);
607
+ parsedGroups.pngFile = parsedPngFileTags;
608
+
609
+ if (tagFilter.shouldReturnGroup('png')) {
610
+ mergeSteps.push({
611
+ type: 'mergePngFile',
612
+ parsedTags: parsedPngFileTags,
613
+ });
413
614
  }
414
615
  }
415
616
 
416
- if (Constants.USE_PNG && hasPngTextData(pngTextChunks)) {
417
- foundMetaData = true;
418
- const {readTags, readTagsPromise} = PngTextTags.read(dataView, pngTextChunks, async, includeUnknown, computed);
419
- addPngTextTags(readTags);
617
+ if (
618
+ Constants.USE_PNG
619
+ && hasPngTextData(pngTextChunks)
620
+ && (
621
+ tagFilter.shouldParseGroup('png')
622
+ || tagFilter.shouldParseGroup('exif')
623
+ || tagFilter.shouldParseGroup('iptc')
624
+ )
625
+ ) {
626
+ const {readTags, readTagsPromise} = PngTextTags.read(
627
+ dataView,
628
+ pngTextChunks,
629
+ async,
630
+ includeUnknown,
631
+ computed,
632
+ tagFilter
633
+ );
634
+ pngTextIsAsync = !!readTagsPromise;
635
+
636
+ mergeSteps.push({
637
+ type: 'processPngTextReadTags',
638
+ readTags,
639
+ });
640
+
420
641
  if (readTagsPromise) {
421
- tagsPromises.push(readTagsPromise.then((tagList) => tagList.forEach(addPngTextTags)));
642
+ deferredPromises.push(readTagsPromise.then((tagList) => {
643
+ deferredResults.pngTextTagList = tagList;
644
+ }));
645
+ mergeSteps.push({
646
+ type: 'processPngTextReadTagsDeferredList',
647
+ deferredKey: 'pngTextTagList',
648
+ });
422
649
  }
423
650
  }
424
651
 
425
- if (Constants.USE_PNG && hasPngData(pngChunkOffsets)) {
426
- foundMetaData = true;
652
+ if (
653
+ Constants.USE_PNG
654
+ && hasPngData(pngChunkOffsets)
655
+ && tagFilter.shouldParseGroup('png')
656
+ ) {
427
657
  const readTags = PngTags.read(dataView, pngChunkOffsets);
428
- if (expanded) {
429
- tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
430
- } else {
431
- tags = objectAssign({}, tags, readTags);
658
+ const parsedPngChunkTags = filterTagsForParse('png', readTags, tagFilter);
659
+ parsedGroups.pngChunk = parsedPngChunkTags;
660
+
661
+ if (tagFilter.shouldReturnGroup('png')) {
662
+ mergeSteps.push({
663
+ type: 'mergePngChunk',
664
+ parsedTags: parsedPngChunkTags,
665
+ });
432
666
  }
433
667
  }
434
668
 
435
- if (Constants.USE_WEBP && hasVp8xData(vp8xChunkOffset)) {
436
- foundMetaData = true;
669
+ if (
670
+ Constants.USE_WEBP
671
+ && hasVp8xData(vp8xChunkOffset)
672
+ && tagFilter.shouldParseGroup('riff')
673
+ ) {
437
674
  const readTags = Vp8xTags.read(dataView, vp8xChunkOffset);
438
- if (expanded) {
439
- tags.riff = !tags.riff ? readTags : objectAssign({}, tags.riff, readTags);
440
- } else {
441
- tags = objectAssign({}, tags, readTags);
675
+ const parsedRiffTags = filterTagsForParse('riff', readTags, tagFilter);
676
+ parsedGroups.riff = parsedRiffTags;
677
+
678
+ if (tagFilter.shouldReturnGroup('riff')) {
679
+ mergeSteps.push({
680
+ type: 'mergeGroupMerge',
681
+ groupKey: 'riff',
682
+ parsedTags: parsedRiffTags,
683
+ });
442
684
  }
443
685
  }
444
686
 
445
- if (Constants.USE_GIF && hasGifFileData(gifHeaderOffset)) {
446
- foundMetaData = true;
687
+ if (
688
+ Constants.USE_GIF
689
+ && hasGifFileData(gifHeaderOffset)
690
+ && tagFilter.shouldParseGroup('gif')
691
+ ) {
447
692
  const readTags = GifFileTags.read(dataView, gifHeaderOffset);
448
- if (expanded) {
449
- tags.gif = !tags.gif ? readTags : objectAssign({}, tags.gif, readTags);
450
- } else {
451
- tags = objectAssign({}, tags, readTags);
693
+ const parsedGifTags = filterTagsForParse('gif', readTags, tagFilter);
694
+ parsedGroups.gif = parsedGifTags;
695
+
696
+ if (tagFilter.shouldReturnGroup('gif')) {
697
+ mergeSteps.push({
698
+ type: 'mergeGroupMerge',
699
+ groupKey: 'gif',
700
+ parsedTags: parsedGifTags,
701
+ });
452
702
  }
453
703
  }
454
704
 
455
- const composite = Composite.get(tags, expanded);
456
- if (composite) {
457
- if (expanded) {
458
- tags.composite = composite;
459
- } else {
460
- tags = objectAssign({}, tags, composite);
461
- }
705
+ mergeSteps.push({type: 'gps'});
706
+ mergeSteps.push({type: 'composite'});
707
+ mergeSteps.push({type: 'thumbnail'});
708
+ mergeSteps.push({type: 'fileType'});
709
+
710
+ if (!fileHasMetaData) {
711
+ throw new exifErrors.MetadataMissingError();
462
712
  }
463
713
 
464
- const thumbnail = (Constants.USE_JPEG || Constants.USE_WEBP)
465
- && Constants.USE_EXIF
466
- && Constants.USE_THUMBNAIL
467
- && Thumbnail.get(dataView, tags.Thumbnail, tiffHeaderOffset);
468
- if (thumbnail) {
469
- foundMetaData = true;
470
- tags.Thumbnail = thumbnail;
471
- } else {
472
- delete tags.Thumbnail;
714
+ const pipelineDependencies = {
715
+ objectAssign,
716
+ hasPngTextData,
717
+ filterTagsForParse,
718
+ filterTagsForReturn,
719
+ getGpsGroupFromExifTags,
720
+ Constants,
721
+ Composite,
722
+ Thumbnail,
723
+ };
724
+
725
+ if (async) {
726
+ if (typeof Promise === 'undefined') {
727
+ throw new Error('Promise is required when async mode is enabled.');
728
+ }
729
+
730
+ return Promise.all(deferredPromises).then(() => {
731
+ const tags = buildTagsFromMergeSteps({
732
+ mergeSteps,
733
+ deferredResults,
734
+ parsedGroups,
735
+ expanded,
736
+ tagFilter,
737
+ dataView,
738
+ tiffHeaderOffset,
739
+ fileType,
740
+ pngTextChunks,
741
+ pngTextIsAsync,
742
+ thumbnailIfdTags,
743
+ deps: pipelineDependencies,
744
+ });
745
+
746
+ return tags;
747
+ });
473
748
  }
474
749
 
475
- if (fileType) {
476
- if (expanded) {
477
- if (!tags.file) {
478
- tags.file = {};
479
- }
480
- tags.file.FileType = fileType;
481
- } else {
482
- tags.FileType = fileType;
750
+ return buildTagsFromMergeSteps({
751
+ mergeSteps,
752
+ deferredResults,
753
+ parsedGroups,
754
+ expanded,
755
+ tagFilter,
756
+ dataView,
757
+ tiffHeaderOffset,
758
+ fileType,
759
+ pngTextChunks,
760
+ pngTextIsAsync,
761
+ thumbnailIfdTags,
762
+ deps: pipelineDependencies,
763
+ });
764
+
765
+ function shouldReadFileTagsForFileTypeOnlyOptimization(includeTagsOptions) {
766
+ if (!includeTagsOptions) {
767
+ return true;
768
+ }
769
+
770
+ if (includeTagsOptions.composite === true) {
771
+ return true;
772
+ }
773
+
774
+ if (
775
+ Array.isArray(includeTagsOptions.composite)
776
+ && includeTagsOptions.composite.length > 0
777
+ ) {
778
+ return true;
779
+ }
780
+
781
+ if (!includeTagsOptions.file || includeTagsOptions.file === true) {
782
+ return true;
783
+ }
784
+
785
+ if (!Array.isArray(includeTagsOptions.file)) {
786
+ return true;
483
787
  }
484
- foundMetaData = true;
788
+
789
+ const isFileTypeOnly =
790
+ includeTagsOptions.file.length === 1
791
+ && includeTagsOptions.file[0] === 'FileType';
792
+
793
+ return !isFileTypeOnly;
485
794
  }
795
+ }
486
796
 
487
- if (!foundMetaData) {
488
- throw new exifErrors.MetadataMissingError();
797
+ function filterTagsForParse(groupKey, readTags, tagFilter) {
798
+ if (!tagFilter.isActive) {
799
+ return readTags;
489
800
  }
490
801
 
491
- if (async) {
492
- return Promise.all(tagsPromises).then(() => tags);
802
+ return filterTags(groupKey, readTags, tagFilter.shouldParseTag);
803
+ }
804
+
805
+ function filterTagsForReturn(groupKey, readTags, tagFilter) {
806
+ if (!tagFilter.isActive) {
807
+ return readTags;
493
808
  }
494
- return tags;
495
809
 
496
- function addIccTags(readTags) {
497
- if (expanded) {
498
- tags.icc = readTags;
499
- } else {
500
- tags = objectAssign({}, tags, readTags);
810
+ return filterTags(groupKey, readTags, tagFilter.shouldReturnTag);
811
+ }
812
+
813
+ function filterTags(groupKey, readTags, matchesTag) {
814
+ if (!readTags) {
815
+ return readTags;
816
+ }
817
+
818
+ const filteredTags = {};
819
+
820
+ for (const tagName in readTags) {
821
+ const tagValue = readTags[tagName];
822
+ const tagId = getTagId(tagValue);
823
+
824
+ if (matchesTag(groupKey, tagName, tagId)) {
825
+ filteredTags[tagName] = tagValue;
501
826
  }
502
827
  }
503
828
 
504
- function addPngTextTags(readTags) {
505
- if (expanded) {
506
- for (const group of ['exif', 'iptc']) {
507
- const groupKey = `__${group}`;
508
- if (readTags[groupKey]) {
509
- tags[group] = !tags[group] ? readTags[groupKey] : objectAssign({}, tags.exif, readTags[groupKey]);
510
- delete readTags[groupKey];
511
- }
829
+ return filteredTags;
830
+
831
+ function getTagId(tagValue) {
832
+ if (!tagValue) {
833
+ return undefined;
834
+ }
835
+
836
+ if (Array.isArray(tagValue)) {
837
+ if (tagValue.length === 0) {
838
+ return undefined;
512
839
  }
513
- tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
514
- tags.pngText = !tags.pngText ? readTags : objectAssign({}, tags.png, readTags);
515
- } else {
516
- tags = objectAssign(
517
- {},
518
- tags,
519
- readTags.__exif ? readTags.__exif : {},
520
- readTags.__iptc ? readTags.__iptc : {},
521
- readTags
522
- );
523
- delete tags.__exif;
524
- delete tags.__iptc;
840
+
841
+ return tagValue[0].id;
525
842
  }
526
- }
527
- }
528
843
 
529
- function hasFileData(fileDataOffset) {
530
- return fileDataOffset !== undefined;
844
+ return tagValue.id;
845
+ }
531
846
  }
532
847
 
533
- function hasJfifData(jfifDataOffset) {
534
- return jfifDataOffset !== undefined;
848
+ function hasPotentialMetaData({
849
+ fileType,
850
+ fileDataOffset,
851
+ jfifDataOffset,
852
+ tiffHeaderOffset,
853
+ iptcDataOffset,
854
+ xmpChunks,
855
+ iccChunks,
856
+ mpfDataOffset,
857
+ pngHeaderOffset,
858
+ pngTextChunks,
859
+ pngChunkOffsets,
860
+ vp8xChunkOffset,
861
+ gifHeaderOffset,
862
+ }) {
863
+ return !!fileType
864
+ || (
865
+ Constants.USE_JPEG
866
+ && Constants.USE_FILE
867
+ && hasFileData(fileDataOffset)
868
+ )
869
+ || (
870
+ Constants.USE_JPEG
871
+ && Constants.USE_JFIF
872
+ && hasJfifData(jfifDataOffset)
873
+ )
874
+ || (
875
+ Constants.USE_EXIF
876
+ && hasExifData(tiffHeaderOffset)
877
+ )
878
+ || (
879
+ Constants.USE_JPEG
880
+ && Constants.USE_IPTC
881
+ && hasIptcData(iptcDataOffset)
882
+ )
883
+ || (
884
+ Constants.USE_XMP
885
+ && hasXmpData(xmpChunks)
886
+ )
887
+ || (
888
+ (Constants.USE_JPEG || Constants.USE_WEBP)
889
+ && Constants.USE_ICC
890
+ && hasIccData(iccChunks)
891
+ )
892
+ || (
893
+ Constants.USE_MPF
894
+ && hasMpfData(mpfDataOffset)
895
+ )
896
+ || (
897
+ Constants.USE_PNG
898
+ && Constants.USE_PNG_FILE
899
+ && hasPngFileData(pngHeaderOffset)
900
+ )
901
+ || (
902
+ Constants.USE_PNG
903
+ && hasPngTextData(pngTextChunks)
904
+ )
905
+ || (
906
+ Constants.USE_PNG
907
+ && hasPngData(pngChunkOffsets)
908
+ )
909
+ || (
910
+ Constants.USE_WEBP
911
+ && hasVp8xData(vp8xChunkOffset)
912
+ )
913
+ || (
914
+ Constants.USE_GIF
915
+ && hasGifFileData(gifHeaderOffset)
916
+ );
535
917
  }
536
918
 
537
- function hasExifData(tiffHeaderOffset) {
538
- return tiffHeaderOffset !== undefined;
539
- }
919
+ function getGpsGroupFromExifTags(exifTags) {
920
+ let gps = undefined;
540
921
 
541
- function addGpsGroup(tags) {
542
- if (tags.exif) {
543
- if (tags.exif.GPSLatitude && tags.exif.GPSLatitudeRef) {
544
- try {
545
- tags.gps = tags.gps || {};
546
- tags.gps.Latitude = getCalculatedGpsValue(tags.exif.GPSLatitude.value);
547
- if (tags.exif.GPSLatitudeRef.value.join('') === 'S') {
548
- tags.gps.Latitude = -tags.gps.Latitude;
549
- }
550
- } catch (error) {
551
- // Ignore.
922
+ if (exifTags.GPSLatitude && exifTags.GPSLatitudeRef) {
923
+ gps = gps || {};
924
+ try {
925
+ gps.Latitude = getCalculatedGpsValue(exifTags.GPSLatitude.value);
926
+ if (exifTags.GPSLatitudeRef.value.join('') === 'S') {
927
+ gps.Latitude = -gps.Latitude;
552
928
  }
929
+ } catch (error) {
930
+ // Ignore.
553
931
  }
932
+ }
554
933
 
555
- if (tags.exif.GPSLongitude && tags.exif.GPSLongitudeRef) {
556
- try {
557
- tags.gps = tags.gps || {};
558
- tags.gps.Longitude = getCalculatedGpsValue(tags.exif.GPSLongitude.value);
559
- if (tags.exif.GPSLongitudeRef.value.join('') === 'W') {
560
- tags.gps.Longitude = -tags.gps.Longitude;
561
- }
562
- } catch (error) {
563
- // Ignore.
934
+ if (exifTags.GPSLongitude && exifTags.GPSLongitudeRef) {
935
+ gps = gps || {};
936
+ try {
937
+ gps.Longitude = getCalculatedGpsValue(exifTags.GPSLongitude.value);
938
+ if (exifTags.GPSLongitudeRef.value.join('') === 'W') {
939
+ gps.Longitude = -gps.Longitude;
564
940
  }
941
+ } catch (error) {
942
+ // Ignore.
565
943
  }
944
+ }
566
945
 
567
- if (tags.exif.GPSAltitude && tags.exif.GPSAltitudeRef) {
568
- try {
569
- tags.gps = tags.gps || {};
570
- tags.gps.Altitude = tags.exif.GPSAltitude.value[0] / tags.exif.GPSAltitude.value[1];
571
- if (tags.exif.GPSAltitudeRef.value === 1) {
572
- tags.gps.Altitude = -tags.gps.Altitude;
573
- }
574
- } catch (error) {
575
- // Ignore.
946
+ if (exifTags.GPSAltitude && exifTags.GPSAltitudeRef) {
947
+ gps = gps || {};
948
+ try {
949
+ gps.Altitude =
950
+ exifTags.GPSAltitude.value[0] / exifTags.GPSAltitude.value[1];
951
+ if (exifTags.GPSAltitudeRef.value === 1) {
952
+ gps.Altitude = -gps.Altitude;
576
953
  }
954
+ } catch (error) {
955
+ // Ignore.
577
956
  }
578
957
  }
958
+
959
+ if (!gps) {
960
+ return undefined;
961
+ }
962
+
963
+ return gps;
964
+ }
965
+
966
+ function hasFileData(fileDataOffset) {
967
+ return fileDataOffset !== undefined;
968
+ }
969
+
970
+ function hasJfifData(jfifDataOffset) {
971
+ return jfifDataOffset !== undefined;
972
+ }
973
+
974
+ function hasExifData(tiffHeaderOffset) {
975
+ return tiffHeaderOffset !== undefined;
579
976
  }
580
977
 
581
978
  function hasIptcData(iptcDataOffset) {
@@ -611,7 +1008,7 @@ function hasPngFileData(pngFileDataOffset) {
611
1008
  }
612
1009
 
613
1010
  function hasPngTextData(pngTextChunks) {
614
- return pngTextChunks !== undefined;
1011
+ return Array.isArray(pngTextChunks) && pngTextChunks.length > 0;
615
1012
  }
616
1013
 
617
1014
  function hasPngData(pngChunkOffsets) {