|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Reflection;
|
|
|
+using Masuit.Tools.Systems;
|
|
|
|
|
|
namespace Masuit.Tools.Files.FileDetector;
|
|
|
|
|
@@ -52,28 +53,36 @@ public static class FileSignatureDetector
|
|
|
|
|
|
public static IDetector DetectFiletype(this Stream stream)
|
|
|
{
|
|
|
- string pre = null;
|
|
|
- IDetector foundDetector = new NoneDetector();
|
|
|
- while (true)
|
|
|
+ if (stream.CanSeek)
|
|
|
{
|
|
|
- bool found = false;
|
|
|
- foreach (var detector in Detectors.Where(d => d.Precondition == pre))
|
|
|
+ string pre = null;
|
|
|
+ IDetector foundDetector = new NoneDetector();
|
|
|
+ while (true)
|
|
|
{
|
|
|
- stream.Position = 0;
|
|
|
- if (detector.Detect(stream))
|
|
|
+ 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)
|
|
|
{
|
|
|
- found = true;
|
|
|
- foundDetector = detector;
|
|
|
- pre = detector.Extension;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if (!found)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
+
|
|
|
+ stream.Position = 0;
|
|
|
+ return foundDetector;
|
|
|
}
|
|
|
|
|
|
- return foundDetector;
|
|
|
+ using var ms = new PooledMemoryStream();
|
|
|
+ stream.CopyTo(ms);
|
|
|
+ return DetectFiletype(ms);
|
|
|
}
|
|
|
}
|