Browse Source

修正文件真实类型检测的bug

懒得勤快 1 year ago
parent
commit
60bdb6f974

+ 1 - 1
Directory.Build.props

@@ -1,6 +1,6 @@
 <Project>
  <PropertyGroup>
-   <Version>2024.5.3</Version>
+   <Version>2024.5.4</Version>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
 </Project>

File diff suppressed because it is too large
+ 221 - 383
Masuit.Tools.Abstractions/Files/FileDetector/AbstractCompoundFileDetailDetector.cs


+ 1 - 4
Masuit.Tools.Abstractions/Files/FileDetector/AbstractFullRegexDetector.cs

@@ -1,7 +1,4 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
+using System.Reflection;
 using System.Text;
 using System.Text.RegularExpressions;
 using Masuit.Tools.Mime;

+ 1 - 4
Masuit.Tools.Abstractions/Files/FileDetector/AbstractISOBaseMediaFileDetailDetector.cs

@@ -1,7 +1,4 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
+using System.Reflection;
 using System.Text;
 using Masuit.Tools.Mime;
 

+ 1 - 5
Masuit.Tools.Abstractions/Files/FileDetector/AbstractRegexSignatureDetector.cs

@@ -1,7 +1,4 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
+using System.Reflection;
 using System.Text;
 using System.Text.RegularExpressions;
 using Masuit.Tools.Mime;
@@ -24,7 +21,6 @@ public abstract class AbstractRegexSignatureDetector : IDetector
     {
         int readBufferSize = Signature.ToString().Length * 8;
         char[] buffer = new char[readBufferSize];
-
         var encodings = new[]
         {
             Encoding.ASCII,

+ 16 - 37
Masuit.Tools.Abstractions/Files/FileDetector/AbstractSignatureDetector.cs

@@ -1,8 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
+using System.Reflection;
 using System.Runtime.InteropServices;
 using Masuit.Tools.Mime;
 
@@ -73,49 +69,32 @@ public abstract class AbstractSignatureDetector : IDetector
         }
 
         byte[] buffer = new byte[_cachedLongestLength];
-
         byte[] preSignature = null;
-        bool correct = false;
-        while (true)
+        foreach (var siginfo in SignatureInformations.Where(si => CompareArray(si.Presignature, preSignature)).OrderBy(si => si.Position))
         {
-            bool found = false;
-            foreach (var siginfo in SignatureInformations.Where(si => CompareArray(si.Presignature, preSignature)).OrderBy(si => si.Position))
-            {
-                correct = false;
-                stream.Position = siginfo.Position;
-                stream.Read(buffer, 0, _cachedLongestLength);
-                if (CompareArray(siginfo.Signature, buffer))
-                {
-                    preSignature = siginfo.Signature;
-                    correct = true;
-                    found = true;
-                    break;
-                }
-            }
-            if (!found)
+            stream.Position = siginfo.Position;
+            _ = stream.Read(buffer, 0, _cachedLongestLength);
+            if (CompareArray(siginfo.Signature, buffer))
             {
-                break;
+                preSignature = siginfo.Signature;
+                return true;
             }
         }
 
-        return correct;
+        return false;
     }
 
-    private bool CompareArray(byte[] a1, byte[] a2)
+    private static bool CompareArray(byte[] a1, byte[] a2)
     {
-        if (a1 == null && a2 == null) return true;
-        if (a1 == null || a2 == null) return false;
+        if (a1 == null && a2 == null)
+        {
+            return true;
+        }
 
-        bool failed = false;
-        int min = a1.Length > a2.Length ? a2.Length : a1.Length;
-        for (int i = 0; i < min; ++i)
+        if (a1 == null || a2 == null)
         {
-            if (a1[i] != a2[i])
-            {
-                failed = true;
-                break;
-            }
+            return false;
         }
-        return !failed;
+        return a1.SequenceEqual(a2);
     }
 }

+ 1 - 4
Masuit.Tools.Abstractions/Files/FileDetector/AbstractZipDetailDetector.cs

@@ -1,7 +1,4 @@
-using System.Collections.Generic;
-using System.IO;
-using System.IO.Compression;
-using System.Linq;
+using System.IO.Compression;
 using System.Reflection;
 using Masuit.Tools.Mime;
 

+ 51 - 69
Masuit.Tools.Abstractions/Files/FileDetector/FileSignatureDetector.cs

