exifreader 4.52.0 → 4.54.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.
@@ -4,47 +4,110 @@
4
4
 
5
5
  import Constants from './constants.js';
6
6
 
7
- // The order the main Exif block reads them in. It is the key order of expanded
8
- // output, and in flat output later groups win a key clash, so Exif wins over
9
- // all of them but an ApplicationNotes XMP packet.
10
- export const EXIF_CARRIED_GROUP_ORDER = ['iptc', 'xmp', 'photoshop', 'icc', 'makerNotes'];
11
-
12
- export function buildTagsFromMergeSteps({
13
- mergeSteps,
14
- deferredResults,
7
+ // A block's position in each mode is the index of its slot. Expanded output
8
+ // takes its key order from the first block of each group: discovery order,
9
+ // with the groups an Exif block carries just before it. In flat output a later
10
+ // block wins a key clash, so Exif wins over the groups it carries except an
11
+ // ApplicationNotes XMP packet.
12
+ export const MERGE_ORDER = {
13
+ expanded: [
14
+ 'file', 'jfif',
15
+ 'carriedIptc', 'carriedXmp', 'carriedPhotoshop', 'carriedIcc', 'carriedMakerNotes', 'exif',
16
+ 'iptc', 'xmp', 'brobExif', 'brobXmp', 'icc', 'mpf',
17
+ 'pngFile', 'pngText', 'deferredPngText', 'pngChunk', 'riff', 'gif', 'jxlFile', 'isoBmffFile',
18
+ 'gps', 'composite', 'thumbnail', 'fileType', 'metadataRange',
19
+ ],
20
+ flat: [
21
+ 'file', 'jfif',
22
+ 'carriedIptc', 'carriedPhotoshop', 'carriedIcc', 'carriedMakerNotes', 'exif', 'carriedXmp',
23
+ 'iptc', 'xmp', 'brobExif', 'brobXmp', 'icc', 'mpf',
24
+ 'pngFile', 'pngText', 'deferredPngText', 'pngChunk', 'riff', 'gif', 'jxlFile', 'isoBmffFile',
25
+ 'gps', 'composite', 'thumbnail', 'fileType', 'metadataRange',
26
+ ],
27
+ };
28
+
29
+ // Every Constants.USE_* guard sits inside the function, since the table is
30
+ // built once at import.
31
+ const MERGE_MODES = {
32
+ assign: assignGroupTags,
33
+ merge: mergeGroupTags,
34
+ brobXmp: assignBrobXmpTags,
35
+ pngFile: mergePngFileTags,
36
+ pngTextTags: mergePngTextTags,
37
+ nested: mergeNestedBlocks,
38
+ thumbnailCandidate: addThumbnailCandidate,
39
+ gps: addGpsGroup,
40
+ composite: addCompositeGroup,
41
+ thumbnail: addThumbnail,
42
+ fileType: addFileType,
43
+ metadataRange: addMetadataRange,
44
+ };
45
+
46
+ /**
47
+ * Merges the blocks into the returned tags, in the order MERGE_ORDER gives the
48
+ * current mode, whatever order they were pushed in.
49
+ *
50
+ * A block has a `slot` (its place in MERGE_ORDER) and a `mode` (a key of
51
+ * MERGE_MODES). A tag block also has the `group` it merges into and its parsed
52
+ * `tags`; a `nested` container has `blocks`, merged among themselves in slot
53
+ * order at the container's position; a `thumbnailCandidate` has `rank` (main
54
+ * or embedded) and the thumbnail as `tags`. A metadataRange block carries
55
+ * `metadataBlocks` and `metadataTruncated`.
56
+ *
57
+ * The top level merges in place, so a PNG with thousands of text chunk blocks
58
+ * stays linear.
59
+ *
60
+ * @returns {object} The tags.
61
+ */
62
+ export function buildTagsFromMergeBlocks({
63
+ mergeBlocks,
15
64
  parsedGroups,
16
65
  expanded,
17
66
  tagFilter,
18
67
  dataView,
19
- tiffHeaderOffset,
20
- exifDataView,
21
68
  fileType,
22
- pngTextChunks,
69
+ pngTextBlocks,
23
70
  pngTextIsAsync,
24
- thumbnailIfdTags,
25
71
  deps,
26
72
  }) {
73
+ const context = {
74
+ parsedGroups,
75
+ expanded,
76
+ tagFilter,
77
+ dataView,
78
+ fileType,
79
+ thumbnails: [],
80
+ deps,
81
+ merge: createInPlaceMerge(deps),
82
+ };
83
+ const orderedBlocks = getBlocksInMergeOrder(mergeBlocks, expanded);
27
84
  let tags = {};
28
- const embeddedExifThumbnails = [];
29
-
30
- for (let i = 0; i < mergeSteps.length; i++) {
31
- tags = applyMergeStep({
32
- step: mergeSteps[i],
33
- deferredResults,
34
- parsedGroups,
35
- expanded,
36
- tagFilter,
37
- dataView,
38
- tiffHeaderOffset,
39
- exifDataView,
40
- fileType,
41
- thumbnailIfdTags,
42
- embeddedExifThumbnails,
43
- tags,
44
- deps,
45
- });
85
+
86
+ for (let i = 0; i < orderedBlocks.length; i++) {
87
+ tags = applyMergeMode(tags, orderedBlocks[i], context);
46
88
  }
47
89
 
90
+ return addPngTextCopyOfPng(tags, {expanded, tagFilter, pngTextBlocks, pngTextIsAsync, deps});
91
+ }
92
+
93
+ // Sorts by position, then push order, without relying on a stable sort.
94
+ function getBlocksInMergeOrder(blocks, expanded) {
95
+ const order = expanded ? MERGE_ORDER.expanded : MERGE_ORDER.flat;
96
+
97
+ return blocks
98
+ .map((block, index) => ({block, index, position: order.indexOf(block.slot)}))
99
+ .sort((a, b) => (a.position - b.position) || (a.index - b.index))
100
+ .map((entry) => entry.block);
101
+ }
102
+
103
+ function applyMergeMode(tags, block, context) {
104
+ return MERGE_MODES[block.mode](tags, block, context);
105
+ }
106
+
107
+ // Expanded output gets a pngText group that copies png when the text chunks
108
+ // are read asynchronously, or when there are text chunks (raw profiles
109
+ // included) but none of them gave PNG text tags.
110
+ function addPngTextCopyOfPng(tags, {expanded, tagFilter, pngTextBlocks, pngTextIsAsync, deps}) {
48
111
  if (
49
112
  Constants.USE_PNG
50
113
  && expanded
@@ -59,7 +122,7 @@ export function buildTagsFromMergeSteps({
59
122
  Constants.USE_PNG
60
123
  && expanded
61
124
  && tagFilter.shouldReturnGroup('png')
62
- && deps.hasPngTextData(pngTextChunks)
125
+ && deps.hasPngTextData(pngTextBlocks)
63
126
  && tags.png
64
127
  && !tags.pngText
65
128
  ) {
@@ -69,506 +132,282 @@ export function buildTagsFromMergeSteps({
69
132
  return tags;
70
133
  }
71
134
 
72
- export function applyMergeStep({
73
- step,
74
- deferredResults,
135
+ /**
136
+ * Merges one block into the tags with the function its mode names.
137
+ *
138
+ * @param {object} context The block, the tags so far and the merge state; see
139
+ * buildTagsFromMergeBlocks for the block shape. `merge` defaults to a copying
140
+ * merge and `thumbnails` to a new list.
141
+ * @returns {object} The tags, which flat output replaces rather than mutates.
142
+ */
143
+ export function mergeBlock({
144
+ block,
145
+ tags,
75
146
  parsedGroups,
76
147
  expanded,
77
148
  tagFilter,
78
149
  dataView,
79
- tiffHeaderOffset,
80
- exifDataView,
81
150
  fileType,
82
- thumbnailIfdTags,
83
- embeddedExifThumbnails = [],
84
- tags,
151
+ thumbnails = [],
85
152
  deps,
153
+ merge = createCopyingMerge(deps),
86
154
  }) {
87
- if (step.type === 'mergeGroupAssign') {
88
- const returnedTags =
89
- deps.filterTagsForReturn(step.groupKey, step.parsedTags, tagFilter);
155
+ return applyMergeMode(tags, block, {
156
+ parsedGroups,
157
+ expanded,
158
+ tagFilter,
159
+ dataView,
160
+ fileType,
161
+ thumbnails,
162
+ deps,
163
+ merge,
164
+ });
165
+ }
90
166
 
91
- return mergeAssignGroup(tags, step.groupKey, returnedTags, expanded, deps);
167
+ // Every tag block writes its parsed group as it merges, since gps, composite
168
+ // and metadataRange read them, whether or not the group is returned.
169
+ function assignGroupTags(tags, block, context) {
170
+ context.parsedGroups[block.group] = block.tags;
171
+ if (!context.tagFilter.shouldReturnGroup(block.group)) {
172
+ return tags;
92
173
  }
174
+ const returnedTags = context.deps.filterTagsForReturn(block.group, block.tags, context.tagFilter);
93
175
 
94
- if (step.type === 'mergeGroupMerge') {
95
- const returnedTags =
96
- deps.filterTagsForReturn(step.groupKey, step.parsedTags, tagFilter);
97
-
98
- return mergeMergeGroup(tags, step.groupKey, returnedTags, expanded, deps);
99
- }
176
+ return mergeAssignGroup(tags, block.group, returnedTags, context.expanded, context.deps);
177
+ }
100
178
 
101
- if (Constants.USE_XMP && step.type === 'mergeXmpGroupAssign') {
102
- const returnedTags =
103
- deps.filterTagsForReturn('xmp', step.parsedTags, tagFilter);
179
+ export function mergeAssignGroup(tags, groupKey, returnedTags, expanded, deps) {
180
+ if (expanded) {
181
+ tags[groupKey] = returnedTags;
104
182
 
105
- return mergeXmpGroup(tags, returnedTags, expanded, deps);
183
+ return tags;
106
184
  }
107
185
 
108
- if (Constants.USE_ICC && step.type === 'mergeIccDeferred') {
109
- const resolvedReadTags = deferredResults[step.deferredKey];
110
- const parsedIccTags =
111
- deps.filterTagsForParse('icc', resolvedReadTags, tagFilter);
112
- parsedGroups.icc = parsedIccTags;
113
-
114
- if (!tagFilter.shouldReturnGroup('icc')) {
115
- return tags;
116
- }
117
-
118
- const returnedIccTags =
119
- deps.filterTagsForReturn('icc', parsedIccTags, tagFilter);
186
+ return deps.objectAssign({}, tags, getTagsForFlat(groupKey, returnedTags, deps));
187
+ }
120
188
 
121
- return mergeAssignGroup(tags, 'icc', returnedIccTags, expanded, deps);
189
+ // Every XMP block drops the raw packet in flat output.
190
+ function getTagsForFlat(groupKey, returnedTags, deps) {
191
+ if (!(Constants.USE_XMP && groupKey === 'xmp')) {
192
+ return returnedTags;
122
193
  }
194
+ const xmpTagsWithoutRaw = deps.objectAssign({}, returnedTags);
195
+ delete xmpTagsWithoutRaw._raw;
123
196
 
124
- if (Constants.USE_JXL && step.type === 'mergeBrobExifDeferred') {
125
- const resolved = deferredResults[step.deferredKey];
126
- if (!resolved) {
127
- return tags;
128
- }
129
- addEmbeddedExifThumbnail(embeddedExifThumbnails, resolved.thumbnail);
130
- if (Object.keys(resolved.exifTags).length === 0) {
131
- return tags;
132
- }
133
-
134
- return addExifAndCarriedGroupsToTagsAndGroups({
135
- exifTags: resolved.exifTags,
136
- carriedGroups: resolved.carriedGroups,
137
- parsedGroups,
138
- expanded,
139
- tagFilter,
140
- tags,
141
- deps,
142
- merge: createCopyingMerge(deps),
143
- });
144
- }
145
-
146
- if (Constants.USE_JXL && step.type === 'mergeBrobXmpDeferred') {
147
- const resolvedReadTags = deferredResults[step.deferredKey];
148
- if (!resolvedReadTags || Object.keys(resolvedReadTags).length === 0) {
149
- return tags;
150
- }
151
- const parsedXmpTags =
152
- deps.filterTagsForParse('xmp', resolvedReadTags, tagFilter);
153
- parsedGroups.xmp = parsedXmpTags;
154
-
155
- if (!tagFilter.shouldReturnGroup('xmp')) {
156
- return tags;
157
- }
158
-
159
- const returnedTags =
160
- deps.filterTagsForReturn('xmp', parsedXmpTags, tagFilter);
197
+ return xmpTagsWithoutRaw;
198
+ }
161
199
 
162
- return mergeXmpGroup(tags, returnedTags, expanded, deps);
200
+ function mergeGroupTags(tags, block, context) {
201
+ context.merge.group(context.parsedGroups, block.group, block.tags);
202
+ if (!context.tagFilter.shouldReturnGroup(block.group)) {
203
+ return tags;
163
204
  }
205
+ const returnedTags = context.deps.filterTagsForReturn(block.group, block.tags, context.tagFilter);
164
206
 
165
- if (Constants.USE_PNG && step.type === 'mergePngFile') {
166
- const returnedPngFileTags =
167
- deps.filterTagsForReturn('png', step.parsedTags, tagFilter);
168
-
169
- if (!tagFilter.shouldReturnGroup('png')) {
170
- return tags;
171
- }
172
-
173
- if (expanded) {
174
- tags.png = !tags.png ? returnedPngFileTags : deps.objectAssign(
175
- {},
176
- tags.png,
177
- returnedPngFileTags
178
- );
179
- tags.pngFile = returnedPngFileTags;
207
+ if (context.expanded) {
208
+ context.merge.group(tags, block.group, returnedTags);
180
209
 
181
- return tags;
182
- }
183
-
184
- return deps.objectAssign({}, tags, returnedPngFileTags);
210
+ return tags;
185
211
  }
186
212
 
187
- if (Constants.USE_PNG && step.type === 'mergePngChunk') {
188
- const returnedPngChunkTags =
189
- deps.filterTagsForReturn('png', step.parsedTags, tagFilter);
190
-
191
- if (!tagFilter.shouldReturnGroup('png')) {
192
- return tags;
193
- }
194
-
195
- if (expanded) {
196
- tags.png = !tags.png ? returnedPngChunkTags : deps.objectAssign(
197
- {},
198
- tags.png,
199
- returnedPngChunkTags
200
- );
201
-
202
- return tags;
203
- }
204
-
205
- return deps.objectAssign({}, tags, returnedPngChunkTags);
206
- }
207
-
208
- if (Constants.USE_PNG && step.type === 'processPngTextReadTags') {
209
- addEmbeddedExifThumbnail(embeddedExifThumbnails, step.embeddedExifThumbnail);
210
- return addPngTextReadTagsToTagsAndGroups({
211
- readTags: step.readTags,
212
- embeddedExifTags: step.embeddedExifTags,
213
- exifCarriedGroups: step.exifCarriedGroups,
214
- embeddedIptcTags: step.embeddedIptcTags,
215
- parsedGroups,
216
- expanded,
217
- tagFilter,
218
- tags,
219
- deps,
220
- });
221
- }
222
-
223
- if (Constants.USE_PNG && step.type === 'processPngTextReadTagsDeferredList') {
224
- const tagList = deferredResults[step.deferredKey] || [];
225
- const merge = createInPlaceMerge(deps);
226
-
227
- for (let i = 0; i < tagList.length; i++) {
228
- const entry = tagList[i];
229
- addEmbeddedExifThumbnail(embeddedExifThumbnails, entry.embeddedExifThumbnail);
230
- tags = addPngTextReadTagsToTagsAndGroups({
231
- readTags: entry.readTags || {},
232
- embeddedExifTags: entry.embeddedExifTags,
233
- exifCarriedGroups: entry.exifCarriedGroups,
234
- embeddedIptcTags: entry.embeddedIptcTags,
235
- parsedGroups,
236
- expanded,
237
- tagFilter,
238
- tags,
239
- deps,
240
- merge,
241
- });
242
- }
213
+ return context.merge.topLevel(tags, getTagsForFlat(block.group, returnedTags, context.deps));
214
+ }
243
215
 
216
+ // In expanded output a brob XMP replaces the xmp group, in flat output it
217
+ // merges key by key, and an empty, failed or over-bound one leaves the earlier
218
+ // xmp group as it is.
219
+ function assignBrobXmpTags(tags, block, context) {
220
+ if (!Constants.USE_JXL) {
244
221
  return tags;
245
222
  }
246
-
247
- if (Constants.USE_EXIF && step.type === 'gps') {
248
- if (
249
- expanded
250
- && tagFilter.shouldReturnGroup('gps')
251
- && parsedGroups.exif
252
- ) {
253
- const gpsGroup = deps.getGpsGroupFromExifTags(parsedGroups.exif);
254
- if (gpsGroup) {
255
- const returnedGpsGroup =
256
- deps.filterTagsForReturn('gps', gpsGroup, tagFilter);
257
- tags.gps = returnedGpsGroup;
258
- }
259
- }
260
-
223
+ if (!block.tags || Object.keys(block.tags).length === 0) {
261
224
  return tags;
262
225
  }
263
226
 
264
- if ((Constants.USE_EXIF || Constants.USE_XMP) && step.type === 'composite') {
265
- if (!tagFilter.shouldReturnGroup('composite')) {
266
- return tags;
267
- }
268
-
269
- let compositeInput = tags;
270
- let compositeInputExpanded = expanded;
271
- if (tagFilter.isActive) {
272
- compositeInput = {exif: parsedGroups.exif, file: parsedGroups.file};
273
- compositeInputExpanded = true;
274
- }
227
+ return assignGroupTags(tags, block, context);
228
+ }
275
229
 
276
- const composite = deps.Composite.get(compositeInput, compositeInputExpanded);
277
- if (!composite) {
278
- return tags;
279
- }
230
+ // The png group gathers the file, text and chunk tags; pngFile keeps the file
231
+ // tags alone.
232
+ function mergePngFileTags(tags, block, context) {
233
+ if (!Constants.USE_PNG) {
234
+ return tags;
235
+ }
236
+ context.parsedGroups.pngFile = block.tags;
237
+ if (!context.tagFilter.shouldReturnGroup('png')) {
238
+ return tags;
239
+ }
240
+ const returnedTags = context.deps.filterTagsForReturn('png', block.tags, context.tagFilter);
280
241
 
281
- const returnedCompositeTags =
282
- deps.filterTagsForReturn('composite', composite, tagFilter);
242
+ if (context.expanded) {
243
+ context.merge.group(tags, 'png', returnedTags);
244
+ tags.pngFile = returnedTags;
283
245
 
284
- return mergeAssignGroup(
285
- tags,
286
- 'composite',
287
- returnedCompositeTags,
288
- expanded,
289
- deps
290
- );
246
+ return tags;
291
247
  }
292
248
 
293
- if (step.type === 'thumbnail') {
294
- // Flat output can already hold an XMP or PNG text tag named Thumbnail.
295
- delete tags.Thumbnail;
249
+ return context.merge.topLevel(tags, returnedTags);
250
+ }
296
251
 
297
- if (
298
- !tagFilter.shouldReturnGroup('thumbnail')
299
- || !tagFilter.shouldReturnTag('thumbnail', 'Thumbnail')
300
- ) {
301
- return tags;
302
- }
252
+ // The blocks of one container (an Exif block with the groups it carries, or
253
+ // the uncompressed text chunks of a PNG) merge among themselves in slot order.
254
+ function mergeNestedBlocks(tags, block, context) {
255
+ return mergeEmbeddedBlocks(tags, block.blocks, context);
256
+ }
303
257
 
304
- const mainThumbnail = getMainThumbnail({
305
- thumbnailIfdTags,
306
- tagFilter,
307
- dataView,
308
- exifDataView,
309
- tiffHeaderOffset,
310
- deps,
311
- });
312
- let thumbnail = mainThumbnail;
313
- if (!hasImage(mainThumbnail)) {
314
- thumbnail = getFirstEmbeddedExifThumbnailWithImage(embeddedExifThumbnails) || mainThumbnail;
315
- }
316
- if (thumbnail) {
317
- tags.Thumbnail = thumbnail;
318
- }
258
+ function mergeEmbeddedBlocks(tags, blocks, context) {
259
+ const orderedBlocks = getBlocksInMergeOrder(blocks, context.expanded);
319
260
 
320
- return tags;
261
+ for (let i = 0; i < orderedBlocks.length; i++) {
262
+ tags = applyMergeMode(tags, orderedBlocks[i], context);
321
263
  }
322
264
 
323
- if (step.type === 'metadataRange') {
324
- if (!expanded) {
325
- return tags;
326
- }
327
- const metadataRange = buildMetadataRange(
328
- step.metadataBlocks,
329
- step.metadataTruncated,
330
- dataView,
331
- parsedGroups
332
- );
333
- if (metadataRange) {
334
- tags.metadataRange = metadataRange;
335
- }
265
+ return tags;
266
+ }
267
+
268
+ // pngText holds the text tags only when there are any; otherwise
269
+ // addPngTextCopyOfPng may fill it with a copy of png.
270
+ function mergePngTextTags(tags, block, context) {
271
+ if (!Constants.USE_PNG || !context.tagFilter.shouldReturnGroup('png')) {
336
272
  return tags;
337
273
  }
274
+ const returnedTags = context.deps.filterTagsForReturn('png', block.tags, context.tagFilter);
338
275
 
339
- if (step.type === 'fileType') {
340
- // Flat output can already hold an XMP or PNG text tag named FileType.
341
- delete tags.FileType;
342
-
343
- if (
344
- fileType
345
- && tagFilter.shouldReturnGroup('file')
346
- && tagFilter.shouldReturnTag('file', 'FileType')
347
- ) {
348
- if (expanded) {
349
- if (!tags.file) {
350
- tags.file = {};
351
- }
352
- tags.file.FileType = fileType;
353
- } else {
354
- tags.FileType = fileType;
355
- }
276
+ if (context.expanded) {
277
+ context.merge.group(tags, 'png', returnedTags);
278
+ if (Object.keys(returnedTags).length > 0) {
279
+ context.merge.group(tags, 'pngText', returnedTags);
356
280
  }
357
281
 
358
282
  return tags;
359
283
  }
360
284
 
361
- return tags;
285
+ return context.merge.topLevel(tags, returnedTags);
362
286
  }
363
287
 
364
- function addEmbeddedExifThumbnail(embeddedExifThumbnails, embeddedExifThumbnail) {
365
- if (embeddedExifThumbnail) {
366
- embeddedExifThumbnails.push(embeddedExifThumbnail);
367
- }
288
+ function addThumbnailCandidate(tags, block, context) {
289
+ context.thumbnails.push({rank: block.rank, thumbnail: block.tags});
290
+ return tags;
368
291
  }
369
292
 
370
- function getMainThumbnail({thumbnailIfdTags, tagFilter, dataView, exifDataView, tiffHeaderOffset, deps}) {
371
- if (!thumbnailIfdTags) {
372
- return undefined;
293
+ // Expanded output only, derived from the parsed Exif group.
294
+ function addGpsGroup(tags, block, context) {
295
+ if (
296
+ Constants.USE_EXIF
297
+ && context.expanded
298
+ && context.tagFilter.shouldReturnGroup('gps')
299
+ && context.parsedGroups.exif
300
+ ) {
301
+ const gpsGroup = context.deps.getGpsGroupFromExifTags(context.parsedGroups.exif);
302
+ if (gpsGroup) {
303
+ tags.gps = context.deps.filterTagsForReturn('gps', gpsGroup, context.tagFilter);
304
+ }
373
305
  }
374
306
 
375
- const parsedThumbnailIfdTags = deps.filterTagsForParse(
376
- 'thumbnail',
377
- thumbnailIfdTags,
378
- tagFilter
379
- );
380
-
381
- return Constants.USE_EXIF
382
- && Constants.USE_THUMBNAIL
383
- && deps.Thumbnail.get(exifDataView || dataView, parsedThumbnailIfdTags, tiffHeaderOffset);
384
- }
385
-
386
- function hasImage(thumbnail) {
387
- return !!thumbnail && !!thumbnail.image;
307
+ return tags;
388
308
  }
389
309
 
390
- function getFirstEmbeddedExifThumbnailWithImage(embeddedExifThumbnails) {
391
- if (!((Constants.USE_PNG || Constants.USE_JXL) && Constants.USE_EXIF && Constants.USE_THUMBNAIL)) {
392
- return undefined;
310
+ // Composite reads the merged tags, or with an active tag filter the parsed exif
311
+ // and file groups.
312
+ function addCompositeGroup(tags, block, context) {
313
+ if (!(Constants.USE_EXIF || Constants.USE_XMP) || !context.tagFilter.shouldReturnGroup('composite')) {
314
+ return tags;
393
315
  }
394
- return embeddedExifThumbnails.filter(hasImage)[0];
395
- }
396
316
 
397
- export function mergeAssignGroup(tags, groupKey, returnedTags, expanded, deps) {
398
- if (expanded) {
399
- tags[groupKey] = returnedTags;
317
+ let compositeInput = tags;
318
+ let compositeInputExpanded = context.expanded;
319
+ if (context.tagFilter.isActive) {
320
+ compositeInput = {exif: context.parsedGroups.exif, file: context.parsedGroups.file};
321
+ compositeInputExpanded = true;
322
+ }
400
323
 
324
+ const composite = context.deps.Composite.get(compositeInput, compositeInputExpanded);
325
+ if (!composite) {
401
326
  return tags;
402
327
  }
403
328
 
404
- return deps.objectAssign({}, tags, returnedTags);
329
+ const returnedCompositeTags = context.deps.filterTagsForReturn('composite', composite, context.tagFilter);
330
+
331
+ return mergeAssignGroup(tags, 'composite', returnedCompositeTags, context.expanded, context.deps);
405
332
  }
406
333
 
407
- function mergeXmpGroup(tags, returnedTags, expanded, deps) {
408
- if (expanded) {
409
- tags.xmp = returnedTags;
334
+ // The main thumbnail when it has an image, else the first embedded Exif
335
+ // thumbnail with one, else the main thumbnail without an image.
336
+ function addThumbnail(tags, block, context) {
337
+ // Flat output can already hold an XMP or PNG text tag named Thumbnail.
338
+ delete tags.Thumbnail;
410
339
 
340
+ if (
341
+ !context.tagFilter.shouldReturnGroup('thumbnail')
342
+ || !context.tagFilter.shouldReturnTag('thumbnail', 'Thumbnail')
343
+ ) {
411
344
  return tags;
412
345
  }
413
346
 
414
- const returnedTagsForFlat = deps.objectAssign({}, returnedTags);
415
- delete returnedTagsForFlat._raw;
347
+ const thumbnail = selectThumbnail(context.thumbnails);
348
+ if (thumbnail) {
349
+ tags.Thumbnail = thumbnail;
350
+ }
416
351
 
417
- return deps.objectAssign({}, tags, returnedTagsForFlat);
352
+ return tags;
418
353
  }
419
354
 
420
- export function mergeMergeGroup(tags, groupKey, returnedTags, expanded, deps) {
421
- if (expanded) {
422
- if (!tags[groupKey]) {
423
- tags[groupKey] = returnedTags;
424
- } else {
425
- tags[groupKey] = deps.objectAssign({}, tags[groupKey], returnedTags);
426
- }
427
-
428
- return tags;
355
+ function selectThumbnail(candidates) {
356
+ if (!(Constants.USE_EXIF && Constants.USE_THUMBNAIL)) {
357
+ return undefined;
429
358
  }
359
+ const main = candidates.filter((candidate) => candidate.rank === 'main')[0];
360
+ if (main && hasImage(main.thumbnail)) {
361
+ return main.thumbnail;
362
+ }
363
+ const embedded = candidates.filter((candidate) => candidate.rank === 'embedded' && hasImage(candidate.thumbnail))[0];
364
+ if (embedded) {
365
+ return embedded.thumbnail;
366
+ }
367
+ return main ? main.thumbnail : undefined;
368
+ }
430
369
 
431
- return deps.objectAssign({}, tags, returnedTags);
370
+ function hasImage(thumbnail) {
371
+ return !!thumbnail && !!thumbnail.image;
432
372
  }
433
373
 
434
- export function addPngTextReadTagsToTagsAndGroups({
435
- readTags,
436
- embeddedExifTags,
437
- exifCarriedGroups,
438
- embeddedIptcTags,
439
- parsedGroups,
440
- expanded,
441
- tagFilter,
442
- tags,
443
- deps,
444
- merge = createCopyingMerge(deps),
445
- }) {
446
- if (embeddedExifTags) {
447
- tags = addExifAndCarriedGroupsToTagsAndGroups({
448
- exifTags: embeddedExifTags,
449
- carriedGroups: exifCarriedGroups || {},
450
- parsedGroups,
451
- expanded,
452
- tagFilter,
453
- tags,
454
- deps,
455
- merge,
456
- });
457
- }
458
-
459
- if (embeddedIptcTags) {
460
- const parsedEmbeddedIptcTags =
461
- deps.filterTagsForParse('iptc', embeddedIptcTags, tagFilter);
462
- merge.group(parsedGroups, 'iptc', parsedEmbeddedIptcTags);
463
-
464
- if (tagFilter.shouldReturnGroup('iptc')) {
465
- const returnedEmbeddedIptcTags = deps.filterTagsForReturn(
466
- 'iptc',
467
- parsedEmbeddedIptcTags,
468
- tagFilter
469
- );
470
- if (expanded) {
471
- merge.group(tags, 'iptc', returnedEmbeddedIptcTags);
472
- } else {
473
- tags = merge.topLevel(tags, returnedEmbeddedIptcTags);
474
- }
475
- }
476
- }
374
+ function addFileType(tags, block, context) {
375
+ // Flat output can already hold an XMP or PNG text tag named FileType.
376
+ delete tags.FileType;
477
377
 
478
- if (tagFilter.shouldReturnGroup('png')) {
479
- const parsedPngTextTags = deps.filterTagsForParse('png', readTags, tagFilter);
480
- const returnedPngTextTags =
481
- deps.filterTagsForReturn('png', parsedPngTextTags, tagFilter);
482
- parsedGroups.pngText = parsedPngTextTags;
483
-
484
- if (expanded) {
485
- merge.group(tags, 'png', returnedPngTextTags);
486
- // Historical behavior in build fixtures:
487
- // - if PNG text chunks yield actual "png" tags, `pngText` should contain only those
488
- // - otherwise (e.g. only embedded Exif/IPTC), `pngText` should represent the full `png` group
489
- // and will be filled in later as a fallback from `tags.png`
490
- if (returnedPngTextTags && Object.keys(returnedPngTextTags).length > 0) {
491
- merge.group(tags, 'pngText', returnedPngTextTags);
378
+ if (
379
+ context.fileType
380
+ && context.tagFilter.shouldReturnGroup('file')
381
+ && context.tagFilter.shouldReturnTag('file', 'FileType')
382
+ ) {
383
+ if (context.expanded) {
384
+ if (!tags.file) {
385
+ tags.file = {};
492
386
  }
387
+ tags.file.FileType = context.fileType;
493
388
  } else {
494
- tags = merge.topLevel(tags, returnedPngTextTags);
389
+ tags.FileType = context.fileType;
495
390
  }
496
391
  }
497
392
 
498
393
  return tags;
499
394
  }
500
395
 
501
- /**
502
- * Merges one Exif block and the groups it carries in the order the main Exif
503
- * block merges them: the carried groups, then Exif, then in flat output the
504
- * ApplicationNotes XMP. The carried groups arrive parsed and are not filtered
505
- * for parse again.
506
- *
507
- * @returns {object} The tags, which flat output replaces rather than mutates.
508
- */
509
- export function addExifAndCarriedGroupsToTagsAndGroups({
510
- exifTags,
511
- carriedGroups,
512
- parsedGroups,
513
- expanded,
514
- tagFilter,
515
- tags,
516
- deps,
517
- merge,
518
- }) {
519
- let flatXmpGroup = undefined;
520
-
521
- for (let i = 0; i < EXIF_CARRIED_GROUP_ORDER.length; i++) {
522
- const groupKey = EXIF_CARRIED_GROUP_ORDER[i];
523
- const group = carriedGroups[groupKey];
524
- if (group === undefined) {
525
- continue;
526
- }
527
- if (groupKey === 'xmp' && !expanded) {
528
- flatXmpGroup = group;
529
- continue;
530
- }
531
- tags = addCarriedGroup(groupKey, group);
532
- }
533
-
534
- if (exifTags) {
535
- const parsedExifTags = deps.filterTagsForParse('exif', exifTags, tagFilter);
536
- merge.group(parsedGroups, 'exif', parsedExifTags);
537
-
538
- if (tagFilter.shouldReturnGroup('exif')) {
539
- const returnedExifTags = deps.filterTagsForReturn('exif', parsedExifTags, tagFilter);
540
- if (expanded) {
541
- merge.group(tags, 'exif', returnedExifTags);
542
- } else {
543
- tags = merge.topLevel(tags, returnedExifTags);
544
- }
545
- }
396
+ // Expanded output only.
397
+ function addMetadataRange(tags, block, context) {
398
+ if (!context.expanded) {
399
+ return tags;
546
400
  }
547
-
548
- if (flatXmpGroup !== undefined) {
549
- tags = addCarriedGroup('xmp', flatXmpGroup, true);
401
+ const metadataRange = buildMetadataRange(
402
+ block.metadataBlocks,
403
+ block.metadataTruncated,
404
+ context.dataView,
405
+ context.parsedGroups
406
+ );
407
+ if (metadataRange) {
408
+ tags.metadataRange = metadataRange;
550
409
  }
551
-
552
410
  return tags;
553
-
554
- function addCarriedGroup(groupKey, group, isFlatXmp) {
555
- merge.group(parsedGroups, groupKey, group);
556
- if (!tagFilter.shouldReturnGroup(groupKey)) {
557
- return tags;
558
- }
559
- const returnedTags = deps.filterTagsForReturn(groupKey, group, tagFilter);
560
- if (expanded) {
561
- merge.group(tags, groupKey, returnedTags);
562
- return tags;
563
- }
564
- return merge.topLevel(tags, isFlatXmp ? getXmpTagsWithoutRaw(returnedTags) : returnedTags);
565
- }
566
-
567
- function getXmpTagsWithoutRaw(xmpTags) {
568
- const xmpTagsWithoutRaw = deps.objectAssign({}, xmpTags);
569
- delete xmpTagsWithoutRaw._raw;
570
- return xmpTagsWithoutRaw;
571
- }
572
411
  }
573
412
 
574
413
  function createCopyingMerge(deps) {
@@ -595,7 +434,7 @@ function createInPlaceMerge(deps) {
595
434
  },
596
435
  };
597
436
 
598
- // Earlier steps can share these containers (tags.png can be tags.pngFile or the
437
+ // Earlier blocks can share these containers (tags.png can be tags.pngFile or the
599
438
  // synchronous text chunks' tags), so each is copied once before merging in place.
600
439
  function own(container) {
601
440
  if (ownedContainers.indexOf(container) !== -1) {