| | | 1 | | using MetadataExtractor.Formats.QuickTime; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Worker.Ingestion.Extraction; |
| | | 5 | | |
| | | 6 | | /// <summary>Reads the raw value of <c>com.apple.quicktime.creationdate</c> from <c>moov</c> → <c>meta</c>, with its off |
| | | 7 | | internal static class AppleCreationDates |
| | | 8 | | { |
| | | 9 | | private const string creationDateKey = "com.apple.quicktime.creationdate"; |
| | | 10 | | |
| | | 11 | | /// <summary>The raw creation date, or null when the file records none or its atoms end early.</summary> |
| | | 12 | | /// <param name="stream">The file, positioned at its start.</param> |
| | | 13 | | public static string? Read(Stream stream) |
| | 15 | 14 | | { |
| | 15 | 15 | | var keys = new List<string>(); |
| | 15 | 16 | | string? value = null; |
| | 15 | 17 | | QuickTimeReader.ProcessAtoms(stream, top => Into(top, "moov", moov => Into(moov, "meta", meta => |
| | 15 | 18 | | { |
| | 15 | 19 | | if (meta.TypeString == "keys") |
| | 15 | 20 | | { |
| | 15 | 21 | | meta.Reader.Skip(8); |
| | 15 | 22 | | QuickTimeReader.ProcessAtoms(stream, key => keys.Add(Utf8(key)), meta.BytesLeft); |
| | 15 | 23 | | } |
| | 15 | 24 | | |
| | 15 | 25 | | var index = keys.IndexOf(creationDateKey) + 1; |
| | 15 | 26 | | Into(meta, "ilst", item => |
| | 15 | 27 | | { |
| | 15 | 28 | | if (item.Type == index) |
| | 15 | 29 | | QuickTimeReader.ProcessAtoms(stream, data => value = data.TypeString == "data" ? DataValue(data) : v |
| | 15 | 30 | | }); |
| | 15 | 31 | | }))); |
| | 14 | 32 | | return value; |
| | 14 | 33 | | } |
| | | 34 | | |
| | | 35 | | private static void Into(AtomCallbackArgs atom, string type, Action<AtomCallbackArgs> children) |
| | 911 | 36 | | { |
| | 911 | 37 | | if (atom.TypeString == type) |
| | 29 | 38 | | QuickTimeReader.ProcessAtoms(atom.Stream, children, atom.BytesLeft); |
| | 908 | 39 | | } |
| | | 40 | | |
| | | 41 | | private static string DataValue(AtomCallbackArgs data) |
| | 6 | 42 | | { |
| | 6 | 43 | | data.Reader.Skip(8); |
| | 6 | 44 | | return Utf8(data); |
| | 4 | 45 | | } |
| | | 46 | | |
| | 18 | 47 | | private static string Utf8(AtomCallbackArgs atom) => Encoding.UTF8.GetString(atom.Reader.GetBytes((int)atom.BytesLef |
| | | 48 | | } |