477 lines
15 KiB
C#
477 lines
15 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using xAiModels.Models;
|
|
using xCommons.Extensions;
|
|
using OfficeIMO.PowerPoint;
|
|
using xExceptions.Constants;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using xAiApi.Interfaces.Extractors;
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
using DrawingText = DocumentFormat.OpenXml.Drawing.Text;
|
|
|
|
namespace xAiApi.Providers.Extractors
|
|
{
|
|
/// <summary>
|
|
/// Extracts content from all PowerPoint formats
|
|
/// (OpenXML: pptx/ppsx/potx/pptm/ppsm/potm | Legacy Binary: ppt/pps/pot)
|
|
/// </summary>
|
|
public class XPowerPointFileContentExtractor : IXPowerPointFileContentExtractor
|
|
{
|
|
/// <summary>
|
|
/// Supported MIME Types ...
|
|
/// </summary>
|
|
private static readonly string[] SupportedMimeTypes =
|
|
[
|
|
// OpenXML Formats
|
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation", // .pptx
|
|
"application/vnd.openxmlformats-officedocument.presentationml.slideshow", // .ppsx
|
|
"application/vnd.openxmlformats-officedocument.presentationml.template", // .potx
|
|
"application/vnd.ms-powerpoint.presentation.macroEnabled.12", // .pptm
|
|
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12", // .ppsm
|
|
"application/vnd.ms-powerpoint.template.macroEnabled.12", // .potm
|
|
// Legacy Binary Formats (.ppt / .pps / .pot share same MIME)
|
|
"application/vnd.ms-powerpoint"
|
|
];
|
|
|
|
/// <summary>
|
|
/// Check if this extractor supports the specified MIME type ...
|
|
/// </summary>
|
|
public bool CanExtract(string mimeType)
|
|
{
|
|
//
|
|
return SupportedMimeTypes.Contains(
|
|
mimeType?.ToLowerInvariant() ?? string.Empty
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract text content from file stream ...
|
|
/// </summary>
|
|
public async Task<string> ExtractAsync(
|
|
Stream fileStream,
|
|
string mimeType,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await ExtractRichAsync(
|
|
fileStream: fileStream,
|
|
fileName: "presentation",
|
|
mimeType: mimeType,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
return result.Text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract content from stream as Rich Result ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileExtractionResult> ExtractRichAsync(
|
|
Stream fileStream,
|
|
string fileName,
|
|
string mimeType,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = new XFileExtractionResult
|
|
{
|
|
FileName = fileName,
|
|
MimeType = mimeType
|
|
};
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
if (IsOpenXmlFormat(mimeType))
|
|
{
|
|
//
|
|
result.Text = await ExtractOpenXmlAsync(
|
|
fileStream,
|
|
cancellationToken
|
|
);
|
|
}
|
|
else if (IsLegacyFormat(mimeType))
|
|
{
|
|
//
|
|
result.Text = await ExtractLegacyAsync(
|
|
fileStream,
|
|
cancellationToken
|
|
);
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result.ErrorMessage =
|
|
$"Unsupported PowerPoint MIME type: {mimeType}";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage =
|
|
$"PowerPoint extraction failed: {ex.Message}";
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Open XML Format ...
|
|
/// </summary>
|
|
/// <param name="mimeType"></param>
|
|
/// <returns></returns>
|
|
private bool IsOpenXmlFormat(string mimeType)
|
|
{
|
|
//
|
|
var result = mimeType?.ToLowerInvariant() switch
|
|
{
|
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation" => true,
|
|
"application/vnd.openxmlformats-officedocument.presentationml.slideshow" => true,
|
|
"application/vnd.openxmlformats-officedocument.presentationml.template" => true,
|
|
"application/vnd.ms-powerpoint.presentation.macroEnabled.12" => true,
|
|
"application/vnd.ms-powerpoint.slideshow.macroEnabled.12" => true,
|
|
"application/vnd.ms-powerpoint.template.macroEnabled.12" => true,
|
|
_ => false
|
|
};
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Legacy Format ...
|
|
/// </summary>
|
|
/// <param name="mimeType"></param>
|
|
/// <returns></returns>
|
|
private bool IsLegacyFormat(string mimeType)
|
|
{
|
|
//
|
|
var result = mimeType?.ToLowerInvariant() == "application/vnd.ms-powerpoint";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract an Slide Title ...
|
|
/// </summary>
|
|
/// <param name="slidePart"></param>
|
|
/// <returns></returns>
|
|
private string GetSlideTitle(SlidePart slidePart)
|
|
{
|
|
//
|
|
var result = string.Empty;
|
|
|
|
//
|
|
// Title placeholder has specific type
|
|
var titleShape = slidePart.Slide
|
|
.Descendants<DocumentFormat.OpenXml.Presentation.Shape>()
|
|
.FirstOrDefault(s => s.NonVisualShapeProperties?
|
|
.NonVisualDrawingProperties?
|
|
.Name?.Value?
|
|
.StartsWith("Title", StringComparison.OrdinalIgnoreCase) == true
|
|
);
|
|
if (titleShape != null)
|
|
{
|
|
//
|
|
var titleText = titleShape
|
|
.Descendants<DrawingText>()
|
|
.Select(t => t.Text?.Trim())
|
|
.Where(t => !string.IsNullOrWhiteSpace(t));
|
|
result = string.Join(" ", titleText);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Speaker Notes ...
|
|
/// </summary>
|
|
/// <param name="slidePart"></param>
|
|
/// <returns></returns>
|
|
private string GetSpeakerNotes(SlidePart slidePart)
|
|
{
|
|
//
|
|
var result = string.Empty;
|
|
|
|
//
|
|
var notesSlidePart = slidePart.NotesSlidePart;
|
|
if (notesSlidePart?.NotesSlide == null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
var notesTexts = notesSlidePart.NotesSlide
|
|
.Descendants<DrawingText>()
|
|
.Select(t => t.Text?.Trim())
|
|
.Where(t => !string.IsNullOrWhiteSpace(t))
|
|
.ToList();
|
|
|
|
//
|
|
result = notesTexts.Count > 0
|
|
? string.Join(Environment.NewLine, notesTexts)
|
|
: null;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract OpenXML Tables ...
|
|
/// </summary>
|
|
/// <param name="slidePart"></param>
|
|
/// <returns></returns>
|
|
private static string ExtractOpenXmlTables(SlidePart slidePart)
|
|
{
|
|
//
|
|
var sb = new StringBuilder();
|
|
|
|
//
|
|
var tableElements = slidePart.Slide
|
|
.Descendants<DocumentFormat.OpenXml.Drawing.Table>();
|
|
|
|
//
|
|
var tableIndex = 0;
|
|
foreach (var table in tableElements)
|
|
{
|
|
//
|
|
tableIndex++;
|
|
sb.AppendLine($" Table {tableIndex}:");
|
|
|
|
//
|
|
foreach (var row in table
|
|
.Descendants<DocumentFormat.OpenXml.Drawing.TableRow>())
|
|
{
|
|
//
|
|
var cells = row
|
|
.Descendants<DocumentFormat.OpenXml.Drawing.TableCell>()
|
|
.Select(c => string.Join(" ", c
|
|
.Descendants<DrawingText>()
|
|
.Select(t => t.Text?.Trim())
|
|
.Where(t => !string.IsNullOrWhiteSpace(t))))
|
|
.ToList();
|
|
|
|
//
|
|
if (cells.Count > 0)
|
|
{
|
|
sb.AppendLine($" | {string.Join(" | ", cells)} |");
|
|
}
|
|
}
|
|
|
|
//
|
|
sb.AppendLine();
|
|
}
|
|
|
|
//
|
|
var result = sb.ToString();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Open XML Format ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> ExtractOpenXmlAsync(
|
|
Stream fileStream,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
var sb = new StringBuilder();
|
|
|
|
//
|
|
// Copy to MemoryStream (OpenXml requires seekable stream)
|
|
await using var ms = new MemoryStream();
|
|
await fileStream.CopyToAsync(ms, cancellationToken);
|
|
ms.Position = 0;
|
|
|
|
//
|
|
using var presentationDocument = PresentationDocument.Open(ms, false);
|
|
var presentationPart = presentationDocument.PresentationPart;
|
|
if (presentationPart == null)
|
|
{
|
|
XException.InvalidData.Throw();
|
|
}
|
|
|
|
//
|
|
var slideParts = presentationPart.SlideParts.ToList();
|
|
var slideIndex = 0;
|
|
foreach (var slidePart in slideParts)
|
|
{
|
|
//
|
|
if (cancellationToken.IsCancellationRequested) break;
|
|
slideIndex++;
|
|
|
|
//
|
|
sb.AppendLine($"[Slide {slideIndex}]");
|
|
|
|
//
|
|
// 1. Extract slide title
|
|
var slideTitle = GetSlideTitle(slidePart);
|
|
if (!string.IsNullOrWhiteSpace(slideTitle))
|
|
{
|
|
sb.AppendLine($"Title: {slideTitle}");
|
|
}
|
|
|
|
//
|
|
// 2. Extract body texts
|
|
var bodyTexts = slidePart.Slide
|
|
.Descendants<DrawingText>()
|
|
.Select(t => t.Text?.Trim())
|
|
.Where(t => !string.IsNullOrWhiteSpace(t) && t != slideTitle)
|
|
.ToList();
|
|
if (bodyTexts.Count > 0)
|
|
{
|
|
//
|
|
sb.AppendLine("Content:");
|
|
foreach (var text in bodyTexts)
|
|
{
|
|
sb.AppendLine($" - {text}");
|
|
}
|
|
}
|
|
|
|
//
|
|
// 3. Extract tables
|
|
var tables = ExtractOpenXmlTables(slidePart);
|
|
if (!string.IsNullOrWhiteSpace(tables))
|
|
{
|
|
//
|
|
sb.AppendLine("[Tables]");
|
|
sb.AppendLine(tables);
|
|
}
|
|
|
|
//
|
|
// 4. Extract speaker notes
|
|
var notes = GetSpeakerNotes(slidePart);
|
|
if (!string.IsNullOrWhiteSpace(notes))
|
|
{
|
|
//
|
|
sb.AppendLine("[Speaker Notes]");
|
|
sb.AppendLine(notes);
|
|
}
|
|
|
|
//
|
|
// 5. Image count metadata
|
|
var imageCount = slidePart.ImageParts?.Count() ?? 0;
|
|
if (imageCount > 0)
|
|
{
|
|
sb.AppendLine($"[Images: {imageCount}]");
|
|
}
|
|
|
|
//
|
|
sb.AppendLine();
|
|
}
|
|
|
|
//
|
|
var result = sb.ToString().TrimEnd();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Legacy Format ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> ExtractLegacyAsync(
|
|
Stream fileStream,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
var sb = new StringBuilder();
|
|
|
|
//
|
|
await using var ms = new MemoryStream();
|
|
await fileStream.CopyToAsync(ms, cancellationToken);
|
|
ms.Position = 0;
|
|
|
|
//
|
|
using var presentation = PowerPointPresentation.Load(ms);
|
|
var slideIndex = 0;
|
|
foreach (var slide in presentation.Slides)
|
|
{
|
|
//
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
slideIndex++;
|
|
|
|
//
|
|
sb.AppendLine($"[Slide {slideIndex}]");
|
|
|
|
//
|
|
var textParts = new List<string>();
|
|
var imageCount = 0;
|
|
foreach (var shape in slide.Shapes)
|
|
{
|
|
//
|
|
if (shape is PowerPointTextBox textBox)
|
|
{
|
|
//
|
|
if (!textBox.Text.IsNullOrEmpty())
|
|
{
|
|
textParts.Add(textBox.Text.Trim());
|
|
}
|
|
}
|
|
else if (shape is PowerPointAutoShape autoShape)
|
|
{
|
|
//
|
|
if (!autoShape.AltText.IsNullOrEmpty())
|
|
{
|
|
textParts.Add(autoShape.AltText.Trim());
|
|
}
|
|
}
|
|
else if (shape is PowerPointPicture picture)
|
|
{
|
|
imageCount++;
|
|
}
|
|
}
|
|
|
|
//
|
|
if (textParts.Count > 0)
|
|
{
|
|
//
|
|
sb.AppendLine("Content:");
|
|
foreach (var text in textParts)
|
|
{
|
|
sb.AppendLine($" - {text}");
|
|
}
|
|
}
|
|
|
|
//
|
|
if (imageCount > 0)
|
|
{
|
|
sb.AppendLine($"[Images: {imageCount}]");
|
|
}
|
|
|
|
//
|
|
sb.AppendLine();
|
|
}
|
|
|
|
//
|
|
var result = sb.ToString().TrimEnd();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |