forked from drewnoakes/metadata-extractor-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractMetadataCmdlet.cs
78 lines (68 loc) · 2.51 KB
/
ExtractMetadataCmdlet.cs
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
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using System.Management.Automation;
using JetBrains.Annotations;
using MetadataExtractor.Formats.Xmp;
namespace MetadataExtractor.PowerShell
{
[Cmdlet("Extract", "Metadata")]
[UsedImplicitly]
public sealed class ExtractMetadata : PSCmdlet
{
[Parameter(Position = 0, Mandatory = true, HelpMessage = "Path to the file to process")]
[ValidateNotNullOrEmpty]
public string FilePath { get; set; } = default!;
[Parameter(HelpMessage = "Show raw value")]
public SwitchParameter Raw { get; set; }
protected override void ProcessRecord()
{
base.ProcessRecord();
WriteVerbose($"Extracting metadata from file: {FilePath}");
var directories = ImageMetadataReader.ReadMetadata(FilePath);
WriteObject(directories.SelectMany(GetDirectoryItems).ToList());
}
private IEnumerable<object> GetDirectoryItems(Directory directory)
{
// XmpDirectory gets special treatment -- we use the XmpMeta object to list properties
var xmp = directory as XmpDirectory;
if (xmp?.XmpMeta is not null)
{
if (Raw)
{
return xmp.XmpMeta.Properties.Select(prop => new
{
Directory = directory.Name,
Tag = prop.Path,
RawValue = (object)prop.Value
});
}
else
{
return xmp.XmpMeta.Properties.Select(prop => new
{
Directory = directory.Name,
Tag = prop.Path,
Description = prop.Value
});
}
}
if (Raw)
{
return directory.Tags.Select(tag => new
{
Directory = directory.Name,
Tag = tag.Name,
RawValue = directory.GetObject(tag.Type)
});
}
else
{
return directory.Tags.Select(tag => new
{
Directory = directory.Name,
Tag = tag.Name,
Description = tag.Description
});
}
}
}
}