QuickImage – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Linq;
7 using System.Runtime.CompilerServices;
8 using System.Threading;
9 using System.Threading.Tasks;
10 using System.Threading.Tasks.Dataflow;
11 using ImageMagick;
12 using ImageMagick.Formats;
13 using QuickImage.Utilities;
14 using Serilog;
15  
16 namespace QuickImage
17 {
18 public class TagManager
19 {
20 private readonly MagickReadSettings _magickReaderSettings;
21 private readonly FileMutex _fileMutex;
22  
23 private TagManager()
24 {
25 _magickReaderSettings = new MagickReadSettings { FrameIndex = 0, FrameCount = 1 };
26 }
27  
28 public TagManager(FileMutex fileMutex) : this()
29 {
30 _fileMutex = fileMutex;
31 }
32  
33 public async Task<bool> StripIptcProfile(string fileName, CancellationToken cancellationToken)
34 {
35 await _fileMutex[fileName].WaitAsync(cancellationToken);
36  
37 try
38 {
39 using var imageCollection = new MagickImageCollection(fileName);
40 using var image = imageCollection[0];
41  
42 var profile = image.GetIptcProfile();
43  
44 if (profile == null)
45 {
46 return true;
47 }
48  
49 image.RemoveProfile(profile);
50 await imageCollection.WriteAsync(fileName, cancellationToken);
51  
52 return true;
53 }
54 catch (Exception exception)
55 {
56 Log.Error(exception, "Could not add keywords.");
57 }
58 finally
59 {
60 _fileMutex[fileName].Release();
61 }
62  
63 return false;
64 }
65  
66 public async Task<bool> AddIptcKeywords(string fileName, IEnumerable<string> keywords,
67 CancellationToken cancellationToken)
68 {
69 await _fileMutex[fileName].WaitAsync(cancellationToken);
70  
71 try
72 {
73 using var imageCollection = new MagickImageCollection(fileName);
74 using var image = imageCollection[0];
75  
76 var profile = image.GetIptcProfile();
77  
78 if (profile == null)
79 {
80 profile = new IptcProfile();
81  
82 foreach (var keyword in keywords)
83 {
84 profile.SetValue(IptcTag.Keyword, keyword);
85 }
86  
87 image.SetProfile(profile);
2 office 88  
1 office 89 await imageCollection.WriteAsync(fileName, cancellationToken);
90  
91 return true;
92 }
93  
94 var union = keywords.Union(profile.GetAllValues(IptcTag.Keyword).Select(iptcValue => iptcValue.Value),
95 StringComparer.Ordinal).ToList();
96 profile.RemoveValue(IptcTag.Keyword);
97 foreach (var keyword in union)
98 {
99 profile.SetValue(IptcTag.Keyword, keyword);
100 }
101  
102 image.SetProfile(profile);
2 office 103 using var writeStream = new FileStream(fileName, FileMode.OpenOrCreate);
104 await imageCollection.WriteAsync(writeStream, cancellationToken);
1 office 105  
106 return true;
107 }
108 catch (Exception exception)
109 {
110 Log.Error(exception, "Could not add keywords.");
111 }
112 finally
113 {
114 _fileMutex[fileName].Release();
115 }
116  
117 return false;
118 }
119  
120 public async Task<bool> RemoveIptcKeywords(string fileName, IEnumerable<string> keywords, CancellationToken cancellationToken)
121 {
122 await _fileMutex[fileName].WaitAsync(cancellationToken);
123  
124 try
125 {
126 using var imageCollection = new MagickImageCollection(fileName);
127 using var image = imageCollection[0];
128  
129 var profile = image.GetIptcProfile();
130  
131 if (profile == null)
132 {
133 return true;
134 }
135  
136 foreach (var keyword in keywords)
137 {
138 profile.RemoveValue(IptcTag.Keyword, keyword);
139 }
140  
141 image.SetProfile(profile);
142 imageCollection.Optimize();
143 await imageCollection.WriteAsync(fileName, cancellationToken);
144  
145 return true;
146 }
147 catch (Exception exception)
148 {
149 Log.Error(exception, "Could not remove keywords.");
150 }
151 finally
152 {
153 _fileMutex[fileName].Release();
154 }
155  
156 return false;
157 }
158  
159 public async IAsyncEnumerable<string> GetIptcKeywords(string fileName, [EnumeratorCancellation] CancellationToken cancellationToken)
160 {
161 await _fileMutex[fileName].WaitAsync(cancellationToken);
162  
163 try
164 {
165 using var imageCollection = new MagickImageCollection(fileName, _magickReaderSettings);
166 using var image = imageCollection[0];
167  
168 var profile = image.GetIptcProfile();
169  
170 if (profile == null)
171 {
172 yield break;
173 }
174  
175 foreach (var keyword in profile.GetAllValues(IptcTag.Keyword))
176 {
177 yield return keyword.Value;
178 }
179 }
180 finally
181 {
182 _fileMutex[fileName].Release();
183 }
184 }
185  
186 public IEnumerable<string> GetIptcKeywords(IMagickImage<ushort> imageFrame)
187 {
188 var profile = imageFrame.GetIptcProfile();
189  
190 if (profile == null)
191 {
192 yield break;
193 }
194  
195 foreach (var keyword in profile.GetAllValues(IptcTag.Keyword))
196 {
197 yield return keyword.Value;
198 }
199  
200 }
201 }
202 }