Repository navigation
Expand file tree
/
Copy pathSourcePreviewService.cs
More file actions
154 lines (142 loc) · 8.32 KB
/
Copy pathSourcePreviewService.cs
File metadata and controls
154 lines (142 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System.Buffers.Binary;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace QuickPreview;
internal sealed record SourcePreviewResult(string Text, string Summary, bool IsTruncated);
/// <summary>Reads source text for visual formats without parsing, formatting, rendering, or extracting it.</summary>
internal static class SourcePreviewService
{
private const int SniffBytes = 4096;
private const int MaxPreviewBytes = 2 * 1024 * 1024;
private const long MaxContainerBytes = 128L * 1024 * 1024;
private static readonly HashSet<string> Candidates = new(StringComparer.OrdinalIgnoreCase)
{
".step", ".stp", ".iges", ".igs", ".brep", ".brp", ".fcstd", ".stl", ".obj",
".3mf", ".ply", ".off", ".amf", ".3ds", ".dae", ".fbx", ".gltf", ".glb",
".wrl", ".3dm", ".ifc", ".bim", ".dxf", ".dwg", ".svg", ".svgz", ".csv", ".tsv"
};
private static readonly HashSet<string> BinaryOnly = new(StringComparer.OrdinalIgnoreCase)
{ ".glb", ".3ds", ".3dm", ".dwg" };
private static readonly byte[] BinaryFbxHeader = Encoding.ASCII.GetBytes("Kaydara FBX Binary");
private static readonly byte[] BinaryDxfHeader = Encoding.ASCII.GetBytes("AutoCAD Binary DXF");
public static bool IsCandidate(string path) => Candidates.Contains(Path.GetExtension(path));
public static Task<bool> CanShowAsync(string path, CancellationToken token = default) =>
Task.Run(() => CanShowCoreAsync(path, token), token);
private static async Task<bool> CanShowCoreAsync(string path, CancellationToken token)
{
token.ThrowIfCancellationRequested();
var extension = Path.GetExtension(path);
if (!IsCandidate(path) || BinaryOnly.Contains(extension)) return false;
try
{
using var source = OpenSource(path);
var bytes = await ReadAtMostAsync(source.Stream, SniffBytes, token).ConfigureAwait(false);
token.ThrowIfCancellationRequested();
return IsReadableSource(bytes, extension, source.Length);
}
catch (Exception exception) when (exception is IOException or InvalidDataException or UnauthorizedAccessException or NotSupportedException or ArgumentException)
{ return false; }
}
public static Task<SourcePreviewResult> LoadAsync(string path, CancellationToken token = default) =>
Task.Run(() => LoadCoreAsync(path, token), token);
private static async Task<SourcePreviewResult> LoadCoreAsync(string path, CancellationToken token)
{
token.ThrowIfCancellationRequested();
var extension = Path.GetExtension(path);
if (!IsCandidate(path) || BinaryOnly.Contains(extension))
throw new NotSupportedException("У этого формата нет текстового представления.");
using var source = OpenSource(path);
var bytes = await ReadAtMostAsync(source.Stream, MaxPreviewBytes + 1, token).ConfigureAwait(false);
token.ThrowIfCancellationRequested();
var truncated = bytes.Length > MaxPreviewBytes || source.Length > MaxPreviewBytes;
if (bytes.Length > MaxPreviewBytes) Array.Resize(ref bytes, MaxPreviewBytes);
if (!IsReadableSource(bytes, extension, source.Length))
throw new NotSupportedException("Файл содержит двоичные данные, текстовый просмотр недоступен.");
var text = FormatCatalog.DecodeText(bytes);
var summary = source.Label + " · " + text.Length.ToString("N0", CultureInfo.CurrentCulture) + " символов";
if (truncated) summary += " · показаны первые 2 МБ";
return new SourcePreviewResult(text, summary, truncated);
}
private static bool IsReadableSource(byte[] bytes, string extension, long? sourceLength)
{
var sample = bytes.AsSpan();
if (sample.StartsWith(BinaryFbxHeader) || sample.StartsWith(BinaryDxfHeader)) return false;
if (extension.Equals(".stl", StringComparison.OrdinalIgnoreCase) && bytes.Length >= 84 && sourceLength.HasValue)
{
var triangles = BinaryPrimitives.ReadUInt32LittleEndian(sample.Slice(80, 4));
if (84L + triangles * 50L == sourceLength.Value) return false;
}
string text;
try { text = FormatCatalog.DecodeText(bytes); }
catch (DecoderFallbackException) { return false; }
if ((extension.Equals(".svgz", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".3mf", StringComparison.OrdinalIgnoreCase) ||
extension.Equals(".fcstd", StringComparison.OrdinalIgnoreCase)) && !text.TrimStart().StartsWith('<')) return false;
if (extension.Equals(".ply", StringComparison.OrdinalIgnoreCase))
{
var header = text.AsSpan(0, Math.Min(text.Length, SniffBytes));
if (header.IndexOf("format binary_".AsSpan(), StringComparison.OrdinalIgnoreCase) >= 0) return false;
}
// DecodeText understands BOM/UTF-16/UTF-32 and falls back to Windows-1251.
// Validate characters after decoding so legitimate Unicode NUL bytes are retained.
foreach (var character in text)
if (character == '\0' || char.IsControl(character) && character is not ('\r' or '\n' or '\t' or '\f')) return false;
return true;
}
private static async Task<byte[]> ReadAtMostAsync(Stream stream, int limit, CancellationToken token)
{
var bytes = new byte[limit];
var length = 0;
while (length < limit)
{
var count = await stream.ReadAsync(bytes.AsMemory(length, limit - length), token).ConfigureAwait(false);
if (count == 0) break;
length += count;
}
if (length != bytes.Length) Array.Resize(ref bytes, length);
return bytes;
}
private static SourceInput OpenSource(string path)
{
var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete,
bufferSize: SniffBytes, useAsync: true);
try
{
var extension = Path.GetExtension(path);
if (extension.Equals(".svgz", StringComparison.OrdinalIgnoreCase))
return new SourceInput(new GZipStream(file, CompressionMode.Decompress), null, null, "Исходный SVG/XML из SVGZ");
if (extension.Equals(".3mf", StringComparison.OrdinalIgnoreCase) || extension.Equals(".fcstd", StringComparison.OrdinalIgnoreCase))
{
if (file.Length > MaxContainerBytes)
throw new NotSupportedException("Контейнер модели слишком большой для текстового предпросмотра.");
var zip = new ZipArchive(file, ZipArchiveMode.Read);
try
{
if (zip.Entries.Count > 20_000)
throw new NotSupportedException("Контейнер модели содержит слишком много частей.");
var is3mf = extension.Equals(".3mf", StringComparison.OrdinalIgnoreCase);
var name = is3mf ? "3D/3dmodel.model" : "Document.xml";
var entry = zip.Entries.FirstOrDefault(part => part.FullName.Equals(name, StringComparison.OrdinalIgnoreCase));
if (entry is null) throw new NotSupportedException("В контейнере модели нет доступного исходного XML.");
return new SourceInput(entry.Open(), zip, entry.Length,
is3mf ? "XML модели внутри 3MF" : "XML документа внутри FCStd");
}
catch { zip.Dispose(); throw; }
}
return new SourceInput(file, null, file.Length, "Исходный текст");
}
catch { file.Dispose(); throw; }
}
private sealed class SourceInput : IDisposable
{
private readonly IDisposable? _container;
public Stream Stream { get; }
public long? Length { get; }
public string Label { get; }
public SourceInput(Stream stream, IDisposable? container, long? length, string label)
{ Stream = stream; _container = container; Length = length; Label = label; }
public void Dispose() { try { Stream.Dispose(); } finally { _container?.Dispose(); } }
}
}