Skip to content

Commit fa893dd

Browse files
committed
use feature for minimum document
1 parent 9bc42e2 commit fa893dd

File tree

4 files changed

+46
-66
lines changed

4 files changed

+46
-66
lines changed

src/DocumentFormat.OpenXml.Framework/Builder/OpenXmlPackageBuilderExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,15 @@ public static IPackageBuilder<TPackage> Use<TPackage>(this IPackageBuilder<TPack
110110

111111
internal static IPackageBuilder<TPackage> UseSettings<TPackage>(this IPackageBuilder<TPackage> builder, OpenSettings settings)
112112
where TPackage : OpenXmlPackage
113-
=> builder.Use(package => package.OpenSettings = settings);
113+
=> builder.Use(package =>
114+
{
115+
package.OpenSettings = settings;
116+
117+
if (settings.VerifyMinimumPackage && package.Features.Get<IMinimumDocumentFeature>() is { } minimumFeature && !minimumFeature.Validate())
118+
{
119+
throw new FileFormatException("The provided package does not conform to the minimum requirements to open.");
120+
}
121+
});
114122

115123
internal static IPackageBuilder<TPackage> UseDefaultBehaviorAndLockBuilder<TPackage>(this IPackageBuilder<TPackage> builder)
116124
where TPackage : OpenXmlPackage
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace DocumentFormat.OpenXml.Features;
5+
6+
internal interface IMinimumDocumentFeature
7+
{
8+
bool Validate();
9+
}

src/DocumentFormat.OpenXml/Packaging/PresentationDocument.cs

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,6 @@ public static PresentationDocument Open(Package package)
232232
public static PresentationDocument Open(string path, bool isEditable, OpenSettings openSettings)
233233
=> CreateDefaultBuilder()
234234
.UseSettings(openSettings)
235-
.Use(package =>
236-
{
237-
if (openSettings.VerifyMinimumPackage)
238-
{
239-
package.ThrowIfNotMinimumPackage();
240-
}
241-
})
242235
.Build()
243236
.Open(path, isEditable);
244237

@@ -256,13 +249,6 @@ public static PresentationDocument Open(string path, bool isEditable, OpenSettin
256249
public static PresentationDocument Open(Stream stream, bool isEditable, OpenSettings openSettings)
257250
=> CreateDefaultBuilder()
258251
.UseSettings(openSettings)
259-
.Use(package =>
260-
{
261-
if (openSettings.VerifyMinimumPackage)
262-
{
263-
package.ThrowIfNotMinimumPackage();
264-
}
265-
})
266252
.Build()
267253
.Open(stream, isEditable);
268254

@@ -279,58 +265,9 @@ public static PresentationDocument Open(Stream stream, bool isEditable, OpenSett
279265
public static PresentationDocument Open(Package package, OpenSettings openSettings)
280266
=> CreateDefaultBuilder()
281267
.UseSettings(openSettings)
282-
.Use(package =>
283-
{
284-
if (openSettings.VerifyMinimumPackage)
285-
{
286-
package.ThrowIfNotMinimumPackage();
287-
}
288-
})
289268
.Build()
290269
.Open(package);
291270

292-
/// <summary>
293-
/// Validates that the current <see cref="PresentationDocument"/> meets the minimum requirements for a valid package.
294-
/// </summary>
295-
/// <exception cref="NotSupportedException">
296-
/// Thrown if the <see cref="PresentationDocumentType"/> is <see cref="PresentationDocumentType.Slideshow"/>,
297-
/// <see cref="PresentationDocumentType.MacroEnabledSlideshow"/>, or <see cref="PresentationDocumentType.AddIn"/>,
298-
/// as validation for these types is not supported.
299-
/// </exception>
300-
/// <exception cref="FileFormatException">
301-
/// Thrown if the <see cref="PresentationPart"/> does not contain valid <c>NotesSize</c> dimensions
302-
/// or if the dimensions are outside the acceptable range for PowerPoint to open.
303-
/// </exception>
304-
/// <remarks>
305-
/// This method ensures that the document conforms to the minimum requirements for PowerPoint to open it.
306-
/// </remarks>
307-
protected override void ThrowIfNotMinimumPackage()
308-
{
309-
if (this.DocumentType == PresentationDocumentType.Slideshow)
310-
{
311-
throw new NotSupportedException("Minimum package verification for PresentationDocumentType.Slideshow (.ppsx) is not supported.");
312-
}
313-
314-
if (this.DocumentType == PresentationDocumentType.MacroEnabledSlideshow)
315-
{
316-
throw new NotSupportedException("Minimum package verification for PresentationDocumentType.MacroEnabledSlideshow (.ppsm) is not supported.");
317-
}
318-
319-
if (this.DocumentType == PresentationDocumentType.AddIn)
320-
{
321-
throw new NotSupportedException("Minimum package verification for PresentationDocumentType.AddIn (.ppam) is not supported.");
322-
}
323-
324-
NotesSize? notesSize = this.PresentationPart?.Presentation?.NotesSize;
325-
326-
if (!(notesSize is not null && notesSize.Cx is not null && notesSize.Cx.HasValue &&
327-
notesSize.Cx >= 0 && notesSize.Cx <= 27273042316900 && notesSize.Cy is not null &&
328-
notesSize.Cy.HasValue && notesSize.Cy >= 0 && notesSize.Cy <= 27273042316900))
329-
{
330-
throw new FileFormatException("The provided package does not conform to the minimum requirements for PowerPoint to open.");
331-
}
332-
}
333-
334271
/// <summary>
335272
/// Changes the document type.
336273
/// </summary>
@@ -570,7 +507,8 @@ public LabelInfoPart? LabelInfoPart
570507
private partial class PresentationDocumentFeatures : TypedPackageFeatureCollection<PresentationDocumentType, PresentationPart>,
571508
IApplicationTypeFeature,
572509
IMainPartFeature,
573-
IProgrammaticIdentifierFeature
510+
IProgrammaticIdentifierFeature,
511+
IMinimumDocumentFeature
574512
{
575513
public PresentationDocumentFeatures(OpenXmlPackage package)
576514
: base(package)
@@ -608,6 +546,25 @@ public PresentationDocumentFeatures(OpenXmlPackage package)
608546
"application/vnd.ms-powerpoint.addin.macroEnabled.main+xml" => PresentationDocumentType.AddIn,
609547
_ => default,
610548
};
549+
550+
bool IMinimumDocumentFeature.Validate()
551+
{
552+
if (DocumentType is PresentationDocumentType.Slideshow or PresentationDocumentType.MacroEnabledSlideshow or PresentationDocumentType.AddIn)
553+
{
554+
return false;
555+
}
556+
557+
NotesSize? notesSize = MainPart?.Presentation?.NotesSize;
558+
559+
if (!(notesSize is not null && notesSize.Cx is not null && notesSize.Cx.HasValue &&
560+
notesSize.Cx >= 0 && notesSize.Cx <= 27273042316900 && notesSize.Cy is not null &&
561+
notesSize.Cy.HasValue && notesSize.Cy >= 0 && notesSize.Cy <= 27273042316900))
562+
{
563+
return false;
564+
}
565+
566+
return true;
567+
}
611568
}
612569
}
613570
}

src/DocumentFormat.OpenXml/Packaging/TypedPackageFeatureCollection.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ private TDocumentType EnsureDocumentType()
6262
return _documentType.Value;
6363
}
6464

65-
TDocumentType IDocumentTypeFeature<TDocumentType>.Current
65+
protected TDocumentType DocumentType
6666
{
6767
get => EnsureDocumentType();
6868
set => _documentType = value;
6969
}
7070

71+
TDocumentType IDocumentTypeFeature<TDocumentType>.Current
72+
{
73+
get => DocumentType;
74+
set => DocumentType = value;
75+
}
76+
7177
protected TMainPart? MainPart => Package.GetSubPartOfType<TMainPart>();
7278

7379
OpenXmlPart? IMainPartFeature.Part => MainPart;

0 commit comments

Comments
 (0)