aboutsummaryrefslogtreecommitdiff
path: root/Emby.Photos/PhotoProvider.cs
blob: 99a635e608cb6f9b8da6d99af9741211f005cd88 (plain)
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
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;
using TagLib;
using TagLib.IFD;
using TagLib.IFD.Entries;
using TagLib.IFD.Tags;

namespace Emby.Photos
{
    public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
    {
        private readonly ILogger _logger;
        private readonly IImageProcessor _imageProcessor;

        // These are causing taglib to hang
        private string[] _includextensions = new string[] { ".jpg", ".jpeg", ".png", ".tiff", ".cr2" };

        public PhotoProvider(ILogger logger, IImageProcessor imageProcessor)
        {
            _logger = logger;
            _imageProcessor = imageProcessor;
        }

        public string Name => "Embedded Information";

        public bool HasChanged(BaseItem item, IDirectoryService directoryService)
        {
            if (item.IsFileProtocol)
            {
                var file = directoryService.GetFile(item.Path);
                return (file != null && file.LastWriteTimeUtc != item.DateModified);
            }

            return false;
        }

        public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
        {
            item.SetImagePath(ImageType.Primary, item.Path);

            // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
            if (_includextensions.Contains(Path.GetExtension(item.Path), StringComparer.OrdinalIgnoreCase))
            {
                try
                {
                    using (var file = TagLib.File.Create(item.Path))
                    {
                        if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
                        {
                            var structure = tag.Structure;
                            if (structure != null
                                && structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
                            {
                                var exifStructure = exif.Structure;
                                if (exifStructure != null)
                                {
                                    var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
                                    if (entry != null)
                                    {
                                        item.Aperture = (double)entry.Value.Numerator / entry.Value.Denominator;
                                    }

                                    entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
                                    if (entry != null)
                                    {
                                        item.ShutterSpeed = (double)entry.Value.Numerator / entry.Value.Denominator;
                                    }
                                }
                            }
                        }

                        if (file is TagLib.Image.File image)
                        {
                            item.CameraMake = image.ImageTag.Make;
                            item.CameraModel = image.ImageTag.Model;

                            item.Width = image.Properties.PhotoWidth;
                            item.Height = image.Properties.PhotoHeight;

                            var rating = image.ImageTag.Rating;
                            if (rating.HasValue)
                            {
                                item.CommunityRating = rating;
                            }
                            else
                            {
                                item.CommunityRating = null;
                            }

                            item.Overview = image.ImageTag.Comment;

                            if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
                                && !item.LockedFields.Contains(MetadataFields.Name))
                            {
                                item.Name = image.ImageTag.Title;
                            }

                            var dateTaken = image.ImageTag.DateTime;
                            if (dateTaken.HasValue)
                            {
                                item.DateCreated = dateTaken.Value;
                                item.PremiereDate = dateTaken.Value;
                                item.ProductionYear = dateTaken.Value.Year;
                            }

                            item.Genres = image.ImageTag.Genres;
                            item.Tags = image.ImageTag.Keywords;
                            item.Software = image.ImageTag.Software;

                            if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
                            {
                                item.Orientation = null;
                            }
                            else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation))
                            {
                                item.Orientation = orientation;
                            }

                            item.ExposureTime = image.ImageTag.ExposureTime;
                            item.FocalLength = image.ImageTag.FocalLength;

                            item.Latitude = image.ImageTag.Latitude;
                            item.Longitude = image.ImageTag.Longitude;
                            item.Altitude = image.ImageTag.Altitude;

                            if (image.ImageTag.ISOSpeedRatings.HasValue)
                            {
                                item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
                            }
                            else
                            {
                                item.IsoSpeedRating = null;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
                }
            }

            if (item.Width <= 0 || item.Height <= 0)
            {
                var img = item.GetImageInfo(ImageType.Primary, 0);

                try
                {
                    var size = _imageProcessor.GetImageDimensions(item, img, false);

                    if (size.Width > 0 && size.Height > 0)
                    {
                        item.Width = size.Width;
                        item.Height = size.Height;
                    }
                }
                catch (ArgumentException)
                {
                    // format not supported
                }
            }

            const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
            return Task.FromResult(result);
        }
    }
}