@@ -10,80 +10,62 @@ namespace Masuit.Tools.Files.FileDetector;
 
 public static class FileSignatureDetector
 {
-	private static List<IDetector> Detectors { get; set; } = [];
+    private static List<IDetector> Detectors { get; set; } = [];
 
-	public static IReadOnlyList<IDetector> Registered => Detectors;
+    public static IReadOnlyList<IDetector> Registered => Detectors;
 
-	public static void AddDetector<T>() where T : IDetector
-	{
-		var instance = Activator.CreateInstance<T>();
-		AddDetector(instance);
-	}
+    public static void AddDetector<T>() where T : IDetector
+    {
+        var instance = Activator.CreateInstance<T>();
+        AddDetector(instance);
+    }
 
-	public static void AddDetector(IDetector instance)
-	{
-		if (!Detectors.Contains(instance))
-		{
-			Detectors.Add(instance);
-		}
-	}
+    public static void AddDetector(IDetector instance)
+    {
+        if (!Detectors.Contains(instance))
+        {
+            Detectors.Add(instance);
+        }
+    }
 
-	static FileSignatureDetector()
-	{
-		var type = typeof(IDetector);
-		foreach (var item in AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.FullName.StartsWith(["System", "Microsoft"])).SelectMany(a => a.GetLoadableExportedTypes()))
-		{
-			if (type.IsAssignableFrom(item) && !item.IsAbstract && !item.IsInterface && item.GetTypeInfo().DeclaredConstructors.First().GetParameters().Length == 0)
-			{
-				AddDetector(Activator.CreateInstance(item) as IDetector);
-			}
-		}
-	}
+    static FileSignatureDetector()
+    {
+        var type = typeof(IDetector);
+        foreach (var item in AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.FullName.StartsWith(["System", "Microsoft"])).SelectMany(a => a.GetLoadableTypes()))
+        {
+            if (type.IsAssignableFrom(item) && !item.IsAbstract && !item.IsInterface && item.GetTypeInfo().DeclaredConstructors.First().GetParameters().Length == 0)
+            {
+                AddDetector(Activator.CreateInstance(item) as IDetector);
+            }
+        }
+    }
 
-	public static IDetector DetectFiletype(string filepath)
-	{
-		using var stream = File.OpenRead(filepath);
-		return DetectFiletype(stream);
-	}
+    public static IDetector DetectFiletype(string filepath)
+    {
+        using var stream = File.OpenRead(filepath);
+        return DetectFiletype(stream);
+    }
 
-	public static IDetector DetectFiletype(this FileInfo file)
-	{
-		using var stream = file.OpenRead();
-		return DetectFiletype(stream);
-	}
+    public static IDetector DetectFiletype(this FileInfo file)
+    {
+        using var stream = file.OpenRead();
+        return DetectFiletype(stream);
+    }
 
-	public static IDetector DetectFiletype(this Stream stream)
-	{
-		if (stream.CanSeek)
-		{
-			string pre = null;
-			IDetector foundDetector = new NoneDetector();
-			while (true)
-			{
-				bool found = false;
-				foreach (var detector in Detectors.Where(d => d.Precondition == pre))
-				{
-					stream.Position = 0;
-					if (detector.Detect(stream))
-					{
-						found = true;
-						foundDetector = detector;
-						pre = detector.Extension;
-						break;
-					}
-				}
-				if (!found)
-				{
-					break;
-				}
-			}
-
-			stream.Position = 0;
-			return foundDetector;
-		}
-
-		using var ms = new PooledMemoryStream();
-		stream.CopyTo(ms);
-		return DetectFiletype(ms);
-	}
+    public static IDetector DetectFiletype(this Stream stream)
+    {
+        if (stream.CanSeek)
+        {
+            foreach (var detector in Detectors)
+            {
+                stream.Position = 0;
+                if (detector.Detect(stream))
+                {
+                    stream.Position = 0;
+                    return detector;
+                }
+            }
+        }
+        return  new NoneDetector();
+    }
 }

+ 1 - 2
Masuit.Tools.Abstractions/Files/FileDetector/FormatCategoryAttribute.cs

@@ -1,5 +1,4 @@
-using System;
-using System.ComponentModel;
+using System.ComponentModel;
 
 namespace Masuit.Tools.Files.FileDetector;
 

+ 1 - 1
Masuit.Tools.Abstractions/Files/FileDetector/InstantDetector.cs

@@ -7,7 +7,7 @@ using System.Reflection;
 
 namespace Masuit.Tools.Files.FileDetector;
 
-public class InstantDetector : IDetector
+internal class InstantDetector : IDetector
 {
     public string Description { get; }
 

Some files were not shown because too many files changed in this diff