QuickImage – Blame information for rev 7
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using MimeDetective; |
2 | using System; |
||
3 | using System.Collections.Concurrent; |
||
4 | using System.Collections.Generic; |
||
5 | using System.Collections.Immutable; |
||
6 | using System.IO; |
||
7 | using System.Linq; |
||
8 | using System.Text; |
||
9 | using System.Threading; |
||
10 | using System.Threading.Tasks; |
||
11 | using MimeDetective.Storage; |
||
12 | using System.Security.Cryptography; |
||
2 | office | 13 | using MimeDetective.Engine; |
14 | |||
1 | office | 15 | namespace QuickImage.Utilities |
16 | { |
||
17 | public class MagicMime |
||
18 | { |
||
19 | private static FileMutex _fileMutex; |
||
2 | office | 20 | private static ConcurrentDictionary<string, MagicMimeFile> _fileMime; |
21 | private static IContentInspector _mimeInspector; |
||
22 | |||
1 | office | 23 | private MagicMime() |
24 | { |
||
25 | _fileMime = new ConcurrentDictionary<string, MagicMimeFile>(); |
||
26 | _mimeInspector = new ContentInspectorBuilder() |
||
27 | { |
||
28 | Definitions = new MimeDetective.Definitions.ExhaustiveBuilder() |
||
29 | { |
||
30 | UsageType = MimeDetective.Definitions.Licensing.UsageType.PersonalNonCommercial |
||
31 | }.Build() |
||
32 | }.Build(); |
||
33 | } |
||
34 | |||
35 | public MagicMime(FileMutex fileMutex) :this() |
||
36 | { |
||
37 | _fileMutex = fileMutex; |
||
38 | } |
||
39 | |||
40 | public async Task<MagicMimeFile> Identify(string fileName, CancellationToken cancellationToken) |
||
41 | { |
||
42 | await _fileMutex[fileName].WaitAsync(cancellationToken); |
||
43 | try |
||
44 | { |
||
2 | office | 45 | var matches = new List<DefinitionMatch>(_mimeInspector.Inspect(fileName)); |
46 | matches.Sort(MimeInspectorDefinitionMatchComparer); |
||
47 | var definitionMatch = matches.FirstOrDefault(); |
||
1 | office | 48 | if (definitionMatch == null) |
49 | { |
||
50 | return null; |
||
51 | } |
||
52 | |||
53 | var magicMimeFile = new MagicMimeFile |
||
54 | { |
||
55 | FileName = fileName, |
||
56 | Definition = definitionMatch.Definition, |
||
57 | FileHash = Cryptography.Hashes.MD5Hash(fileName) |
||
58 | }; |
||
59 | |||
60 | _fileMime.AddOrUpdate(fileName, magicMimeFile, |
||
61 | (key, oldValue) => magicMimeFile); |
||
62 | |||
63 | return magicMimeFile; |
||
64 | } |
||
65 | finally |
||
66 | { |
||
67 | _fileMutex[fileName].Release(); |
||
68 | } |
||
2 | office | 69 | } |
70 | |||
71 | private int MimeInspectorDefinitionMatchComparer(DefinitionMatch x, DefinitionMatch y) |
||
72 | { |
||
73 | return y.Points.CompareTo(x.Points); |
||
74 | } |
||
75 | |||
1 | office | 76 | public async Task<string> GetMimeType(string fileName, CancellationToken cancellationToken) |
77 | { |
||
78 | var identify = await Identify(fileName, cancellationToken); |
||
79 | |||
7 | office | 80 | if(identify == null) |
81 | { |
||
82 | return string.Empty; |
||
83 | } |
||
84 | |||
1 | office | 85 | return identify.Definition.File.MimeType; |
86 | } |
||
87 | |||
88 | public MagicMimeFile Identify(string fileName, MemoryStream memoryStream, CancellationToken cancellationToken) |
||
89 | { |
||
90 | var definitionMatch = _mimeInspector.Inspect(memoryStream).FirstOrDefault(); |
||
91 | if (definitionMatch == null) |
||
92 | { |
||
93 | return null; |
||
94 | } |
||
95 | |||
96 | var magicMimeFile = new MagicMimeFile |
||
97 | { |
||
98 | FileName = fileName, |
||
99 | Definition = definitionMatch.Definition, |
||
100 | FileHash = Cryptography.Hashes.MD5Hash(fileName) |
||
101 | }; |
||
102 | |||
7 | office | 103 | _fileMime.AddOrUpdate(fileName, magicMimeFile, (key, oldValue) => magicMimeFile); |
1 | office | 104 | return magicMimeFile; |
105 | } |
||
106 | } |
||
107 | } |