Repository navigation
Expand file tree
/
Copy pathfiles_core.cpp
More file actions
3589 lines (3103 loc) · 125 KB
/
Copy pathfiles_core.cpp
File metadata and controls
3589 lines (3103 loc) · 125 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This program is free software; you can redistribute it and / or modify it
// under the terms of the LGPL License either version 2.1 or later.
// License details are available at https://www.gnu.org/licenses/lgpl-2.1.html
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
// Purpose: Core file handling operations. Manages file loading, saving, and format detection
// for images and media files. Coordinates metadata extraction across different file types.
#include "pch.h"
#include "app_text.h"
#include "files.h"
#include "metadata_exif.h"
#include "metadata_xmp.h"
#include "av_format.h"
#include "metadata_icc.h"
#include "metadata_iptc.h"
#define LIBARCHIVE_STATIC
#include <archive.h>
#include <archive_entry.h>
// windows.h arrives through the vendored headers above and brings the min/max function macros with
// it, which swallow the std:: calls further down this file. Both go, not just min.
#undef GetObject
#undef min
#undef max
df_assert_move_only(file_scan_result);
df_assert_move_only(metadata_parts);
df_assert_movable(metadata_kv_list);
df_assert_movable(file_load_result);
std::string file_group::display_name(const bool is_plural) const
{
const auto result = is_plural && !str::is_empty(plural_name) ? plural_name : name;
return tt.translate_text(std::string(result));
}
ui::const_surface_ptr file_type::default_thumbnail() const
{
return platform::create_icon_surface(static_cast<char32_t>(group->icon));
}
struct file_type_config
{
// Replaced whole, never edited in place. parse_file_group reads it on workers - auto-complete and
// query - while a language switch registers that language's spellings on the UI thread, and an
// insert that rehashed under a lookup left the reader walking freed buckets.
std::atomic<std::shared_ptr<const file_group_by_name>> groups_by_name{
std::make_shared<const file_group_by_name>()
};
file_type_by_extension types_by_name;
std::vector<file_group_ref> groups;
std::vector<file_type> types;
};
static file_type_config s_config;
bool file_tool::invoke(const df::file_path path) const
{
if (exe_path.is_empty()) return false;
auto substitute = [&](std::ostringstream& result, const std::string_view token)
{
if (token == "item-path") result << str::quote_if_white_space(path.str());
else if (token == "exe-path") result << str::quote_if_white_space(exe_path.str());
};
return platform::run(exe_path, replace_tokens(invoke_text, substitute));
}
file_group_ref file_group_from_index(const int from_id)
{
return s_config.groups[from_id];
}
file_group_ref parse_file_group(const std::string& text)
{
const auto groups = s_config.groups_by_name.load();
if (!groups) return nullptr;
const auto found = groups->find(text);
return found != groups->end() ? found->second : nullptr;
}
// `@photo` is not the only spelling that reaches parse_file_group: format_term writes the group's
// translated display name, and the search vocabulary offers the plural. A term that cannot be read
// back is dropped silently, so an address box the user can read has to be one the parser accepts.
//
// They are registered here rather than compared per call because parse_file_group runs on the
// auto-complete worker once per candidate per keystroke, and display_name reads the translation
// catalog, which the language switch replaces wholesale. A canonical name always wins a collision.
// The table is copied, extended and published whole, because that worker may be reading it.
void register_file_group_spellings()
{
auto groups = std::make_shared<file_group_by_name>(*s_config.groups_by_name.load());
for (const auto g : s_config.groups)
{
const auto add = [g, &groups](const std::string_view name)
{
if (!str::is_empty(name)) groups->try_emplace(str::cache(name), g);
};
add(g->plural_name);
add(g->display_name(false));
add(g->display_name(true));
}
s_config.groups_by_name.store(std::shared_ptr<const file_group_by_name>(std::move(groups)));
}
static constexpr file_traits photo_traits = file_traits::bitmap | file_traits::cache_metadata |
file_traits::zoom | file_traits::hide_overlays |
file_traits::thumbnail | file_traits::photo_metadata;
static constexpr file_traits video_traits = file_traits::av | file_traits::preview_video |
file_traits::cache_metadata | file_traits::hide_overlays |
file_traits::thumbnail | file_traits::video_metadata;
static constexpr file_traits audio_traits = file_traits::av | file_traits::visualize_audio |
file_traits::cache_metadata | file_traits::music_metadata;
static constexpr file_traits commodore_traits = file_traits::commodore | file_traits::no_metadata_grouping;
static constexpr file_traits archive_traits = file_traits::archive | file_traits::no_metadata_grouping;
file_group file_group::other("other", "others", 0x5E5E5E, icon_index::document,
file_traits::no_metadata_grouping, group_key_type::other, {});
file_group file_group::folder("folder", "folders", 0x18A59C, icon_index::folder,
file_traits::no_metadata_grouping, group_key_type::folder, {});
file_group file_group::photo("photo", "photos", 0x18A549, icon_index::photo, photo_traits,
group_key_type::photo, {"xmp"});
file_group file_group::video("video", "videos", 0xA55018, icon_index::video, video_traits,
group_key_type::video,
{"srt", "smi", "vtt", "mpl2", "thm", "xmp"});
file_group file_group::audio("audio", {}, 0xA5184B, icon_index::audio, audio_traits, group_key_type::audio, {});
file_group file_group::archive("archive", "archives", 0x5588DD, icon_index::archive, archive_traits,
group_key_type::archive, {});
file_group file_group::commodore("commodore", {}, 0xFF8811, icon_index::retro, commodore_traits,
group_key_type::retro, {});
file_type file_type::folder(file_group::folder, {}, {}, {});
file_type file_type::other(file_group::other, {}, {}, {});
void load_file_types()
{
s_config.groups = {
file_group::folder,
file_group::photo,
file_group::video,
file_group::audio,
file_group::archive,
file_group::commodore,
file_group::other
};
s_config.types = {
{file_group::video, "264", {}, {}},
{file_group::video, "265", {}, {}},
{file_group::video, "302", {}, {}},
{file_group::photo, "3fr", "Hasselblad raw", file_traits::raw | file_traits::edit},
{file_group::audio, "669", {}, {}},
{file_group::video, "722", {}, {}},
{file_group::video, "A64", {}, {}},
{file_group::video, "aa", {}, {}},
{file_group::video, "aa3", {}, {}},
{file_group::audio, "aax", "Audible Audiobook", {}},
{file_group::audio, "aac,adt,adts", "Advanced Audio Coding", {}},
{file_group::video, "ac3", "Dolby Digital (AC-3)", {}},
{file_group::video, "ac4", "Dolby AC-4", {}},
{file_group::video, "acm", {}, {}},
{file_group::video, "adf", {}, {}},
{file_group::video, "adp", {}, {}},
{file_group::video, "ads", {}, {}},
{file_group::video, "adx", {}, {}},
{file_group::video, "aea", {}, {}},
{file_group::audio, "afc", {}, {}},
{file_group::audio, "aif, aifc, aiff", "Audio Interchange File Format", file_traits::embedded_xmp},
{file_group::audio, "au,snd", "Sun Microsystems and NeXT audio", {}},
{file_group::video, "aix", {}, {}},
{file_group::audio, "amf", {}, {}},
{file_group::video, "amr", "Adaptive Multi-Rate audio", {}},
{file_group::audio, "ams", {}, {}},
{file_group::audio, "ape", "Monkey's Audio (APE)", {}},
{file_group::video, "apl", {}, {}},
{file_group::video, "apng", {}, {}},
{file_group::video, "apv", "Advanced Professional Video", {}},
{file_group::video, "aptx", {}, {}},
{file_group::video, "aptxhd", {}, {}},
{file_group::video, "aqt", {}, {}},
{file_group::photo, "arw,arq,sr2,srf", "Sony raw", file_traits::raw | file_traits::edit},
{file_group::photo, "bay", "Casio raw", file_traits::raw | file_traits::edit},
{file_group::photo, "cap", "Phase One raw", file_traits::raw | file_traits::edit},
{
file_group::video, "asf", "Windows Media",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{file_group::video, "ass", {}, {}},
{file_group::video, "ast", "Audio Stream", {}},
{
file_group::video, "avi", "Audio Visual Interleave",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{file_group::video, "avr", {}, {}},
{file_group::video, "avs", {}, {}},
{file_group::video, "avs2", {}, {}},
{file_group::video, "bcstm", {}, {}},
{file_group::video, "bfstm", {}, {}},
still_file_type(detected_format::BMP),
{file_group::video, "bmv", {}, {}},
{file_group::video, "brstm", "Binary Revolution Stream", {}},
{file_group::video, "caf", {}, {}},
{file_group::video, "cavs", {}, {}},
{file_group::video, "cdata", {}, {}},
{file_group::video, "cdg", {}, {}},
{file_group::video, "cif", {}, {}},
{file_group::video, "cpk", {}, {}},
{file_group::photo, "crw,cr2,cr3", "Canon raw", file_traits::raw | file_traits::edit},
{file_group::photo, "cs1,sti,ia", "Sinar raw", file_traits::raw | file_traits::edit},
{file_group::video, "cdxl", "Commodore CDXL video", {}},
{file_group::video, "daud", {}, {}},
{file_group::video, "dav", {}, {}},
{file_group::audio, "dbm", {}, {}},
{file_group::audio, "cda", "CD Audio Track", {}},
{file_group::photo, "dc2,dcr,dcs,drf,k25,kc2,kdc", "Kodak raw", file_traits::raw | file_traits::edit},
{file_group::audio, "dff", {}, {}},
{file_group::audio, "digi", {}, {}},
{file_group::video, "divx", "DivX video", {}},
{file_group::audio, "dmf", {}, {}},
{
file_group::photo, "dng", "Adobe Digital Negative",
file_traits::raw | file_traits::edit | file_traits::embedded_xmp | file_traits::in_place_metadata
},
{file_group::video, "dnxhd", {}, {}},
{file_group::video, "dnxhr", {}, {}},
{file_group::video, "drc", {}, {}},
{file_group::audio, "dsf", {}, {}},
{file_group::audio, "dsm", {}, {}},
{file_group::video, "dss", {}, {}},
{file_group::video, "dtk", {}, {}},
{file_group::audio, "dtm", {}, {}},
{file_group::video, "dts", "DTS (sound system)", {}},
{file_group::video, "dtshd", "DTS (sound system)", {}},
{file_group::video, "dv,dif", {}, {}},
{file_group::video, "dvd", {}, {}},
{file_group::video, "eac3,ec3", "Dolby Digital Plus (E-AC-3)", {}},
{file_group::photo, "erf", "Epson raw", file_traits::raw | file_traits::edit},
{file_group::photo, "mfw", "Mamiya raw", file_traits::raw | file_traits::edit},
{file_group::photo, "raw", "Panasonic or Leica raw", file_traits::raw | file_traits::edit},
{file_group::photo, "exr", {}, {}},
{file_group::video, "fap", {}, {}},
{file_group::audio, "far", {}, {}},
{file_group::photo, "fff", "Hasselblad or Imacon raw", file_traits::raw | file_traits::edit},
{file_group::video, "fits", {}, {}},
{file_group::audio, "flac", "Free Lossless Audio", {}},
{file_group::video, "flm", {}, {}},
{
file_group::video, "flv", "Flash video",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{file_group::video, "fsb", {}, {}},
{file_group::video, "fwse", {}, {}},
{file_group::video, "g722", {}, {}},
{file_group::video, "g723_1", {}, {}},
{file_group::video, "g728", {}, {}},
{file_group::video, "g729", {}, {}},
{file_group::audio, "gdm", {}, {}},
{file_group::video, "genh", {}, {}},
still_file_type(detected_format::GIF),
{file_group::video, "gsm", "GSM Full Rate", {}},
{file_group::video, "gxf", {}, {}},
{file_group::video, "h261", "H.261 video", {}},
{file_group::video, "h263", "H.263 video", {}},
{file_group::video, "h264", "H.264 / AVC video", {}},
{file_group::video, "h265", "H.265 / HEVC video", {}},
{file_group::video, "hca", {}, {}},
{file_group::photo, "hdr", {}, {}},
{file_group::video, "hevc", "H.265 / HEVC video", {}},
{file_group::video, "evc", "MPEG-5 EVC", {}},
{file_group::audio, "ice", {}, {}},
{file_group::photo, "ico", "Microsoft Windows icon", {}},
{file_group::audio, "id3", {}, {}},
{file_group::video, "idf", {}, {}},
{file_group::photo, "iff", "ILBM", {}},
{file_group::video, "ifv", {}, {}},
{file_group::photo, "iiq", "Phase One raw", file_traits::raw | file_traits::edit},
{file_group::audio, "imf", {}, {}},
{file_group::video, "ircam", {}, {}},
{file_group::video, "ism", {}, {}},
{file_group::video, "isma", {}, {}},
{file_group::video, "ismv", {}, {}},
{file_group::audio, "it", {}, {}},
{file_group::video, "ivf", "Indeo Video Technology", {}},
{file_group::video, "ivr", {}, {}},
{file_group::audio, "j2b", {}, {}},
{file_group::photo, "j2c", {}, {}},
{file_group::photo, "j2k", {}, {}},
{file_group::photo, "jif", {}, {}},
{file_group::video, "jls", {}, {}},
{file_group::photo, "jp2", "JPEG2000", {}},
still_file_type(detected_format::JPEG),
// Its metadata goes to a sidecar: nothing Diffractor uses writes a JPEG 2000 packet, and its own
// scan would not read one back.
{file_group::photo, "jpx", "Jpeg 2000", file_traits::edit},
still_file_type(detected_format::JXL),
{file_group::video, "jss", {}, {}},
{file_group::photo, "koa", {}, {}},
{file_group::video, "kux", {}, {}},
{file_group::video, "latm", {}, {}},
{file_group::video, "lbc", {}, {}},
{file_group::photo, "lbm", "ILBM", {}},
{file_group::video, "ljpg", {}, {}},
{file_group::video, "loas", {}, {}},
{file_group::video, "lrc", {}, {}},
{file_group::video, "lrv", {}, {}},
{file_group::video, "lvf", {}, {}},
{file_group::audio, "m15", {}, {}},
{file_group::audio, "mid,midi,rmi", "Musical Instrument Digital Interface", {}},
{file_group::video, "m2p", {}, {}},
{file_group::video, "m2t", {}, {}},
{file_group::video, "m2ts", "MPEG-2 TS video", {}},
{file_group::video, "mac", {}, {}},
{file_group::photo, "mdc,mrw", "Minolta raw", file_traits::raw | file_traits::edit},
{file_group::audio, "mdl", {}, {}},
{file_group::audio, "med", {}, {}},
{file_group::photo, "mef", "Mamiya raw", file_traits::raw | file_traits::edit},
{file_group::video, "mj2", {}, {}},
{file_group::video, "mjpeg", {}, {}},
{file_group::video, "mjpg", {}, {}},
{file_group::video, "mk3d", {}, {}},
{file_group::audio, "mka", {}, {}},
{file_group::video, "mks", {}, {}},
{file_group::video, "mkv", "Matroska video", {}},
{file_group::video, "mlp", {}, {}},
{file_group::audio, "mmcmp", {}, {}},
{file_group::video, "mmf", {}, {}},
{file_group::audio, "mms", {}, {}},
{file_group::photo, "mng", "Multiple Network Graphics animation", {}},
{file_group::audio, "mo3", {}, {}},
{file_group::audio, "mod", {}, {}},
{file_group::photo, "mos", "Leaf raw", file_traits::raw | file_traits::edit},
{
file_group::audio, "mp3", "MPEG Layer 3",
file_traits::embedded_xmp | file_traits::edit | file_traits::thumbnail | file_traits::in_place_metadata
},
{
file_group::video, "mov", {},
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{
file_group::video, "mp4,mp4a,mp4v,m4v,m4b,f4v,3g2,3gp2,3gp,3gpp,crm", "MPEG-4",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{
file_group::video, "crm", "Canon Cinema RAW Light",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{
file_group::audio, "m4a,mp4a,m4r", "MPEG-4 Audio",
file_traits::embedded_xmp | file_traits::edit | file_traits::thumbnail |
file_traits::in_place_metadata
},
{
file_group::audio, "m4p", "MPEG-4 (DRM)",
file_traits::embedded_xmp | file_traits::edit | file_traits::thumbnail |
file_traits::in_place_metadata
},
{file_group::audio, "mpc", "Musepack", {}},
{file_group::video, "mpd", {}, {}},
{
file_group::video, "mpeg,mpg,mpe,m1v,m2v,mp2,mpv,m2p,m2t,vob,ms-pvr,dvr-ms", "MPEG",
file_traits::edit
},
{
file_group::audio, "mpa,m2a", "MPEG",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
still_file_type(detected_format::HEIF),
{file_group::video, "avc1", "Advanced Video Coding", {}},
{file_group::audio, "mptm", {}, {}},
{file_group::video, "msbc", {}, {}},
{file_group::video, "msf", {}, {}},
{file_group::audio, "mt2", {}, {}},
{file_group::video, "mtaf", {}, {}},
{file_group::audio, "mtm", {}, {}},
{file_group::video, "mts", "AVCHD video", {}},
{file_group::video, "musx", {}, {}},
{file_group::video, "mvi", {}, {}},
{file_group::video, "mxf", "SMPTE Material Exchange Format", {}},
{file_group::video, "mxg", {}, {}},
{file_group::photo, "nef,nrw", "Nikon raw", file_traits::raw | file_traits::edit},
{file_group::video, "nist", {}, {}},
{file_group::video, "nsp", {}, {}},
{file_group::audio, "nst", {}, {}},
{file_group::video, "nut", {}, {}},
{file_group::video, "obu", "AV1 video stream", {}},
{file_group::audio, "oga", "Ogg audio", {}},
{file_group::audio, "ogg", "container, multimedia", {}},
{file_group::video, "ogm", {}, {}},
{file_group::video, "ogv", {}, {}},
{file_group::video, "ogx", {}, {}},
{file_group::audio, "okt", {}, {}},
{file_group::video, "oma", {}, {}},
{file_group::video, "omg", {}, {}},
{file_group::audio, "opus", "Opus audio", {}},
{file_group::audio, "qoa", "Quite OK Audio", {}},
{file_group::audio, "iamf", "Immersive Audio Model and Formats", {}},
{file_group::audio, "lc3", "Low Complexity Communication Codec", {}},
{file_group::photo, "orf,ori", "Olympus raw", file_traits::raw | file_traits::edit},
{file_group::video, "paf", {}, {}},
{file_group::video, "pam", {}, {}},
{file_group::photo, "pbm", "Portable bitmap", {}},
{file_group::photo, "pcd", {}, {}},
{file_group::photo, "pct", "Apple Macintosh PICT image", {}},
{file_group::photo, "pcx", "ZSoft's PC Paint image", {}},
{file_group::photo, "pef,ptx", "Pentax raw", file_traits::raw | file_traits::edit},
{file_group::photo, "pfm", {}, {}},
{file_group::photo, "pgm", "Portable graymap", {}},
{file_group::video, "pgmyuv", {}, {}},
{file_group::photo, "pic", {}, {}},
{file_group::photo, "pict", "Apple Macintosh PICT image", {}},
{file_group::video, "pjs", {}, {}},
{file_group::audio, "plm", {}, {}},
still_file_type(detected_format::PNG),
{file_group::photo, "ppm", "Portable Pixmap", {}},
still_file_type(detected_format::PSD),
{file_group::audio, "psm", {}, {}},
{file_group::video, "psp", "Paint Shop Pro image", {}},
{file_group::audio, "pt36", {}, {}},
{file_group::audio, "ptm", {}, {}},
{file_group::video, "pvf", {}, {}},
{file_group::photo, "pxn", "Logitech raw", file_traits::raw | file_traits::edit},
{file_group::video, "qcif", {}, {}},
{file_group::photo, "qtk", "Apple Quicktake raw", file_traits::raw | file_traits::edit},
{file_group::audio, "ra, rm", "RealAudio", {}},
{file_group::photo, "raf", "Fuji raw", file_traits::raw | file_traits::edit},
{file_group::photo, "ras", {}, {}},
{file_group::video, "rco", {}, {}},
{file_group::video, "rcv", {}, {}},
{file_group::video, "rmd,r3d", "RED", {}},
{file_group::video, "rgb", "Silicon Graphics Image", {}},
{file_group::video, "rm", "RealAudio (RA, RM)", {}},
{file_group::video, "roq", "Quake 3 video", {}},
{file_group::video, "rsd", {}, {}},
{file_group::video, "rso", {}, {}},
{file_group::video, "rt", {}, {}},
{file_group::photo, "rw2", "Panasonic raw", file_traits::raw | file_traits::edit},
{file_group::photo, "rwl", "Leica raw", file_traits::raw | file_traits::edit},
{file_group::audio, "s3m", {}, {}},
{file_group::video, "sami", {}, {}},
{file_group::video, "sbc", {}, {}},
{file_group::video, "sbg", {}, {}},
{file_group::video, "scc", {}, {}},
{file_group::video, "sdr2", {}, {}},
{file_group::video, "sds", {}, {}},
{file_group::video, "sdx", {}, {}},
{file_group::video, "ser", {}, {}},
{file_group::video, "sf", {}, {}},
{file_group::audio, "sfx", {}, {}},
{file_group::audio, "sfx2", {}, {}},
{file_group::photo, "sgi", "Silicon Graphics Image", {}},
{file_group::video, "shn", "Shorten (SHN)", {}},
{file_group::video, "son", {}, {}},
{file_group::video, "sox", {}, {}},
{file_group::video, "spdif", {}, {}},
{file_group::video, "sph", {}, {}},
{file_group::audio, "spx", "Speex low bitrate audio", {}},
{file_group::photo, "srw", "Samsung raw", file_traits::raw | file_traits::edit},
{file_group::video, "ss2", {}, {}},
{file_group::video, "ssa", {}, {}},
{file_group::audio, "st26", {}, {}},
{file_group::audio, "stk", {}, {}},
{file_group::video, "stl", {}, {}},
{file_group::audio, "stm", {}, {}},
{file_group::audio, "stp", {}, {}},
{file_group::video, "str", {}, {}},
{file_group::video, "sub", {}, {}},
{file_group::video, "sup", {}, {}},
{file_group::video, "ag", {}, {}},
{file_group::video, "swf", "Macromedia Flash", file_traits::embedded_xmp},
{file_group::video, "tak", "Tom's Lossless Audio Kompressor", {}},
{file_group::photo, "tga,targa", "Truevision TGA (Targa) image", {}},
{file_group::video, "tco", {}, {}},
{file_group::video, "thd", {}, {}},
still_file_type(detected_format::TIFF),
{file_group::photo, "cin", "Kodak Cineon Image", {}},
{file_group::photo, "dpx", "Digital Picture Exchange", {}},
{file_group::video, "tod", {}, {}},
{file_group::video, "ts", "MPEG-2 Transport Stream", {}},
{file_group::audio, "tta", "True Audio", {}},
{file_group::video, "ty", {}, {}},
{file_group::video, "ty+", {}, {}},
{file_group::audio, "ult", {}, {}},
{file_group::audio, "umx", {}, {}},
{file_group::video, "v210", {}, {}},
{file_group::video, "vag", {}, {}},
{file_group::video, "vc1", "SMPTE VC-1 video", {}},
{file_group::video, "vc2", {}, {}},
{file_group::video, "viv", {}, {}},
{file_group::video, "voc", "Creative Labs Soundblaster", {}},
{file_group::video, "vpk", {}, {}},
{file_group::video, "vqe", {}, {}},
{file_group::video, "vqf", "Yamaha TwinVQ", {}},
{file_group::video, "vql", {}, {}},
{file_group::video, "w64", {}, {}},
{file_group::photo, "wap", {}, {}},
{
file_group::audio, "wav", "Microsoft Wave",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{file_group::photo, "wbm", {}, {}},
// WBMP has no metadata container, so its metadata goes to a sidecar.
{file_group::photo, "wbmp", {}, file_traits::edit},
{file_group::video, "web", {}, {}},
{file_group::video, "webm", "WebM video", {}},
still_file_type(detected_format::WEBP),
{
file_group::audio, "wma", "Windows Media Audio 9",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{
file_group::video, "wmv,wm", "Windows Media video",
file_traits::embedded_xmp | file_traits::edit | file_traits::in_place_metadata
},
{file_group::audio, "wow", {}, {}},
{file_group::video, "wsd", {}, {}},
{file_group::video, "wtv", {}, {}},
{file_group::audio, "wv", "WavPack", {}},
{file_group::photo, "x3f", "Sigma raw", file_traits::raw | file_traits::edit},
{file_group::photo, "xbm", "X Window System Bitmap", {}},
{file_group::video, "xl", {}, {}},
{file_group::audio, "xm", {}, {}},
{file_group::video, "xmv", {}, {}},
{file_group::audio, "xpk", {}, {}},
{file_group::photo, "xpm", "X Window System Pixmap", {}},
{file_group::video, "xvag", {}, {}},
{file_group::audio, "xx", {}, {}},
{file_group::video, "y4m", {}, {}},
{file_group::video, "yop", {}, {}},
{file_group::video, "yuv", "Raw YUV video format", {}},
{file_group::video, "yuv10", {}, {}},
{file_group::archive, "zip", {}, {file_traits::archive}},
{file_group::archive, "rar", {}, {file_traits::archive}},
{file_group::archive, "7z", {}, {file_traits::archive}},
{file_group::archive, "gz", {}, {file_traits::archive}},
{file_group::archive, "tgz", {}, {file_traits::archive}},
{file_group::archive, "cpio", {}, {file_traits::archive}},
{file_group::archive, "iso", {}, {file_traits::archive}},
{file_group::archive, "cab", {}, {file_traits::archive}},
{file_group::archive, "pax", {}, {file_traits::archive}},
{file_group::archive, "lzip", {}, {file_traits::archive}},
{file_group::archive, "lza", {}, {file_traits::archive}},
{file_group::archive, "bzip2,bz2", {}, {file_traits::archive}},
{file_group::archive, "tar", {}, {file_traits::archive}},
{file_group::archive, "lha", {}, {file_traits::archive}},
{file_group::archive, "a,ar", {}, {file_traits::archive}},
{file_group::commodore, "d64", {}, {file_traits::disk_image | file_traits::commodore}},
{file_group::commodore, "d71", {}, {file_traits::disk_image | file_traits::commodore}},
{file_group::commodore, "d81", {}, {file_traits::disk_image | file_traits::commodore}},
{file_group::commodore, "t64", {}, {file_traits::commodore}},
{file_group::commodore, "crt", {}, {file_traits::commodore}},
{file_group::commodore, "prg", {}, {file_traits::commodore}},
{file_group::commodore, "p00", {}, {file_traits::commodore}},
};
int next_id = 0;
auto canonical = std::make_shared<file_group_by_name>();
for (const auto g : s_config.groups)
{
(*canonical)[g->name] = g;
g->id = next_id;
next_id += 1;
}
s_config.groups_by_name.store(std::shared_ptr<const file_group_by_name>(std::move(canonical)));
// The plural and the current-language spellings, so the address box round trips before any
// catalog is loaded. Loading one calls this again.
register_file_group_spellings();
for (auto& ft : s_config.types)
{
auto& sidecars = ft.sidecars;
sidecars.insert(sidecars.end(), ft.group->sidecars.begin(), ft.group->sidecars.end());
std::ranges::sort(sidecars);
sidecars.erase(std::ranges::unique(sidecars).begin(), sidecars.end());
if (ft.icon == icon_index::document)
{
ft.icon = ft.group->icon;
}
if (!ft.extension.empty())
{
str::split2(ft.extension, true, [pft = &ft](const std::string_view ext)
{
s_config.types_by_name.insert_or_assign(str::cache(ext), pft);
});
}
}
av_initialise();
}
static std::vector<file_map_link> s_map_links;
namespace
{
// Tools sit a few levels below the configured roots (vendor\product\app.exe). Capping the walk
// keeps startup bounded on machines with large Program Files trees and stops a directory
// junction from recursing forever.
constexpr int max_tool_scan_depth = 4;
using exe_path_by_name = df::hash_map<std::string_view, df::file_path, df::ihash, df::ieq>;
// Only the executables named in the config are retained, so memory stays proportional to the
// config and not to the number of programs installed.
void find_executables(const df::folder_path folder, const int depth,
const df::hash_set<std::string_view, df::ihash, df::ieq>& wanted,
exe_path_by_name& found)
{
if (found.size() >= wanted.size()) return;
const auto contents = platform::iterate_file_items(folder, true);
for (const auto& f : contents.files)
{
const auto path = folder.combine_file(f.name);
if (str::icmp(path.extension(), ".exe") == 0)
{
const auto match = wanted.find(path.file_name_without_extension());
if (match != wanted.end())
{
found.emplace(*match, path);
}
}
}
if (depth < max_tool_scan_depth)
{
for (const auto& sub : contents.folders)
{
find_executables(folder.combine(sub.name.sv()), depth + 1, wanted, found);
}
}
}
// Map links are handed to the shell, so only http(s) templates are accepted; another scheme in
// the config would otherwise be a way to launch an arbitrary program from a menu click.
bool is_web_url(const std::string_view url)
{
return str::starts(url, "https://") || str::starts(url, "http://");
}
std::vector<file_map_link> built_in_map_links()
{
return {
{str::cache("Google Maps"), str::cache("https://www.google.com/maps/place/{latitude},{longitude}")},
{str::cache("Google Earth"), str::cache("https://earth.google.com/web/search/{latitude},{longitude}")},
{str::cache("Bing Maps"), str::cache("https://www.bing.com/maps?cp={latitude}~{longitude}&lvl=17&style=h")},
{
str::cache("OpenStreetMap"),
str::cache(
"https://www.openstreetmap.org/?mlat={latitude}&mlon={longitude}#map=17/{latitude}/{longitude}")
},
{
str::cache("Apple Maps"),
str::cache("https://maps.apple.com/?ll={latitude},{longitude}&q=Photo%20Location")
},
};
}
}
file_tools_result scan_tools()
{
file_tools_result result;
std::vector<df::folder_path> tool_paths;
std::vector<file_tool_ptr> tools;
try
{
const auto json = blob_from_file(df::probe_data_file("diffractor-tools.json"));
if (!json.empty())
{
df::util::json::json_doc document;
document.Parse(std::bit_cast<const char*>(json.data()), json.size());
if (document.HasParseError())
{
df::log(__FUNCTION__, std::format("diffractor-tools.json is not valid json (offset {})",
document.GetErrorOffset()));
}
else if (document.IsObject())
{
const auto tools_member = document.FindMember("tools");
if (tools_member != document.MemberEnd() && tools_member->value.IsObject())
{
for (const auto& m : tools_member->value.GetObject())
{
if (str::icmp(m.name.GetString(), "folders") == 0)
{
if (m.value.IsArray())
{
for (const auto& folder : m.value.GetArray())
{
if (folder.IsString())
{
tool_paths.emplace_back(df::folder_path(folder.GetString()));
}
}
}
}
if (str::icmp(m.name.GetString(), "apps") == 0)
{
if (m.value.IsArray())
{
for (const auto& app : m.value.GetArray())
{
if (app.IsObject())
{
auto tool = std::make_shared<file_tool>();
for (const auto& a : app.GetObject())
{
if (!a.value.IsString()) continue;
if (str::icmp(a.name.GetString(), "exe") == 0)
tool->exe = str::cache(a.value.GetString());
if (str::icmp(a.name.GetString(), "invoke") == 0)
tool->invoke_text = str::cache(a.value.GetString());
if (str::icmp(a.name.GetString(), "text") == 0)
tool->text = str::cache(a.value.GetString());
if (str::icmp(a.name.GetString(), "extensions") == 0)
tool->extensions = str::cache(a.value.GetString());
if (str::icmp(a.name.GetString(), "group") == 0)
tool->group = str::cache(a.value.GetString());
}
if (!tool->exe.is_empty() && !tool->invoke_text.is_empty())
{
tools.emplace_back(std::move(tool));
}
}
}
}
}
if (str::icmp(m.name.GetString(), "maps") == 0)
{
if (m.value.IsArray())
{
for (const auto& map : m.value.GetArray())
{
if (map.IsObject())
{
file_map_link link;
for (const auto& a : map.GetObject())
{
if (!a.value.IsString()) continue;
if (str::icmp(a.name.GetString(), "text") == 0)
link.text = str::cache(a.value.GetString());
if (str::icmp(a.name.GetString(), "url") == 0)
link.url = str::cache(a.value.GetString());
}
if (!str::is_empty(link.text) && is_web_url(link.url.sv()))
{
result.map_links.emplace_back(link);
}
}
}
}
}
}
}
}
}
}
catch (std::exception& e)
{
df::log(__FUNCTION__, e.what());
}
df::hash_set<std::string_view, df::ihash, df::ieq> wanted;
for (const auto& tool : tools)
{
wanted.emplace(tool->exe.sv());
}
exe_path_by_name exe_by_name;
for (const auto& folder : tool_paths)
{
find_executables(folder, 1, wanted, exe_by_name);
}
for (const auto& tool : tools)
{
const auto found = exe_by_name.find(tool->exe.sv());
if (found == exe_by_name.end())
{
continue;
}
tool->exe_path = found->second;
str::split2(tool->extensions, true, [&result, &tool](const std::string_view ext)
{
result.by_extension[str::cache(ext)].emplace_back(tool);
});
if (!tool->group.is_empty())
{
result.by_group[tool->group.sv()].emplace_back(tool);
}
}
if (result.map_links.empty())
{
// Fall back to built-in map services when diffractor-tools.json has no usable "maps"
// section (e.g. an older config), so location links always work.
result.map_links = built_in_map_links();
}
return result;
}
void apply_tools(file_tools_result result)
{
for (auto& ft : s_config.types)
{
ft.tools.clear();
if (!ft.extension.empty())
{
str::split2(ft.extension, true, [&ft, &result](const std::string_view ext)
{
const auto found = result.by_extension.find(ext);
if (found != result.by_extension.end())
{
for (const auto& tool : found->second)
{
if (std::ranges::find(ft.tools, tool) == ft.tools.end())
{
ft.tools.emplace_back(tool);
}
}
}
});
}
}
for (const auto g : s_config.groups)
{
const auto found = result.by_group.find(g->name);
g->tools = found != result.by_group.end() ? found->second : std::vector<file_tool_ptr>{};
}
s_map_links = std::move(result.map_links);
}
std::vector<file_map_link> all_map_links()
{
return s_map_links;
}
std::vector<file_group_ref> all_file_groups()
{
std::vector<file_group_ref> result;
result.reserve(s_config.groups.size());
for (auto* const g : s_config.groups)
{
result.emplace_back(g);
}
return result;
}
std::vector<file_type_ref> all_file_types()
{
std::vector<file_type_ref> result;
result.reserve(s_config.types.size());
for (const auto& ft : s_config.types)
{
result.emplace_back(&ft);
}
return result;
}
namespace
{
constexpr auto file_op_stat_count = static_cast<size_t>(file_op_stat::count);
using file_op_row = std::array<uint64_t, file_op_stat_count>;
// Bumped from every queue that touches a file and read once at shutdown. Each bump accompanies
// an open, a decode or a swap, so one short lock costs nothing against the I/O it sits beside.
platform::mutex file_op_mutex;
_Guarded_by_(file_op_mutex) df::hash_map<file_type_ref, file_op_row> file_op_stats;
}
std::function<void(df::file_path destination)> test_before_file_update_publish;
void record_file_op(const file_type_ref ft, const file_op_stat stat)
{
if (!ft) return;
platform::exclusive_lock lock(file_op_mutex);
file_op_stats[ft][static_cast<size_t>(stat)] += 1;
}
void log_file_op_summary()
{
std::vector<std::pair<file_type_ref, file_op_row>> rows;
{
platform::exclusive_lock lock(file_op_mutex);
rows.assign(file_op_stats.begin(), file_op_stats.end());
}
if (rows.empty()) return;
const auto row_total = [](const file_op_row& r)
{
uint64_t total = 0;
for (const auto n : r) total += n;
return total;
};
std::ranges::sort(rows, [&row_total](const auto& a, const auto& b)
{
return row_total(a.second) > row_total(b.second);
});
// The first extension names the whole group, and the capability marker states what the traits
// table promises - so a row that patches when it claims it cannot, or replaces when it claims
// it can patch, is visible without cross-referencing the traits.
const auto group_name = [](const std::string_view extensions)
{
const auto comma = extensions.find(',');
return comma == std::string_view::npos ? extensions : extensions.substr(0, comma);
};
const auto capability = [](const file_type_ref ft)
{
return ft->has_trait(file_traits::in_place_metadata) ? "if it fits"sv : "-"sv;
};
file_op_row totals{};
std::string report =
"\n### Diffractor file operation results\n\n"
"| File type | In-place capability | Reads | In-place writes | Replacements | Sidecars | Write failures |\n"
"|---|---|---:|---:|---:|---:|---:|\n";
for (const auto& [ft, row] : rows)
{
for (auto i = 0_z; i < file_op_stat_count; ++i) totals[i] += row[i];
report += std::format("| {} | {} | {} | {} | {} | {} | {} |\n", group_name(ft->extension),
capability(ft), row[0], row[1], row[2], row[3], row[4]);
}
report += std::format("| **Total** | - | {} | {} | {} | {} | {} |\n", totals[0], totals[1], totals[2],
totals[3], totals[4]);
df::log("perf file types", report);
}
namespace
{
class loaded_clipboard_image final : public platform::clipboard_image
{
file_load_result _loaded;
public:
explicit loaded_clipboard_image(file_load_result loaded) : _loaded(std::move(loaded))
{
}
sizei dimensions() const override