Introduced the `InvoiceAttachment` entity and its relationship with `ZugferdInvoice` to manage extracted invoice attachments. Updated `AppDbContext` and added a migration to create the `InvoiceAttachments` table with cascading delete behavior and an index for optimized queries. Enhanced the UI to display attachments in `Details.cshtml`, including file type icons, file size, and extraction date. Added a new `ViewAttachment` page to render or download attachments based on their type, with support for XML, plain text, images, and downloads. Implemented `AttachmentViewerService` to determine viewer types and MIME types for attachments. Registered the service in the DI container. Updated `Upload.cshtml.cs` to save extracted attachments to the database. Improved user experience with syntax highlighting for XML files and appropriate messages for unsupported file types.
50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using DXApp.TemplateKitProject.Models;
|
|
|
|
namespace DXApp.TemplateKitProject.Services;
|
|
|
|
public class AttachmentViewerService
|
|
{
|
|
public AttachmentViewerType DetermineViewerType(string fileName)
|
|
{
|
|
var extension = Path.GetExtension(fileName).ToLowerInvariant();
|
|
|
|
return extension switch
|
|
{
|
|
".pdf" => AttachmentViewerType.Pdf,
|
|
".xml" => AttachmentViewerType.Xml,
|
|
".txt" => AttachmentViewerType.Text,
|
|
".jpg" or ".jpeg" or ".png" or ".gif" => AttachmentViewerType.Image,
|
|
".docx" or ".doc" => AttachmentViewerType.Word,
|
|
_ => AttachmentViewerType.Download
|
|
};
|
|
}
|
|
|
|
public string GetMimeType(string fileName)
|
|
{
|
|
var extension = Path.GetExtension(fileName).ToLowerInvariant();
|
|
|
|
return extension switch
|
|
{
|
|
".pdf" => "application/pdf",
|
|
".xml" => "application/xml",
|
|
".txt" => "text/plain",
|
|
".jpg" or ".jpeg" => "image/jpeg",
|
|
".png" => "image/png",
|
|
".gif" => "image/gif",
|
|
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
".doc" => "application/msword",
|
|
_ => "application/octet-stream"
|
|
};
|
|
}
|
|
}
|
|
|
|
public enum AttachmentViewerType
|
|
{
|
|
Pdf, // PDF.js Viewer
|
|
Xml, // Syntax-highlighted XML
|
|
Text, // Plain Text
|
|
Image, // <img> Tag
|
|
Word, // Konvertierung zu PDF (optional)
|
|
Download // Nur Download-Button
|
|
}
|