|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Globalization; |
| 6 | +using System.IO.Abstractions; |
| 7 | +using Elastic.Markdown.Diagnostics; |
| 8 | + |
| 9 | +namespace Elastic.Markdown.Myst.Directives.CsvInclude; |
| 10 | + |
| 11 | +public class CsvIncludeBlock(DirectiveBlockParser parser, ParserContext context) : DirectiveBlock(parser, context) |
| 12 | +{ |
| 13 | + public override string Directive => "csv-include"; |
| 14 | + |
| 15 | + public string? CsvFilePath { get; private set; } |
| 16 | + public string? CsvFilePathRelativeToSource { get; private set; } |
| 17 | + public bool Found { get; private set; } |
| 18 | + public string? Caption { get; private set; } |
| 19 | + public string Separator { get; private set; } = ","; |
| 20 | + public int MaxRows { get; private set; } = 25000; |
| 21 | + public long MaxFileSizeBytes { get; private set; } = 10 * 1024 * 1024; // 10MB |
| 22 | + public int MaxColumns { get; private set; } = 10; |
| 23 | + |
| 24 | + public override void FinalizeAndValidate(ParserContext context) |
| 25 | + { |
| 26 | + Caption = Prop("caption"); |
| 27 | + |
| 28 | + var separator = Prop("separator", "delimiter"); |
| 29 | + if (!string.IsNullOrEmpty(separator)) |
| 30 | + Separator = separator; |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + ExtractCsvPath(context); |
| 35 | + } |
| 36 | + |
| 37 | + private void ExtractCsvPath(ParserContext context) |
| 38 | + { |
| 39 | + var csvPath = Arguments; |
| 40 | + if (string.IsNullOrWhiteSpace(csvPath)) |
| 41 | + { |
| 42 | + this.EmitError("csv-include requires an argument specifying the path to the CSV file."); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + var csvFrom = context.MarkdownSourcePath.Directory!.FullName; |
| 47 | + if (csvPath.StartsWith('/')) |
| 48 | + csvFrom = Build.DocumentationSourceDirectory.FullName; |
| 49 | + |
| 50 | + CsvFilePath = Path.Combine(csvFrom, csvPath.TrimStart('/')); |
| 51 | + CsvFilePathRelativeToSource = Path.GetRelativePath(Build.DocumentationSourceDirectory.FullName, CsvFilePath); |
| 52 | + |
| 53 | + if (Build.ReadFileSystem.File.Exists(CsvFilePath)) |
| 54 | + { |
| 55 | + ValidateFileSize(); |
| 56 | + Found = true; |
| 57 | + } |
| 58 | + else |
| 59 | + this.EmitError($"CSV file `{CsvFilePath}` does not exist."); |
| 60 | + } |
| 61 | + |
| 62 | + private void ValidateFileSize() |
| 63 | + { |
| 64 | + if (CsvFilePath == null) |
| 65 | + return; |
| 66 | + |
| 67 | + try |
| 68 | + { |
| 69 | + var fileInfo = Build.ReadFileSystem.FileInfo.New(CsvFilePath); |
| 70 | + if (fileInfo.Length > MaxFileSizeBytes) |
| 71 | + { |
| 72 | + var sizeMB = fileInfo.Length / (1024.0 * 1024.0); |
| 73 | + var maxSizeMB = MaxFileSizeBytes / (1024.0 * 1024.0); |
| 74 | + this.EmitError($"CSV file `{CsvFilePath}` is {sizeMB:F1}MB, which exceeds the maximum allowed size of {maxSizeMB:F1}MB."); |
| 75 | + Found = false; |
| 76 | + } |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + this.EmitError($"Could not validate CSV file size: {ex.Message}"); |
| 81 | + Found = false; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments