QuickImage – Blame information for rev 1
?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; |
||
13 | |||
14 | namespace QuickImage.Utilities |
||
15 | { |
||
16 | public class MagicMime |
||
17 | { |
||
18 | private static ContentInspector _mimeInspector; |
||
19 | private static FileMutex _fileMutex; |
||
20 | private static ConcurrentDictionary<string, MagicMimeFile> _fileMime; |
||
21 | |||
22 | private MagicMime() |
||
23 | { |
||
24 | _fileMime = new ConcurrentDictionary<string, MagicMimeFile>(); |
||
25 | _mimeInspector = new ContentInspectorBuilder() |
||
26 | { |
||
27 | Definitions = new MimeDetective.Definitions.ExhaustiveBuilder() |
||
28 | { |
||
29 | UsageType = MimeDetective.Definitions.Licensing.UsageType.PersonalNonCommercial |
||
30 | }.Build() |
||
31 | }.Build(); |
||
32 | } |
||
33 | |||
34 | public MagicMime(FileMutex fileMutex) :this() |
||
35 | { |
||
36 | _fileMutex = fileMutex; |
||
37 | } |
||
38 | |||
39 | public async Task<MagicMimeFile> Identify(string fileName, CancellationToken cancellationToken) |
||
40 | { |
||
41 | await _fileMutex[fileName].WaitAsync(cancellationToken); |
||
42 | try |
||
43 | { |
||
44 | var definitionMatch = _mimeInspector.Inspect(fileName).FirstOrDefault(); |
||
45 | if (definitionMatch == null) |
||
46 | { |
||
47 | return null; |
||
48 | } |
||
49 | |||
50 | var magicMimeFile = new MagicMimeFile |
||
51 | { |
||
52 | FileName = fileName, |
||
53 | Definition = definitionMatch.Definition, |
||
54 | FileHash = Cryptography.Hashes.MD5Hash(fileName) |
||
55 | }; |
||
56 | |||
57 | _fileMime.AddOrUpdate(fileName, magicMimeFile, |
||
58 | (key, oldValue) => magicMimeFile); |
||
59 | |||
60 | return magicMimeFile; |
||
61 | } |
||
62 | finally |
||
63 | { |
||
64 | _fileMutex[fileName].Release(); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | public async Task<string> GetMimeType(string fileName, CancellationToken cancellationToken) |
||
69 | { |
||
70 | var identify = await Identify(fileName, cancellationToken); |
||
71 | |||
72 | return identify.Definition.File.MimeType; |
||
73 | } |
||
74 | |||
75 | public MagicMimeFile Identify(string fileName, MemoryStream memoryStream, CancellationToken cancellationToken) |
||
76 | { |
||
77 | var definitionMatch = _mimeInspector.Inspect(memoryStream).FirstOrDefault(); |
||
78 | if (definitionMatch == null) |
||
79 | { |
||
80 | return null; |
||
81 | } |
||
82 | |||
83 | var magicMimeFile = new MagicMimeFile |
||
84 | { |
||
85 | FileName = fileName, |
||
86 | Definition = definitionMatch.Definition, |
||
87 | FileHash = Cryptography.Hashes.MD5Hash(fileName) |
||
88 | }; |
||
89 | |||
90 | _fileMime.AddOrUpdate(fileName, magicMimeFile, |
||
91 | (key, oldValue) => magicMimeFile); |
||
92 | |||
93 | return magicMimeFile; |
||
94 | } |
||
95 | } |
||
96 | } |