-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (59 loc) · 2.03 KB
/
Program.cs
File metadata and controls
72 lines (59 loc) · 2.03 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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using MimeKit;
using System.IO;
using System.Linq;
var builder = WebApplication.CreateBuilder(args);
// Minimal API + Swagger only
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// FIX: Remove the [ValidateAntiForgeryToken] metadata by disabling antiforgery for this endpoint.
// Add .DisableAntiforgery() to the endpoint mapping.
app.MapPost("/api/emlparser/parse", async ([FromBody] EmlParserApi.EmlFileRequest request) =>
{
if (request == null || string.IsNullOrEmpty(request.Base64Content))
return Results.BadRequest("File content is missing.");
// Convert base64 to stream
var bytes = Convert.FromBase64String(request.Base64Content);
using var stream = new MemoryStream(bytes);
// Load MIME message
var message = MimeKit.MimeMessage.Load(stream);
// Extract attachments
var attachments = message.Attachments.Select(a =>
{
using var ms = new MemoryStream();
if (a is MimeKit.MimePart part)
part.Content.DecodeTo(ms);
else if (a is MimeKit.MessagePart rfc822)
rfc822.Message.WriteTo(ms);
return new
{
FileName = a.ContentDisposition?.FileName ?? a.ContentType.Name,
Base64Content = Convert.ToBase64String(ms.ToArray())
};
}).ToList();
var result = new
{
From = message.From.ToString(),
To = message.To.ToString(),
Subject = message.Subject,
Date = message.Date.ToString(),
BodyText = message.TextBody,
BodyHtml = message.HtmlBody,
Attachments = attachments
};
return Results.Ok(result);
})
.Accepts<EmlParserApi.EmlFileRequest>("application/json")
.Produces(StatusCodes.Status200OK)
.Produces(StatusCodes.Status400BadRequest)
.DisableAntiforgery();
app.Run();