mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-06 02:31:36 +00:00
Improved reading and writing large Office files without copying them into memory (#1243)
This commit is contained in:
1 parent
918572c158
commit
dbe609a9a7
37 files changed
+1161
-471
No files matched your search
@@ -1,3 +1,4 @@
|
||||
using System.IO.Packaging;
|
||||
using System.Text;
|
||||
|
||||
using AIStudio.Tools.PowerPoint;
|
||||
@@ -226,6 +227,44 @@ internal static class TestTemplate
|
||||
return PresentationTemplate.Read(stream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a video to a slide of a presentation file as PowerPoint stores it: uncompressed, behind the slides, which
|
||||
/// the update mode of ZipArchive would load into memory.
|
||||
/// </summary>
|
||||
/// <param name="path">The presentation file.</param>
|
||||
/// <param name="slideNumber">The 1-based number of the slide in the order of the presentation.</param>
|
||||
/// <param name="video">The content of the video.</param>
|
||||
/// <param name="videoPartName">The name of the part of the video.</param>
|
||||
public static void AddVideo(string path, int slideNumber, byte[] video, string videoPartName = "/ppt/media/video1.mp4")
|
||||
{
|
||||
Uri slideUri;
|
||||
using (var document = PresentationDocument.Open(path, false))
|
||||
{
|
||||
var presentationPart = document.PresentationPart!;
|
||||
var slideId = presentationPart.Presentation!.SlideIdList!.Elements<P.SlideId>().ElementAt(slideNumber - 1);
|
||||
slideUri = presentationPart.GetPartById(slideId.RelationshipId!.Value!).Uri;
|
||||
}
|
||||
|
||||
using var package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite);
|
||||
var videoPart = package.CreatePart(new Uri(videoPartName, UriKind.Relative), "video/mp4", CompressionOption.NotCompressed);
|
||||
using (var stream = videoPart.GetStream())
|
||||
stream.Write(video);
|
||||
|
||||
package.GetPart(slideUri).CreateRelationship(videoPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/video");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The content of a video for a test, which no compression shrinks.
|
||||
/// </summary>
|
||||
/// <param name="size">The size in bytes.</param>
|
||||
/// <param name="seed">Tells videos of the same size apart.</param>
|
||||
public static byte[] VideoOf(int size, int seed = 1179)
|
||||
{
|
||||
var video = new byte[size];
|
||||
new Random(seed).NextBytes(video);
|
||||
return video;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The shape tree of a layout, a master or a slide.
|
||||
/// </summary>
|
||||
@@ -423,6 +462,34 @@ internal sealed record TestElement(Func<uint, SlidePart, OpenXmlElement> Create)
|
||||
new P.ShapeProperties(TransformOf(bounds), new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle }));
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// A video as PowerPoint embeds one: a picture with a poster frame, which refers to the video twice, once for older
|
||||
/// versions of PowerPoint and once for newer ones.
|
||||
/// </summary>
|
||||
public static TestElement Video(SlotBounds bounds, params byte[] data) => new((id, slidePart) =>
|
||||
{
|
||||
var mediaPart = ((PresentationDocument)slidePart.OpenXmlPackage).CreateMediaDataPart(MediaDataPartType.Mp4);
|
||||
using (var stream = new MemoryStream(data))
|
||||
mediaPart.FeedData(stream);
|
||||
|
||||
var posterPart = slidePart.AddImagePart("image/png");
|
||||
using (var stream = new MemoryStream([1, 2, 3]))
|
||||
posterPart.FeedData(stream);
|
||||
|
||||
var videoId = slidePart.AddVideoReferenceRelationship(mediaPart).Id;
|
||||
var mediaId = slidePart.AddMediaReferenceRelationship(mediaPart).Id;
|
||||
return new P.Picture(
|
||||
new P.NonVisualPictureProperties(
|
||||
new P.NonVisualDrawingProperties { Id = id, Name = $"Video {id}" },
|
||||
new P.NonVisualPictureDrawingProperties(new A.PictureLocks { NoChangeAspect = true }),
|
||||
new P.ApplicationNonVisualDrawingProperties(
|
||||
new A.VideoFromFile { Link = videoId },
|
||||
new P.ApplicationNonVisualDrawingPropertiesExtensionList(
|
||||
new P.ApplicationNonVisualDrawingPropertiesExtension(new P14.Media { Embed = mediaId }) { Uri = "{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}" }))),
|
||||
new P.BlipFill(new A.Blip { Embed = slidePart.GetIdOfPart(posterPart) }, new A.Stretch(new A.FillRectangle())),
|
||||
new P.ShapeProperties(TransformOf(bounds), new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle }));
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// A bar chart without data, with a chart part of its own.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user