QuickImage – Diff between revs 1 and 2
?pathlinks?
Rev 1 | Rev 2 | |||
---|---|---|---|---|
Line 8... | Line 8... | |||
8 | using System.Text; |
8 | using System.Text; |
|
9 | using System.Threading; |
9 | using System.Threading; |
|
10 | using System.Threading.Tasks; |
10 | using System.Threading.Tasks; |
|
11 | using MimeDetective.Storage; |
11 | using MimeDetective.Storage; |
|
12 | using System.Security.Cryptography; |
12 | using System.Security.Cryptography; |
|
- | 13 | using MimeDetective.Engine; |
||
13 | |
14 | |
|
14 | namespace QuickImage.Utilities |
15 | namespace QuickImage.Utilities |
|
15 | { |
16 | { |
|
16 | public class MagicMime |
17 | public class MagicMime |
|
17 | { |
18 | { |
|
18 | private static ContentInspector _mimeInspector; |
- | ||
19 | private static FileMutex _fileMutex; |
19 | private static FileMutex _fileMutex; |
|
20 | private static ConcurrentDictionary<string, MagicMimeFile> _fileMime; |
20 | private static ConcurrentDictionary<string, MagicMimeFile> _fileMime; |
|
- | 21 | private static IContentInspector _mimeInspector; |
||
21 | |
22 | |
|
22 | private MagicMime() |
23 | private MagicMime() |
|
23 | { |
24 | { |
|
24 | _fileMime = new ConcurrentDictionary<string, MagicMimeFile>(); |
25 | _fileMime = new ConcurrentDictionary<string, MagicMimeFile>(); |
|
25 | _mimeInspector = new ContentInspectorBuilder() |
26 | _mimeInspector = new ContentInspectorBuilder() |
|
26 | { |
27 | { |
|
Line 39... | Line 40... | |||
39 | public async Task<MagicMimeFile> Identify(string fileName, CancellationToken cancellationToken) |
40 | public async Task<MagicMimeFile> Identify(string fileName, CancellationToken cancellationToken) |
|
40 | { |
41 | { |
|
41 | await _fileMutex[fileName].WaitAsync(cancellationToken); |
42 | await _fileMutex[fileName].WaitAsync(cancellationToken); |
|
42 | try |
43 | try |
|
43 | { |
44 | { |
|
- | 45 | var matches = new List<DefinitionMatch>(_mimeInspector.Inspect(fileName)); |
||
- | 46 | matches.Sort(MimeInspectorDefinitionMatchComparer); |
||
44 | var definitionMatch = _mimeInspector.Inspect(fileName).FirstOrDefault(); |
47 | var definitionMatch = matches.FirstOrDefault(); |
|
45 | if (definitionMatch == null) |
48 | if (definitionMatch == null) |
|
46 | { |
49 | { |
|
47 | return null; |
50 | return null; |
|
48 | } |
51 | } |
|
Line 61... | Line 64... | |||
61 | } |
64 | } |
|
62 | finally |
65 | finally |
|
63 | { |
66 | { |
|
64 | _fileMutex[fileName].Release(); |
67 | _fileMutex[fileName].Release(); |
|
65 | } |
68 | } |
|
66 | } |
69 | } |
|
- | 70 | |
||
- | 71 | private int MimeInspectorDefinitionMatchComparer(DefinitionMatch x, DefinitionMatch y) |
||
- | 72 | { |
||
- | 73 | return y.Points.CompareTo(x.Points); |
||
- | 74 | } |
||
67 | |
75 | |
|
68 | public async Task<string> GetMimeType(string fileName, CancellationToken cancellationToken) |
76 | public async Task<string> GetMimeType(string fileName, CancellationToken cancellationToken) |
|
69 | { |
77 | { |
|
70 | var identify = await Identify(fileName, cancellationToken); |
78 | var identify = await Identify(fileName, cancellationToken); |
|
Line 71... | Line 79... | |||
71 | |
79 | |