Skip to content

Commit d3b5dfd

Browse files
committed
更新 9 月版
1 parent 4a54912 commit d3b5dfd

96 files changed

Lines changed: 30140 additions & 182 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/Compatibilities/Packaging/CompatiblePackage.cs

Lines changed: 1101 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.IO;
2+
using System.IO.Packaging;
3+
4+
namespace DocumentFormat.OpenXml.Flatten.Compatibilities.Packaging;
5+
6+
/// <summary>
7+
/// 带兼容处理的 Package 提供器
8+
/// </summary>
9+
public static class CompatiblePackageProvider
10+
{
11+
/// <summary>
12+
/// 从传入的 <paramref name="s"/> 打开 <see cref="CompatiblePackage"/> 对象
13+
/// </summary>
14+
/// <param name="s"></param>
15+
/// <param name="packageFileMode"></param>
16+
/// <param name="packageFileAccess"></param>
17+
/// <returns></returns>
18+
public static Package OpenPackage(Stream s, FileMode packageFileMode,
19+
FileAccess packageFileAccess)
20+
{
21+
return CompatiblePackage.OpenPackage(s, packageFileMode, packageFileAccess);
22+
}
23+
}

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/Compatibilities/Packaging/ContentType.cs

Lines changed: 544 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.IO;
3+
using System.IO.Packaging;
4+
using System.Text.RegularExpressions;
5+
6+
namespace DocumentFormat.OpenXml.Flatten.Compatibilities.Packaging;
7+
8+
partial class CompatiblePackage
9+
{
10+
private class EmptyPackagePart : PackagePart
11+
{
12+
private Package _package;
13+
private readonly ContentTypeHelper _contentTypeHelper;
14+
public EmptyPackagePart(Package package, Uri partUri, ContentTypeHelper contentTypeHelper) : base(package, partUri)
15+
{
16+
_package = package;
17+
_contentTypeHelper = contentTypeHelper;
18+
}
19+
20+
protected override string GetContentTypeCore()
21+
{
22+
const string UnknownType = "application/vnd.openxmlformats-officedocument.miss-content-unknown-type";
23+
24+
var validatedPartUri = PackUriHelper.GetRelationshipPartUri(Uri) as PackUriHelper.ValidatedPartUri;
25+
if (validatedPartUri == null)
26+
{
27+
return UnknownType;
28+
}
29+
30+
var sourcePartUriFromRelationshipPartUri = PackUriHelper.GetSourcePartUriFromRelationshipPartUri(validatedPartUri) as PackUriHelper.ValidatedPartUri;
31+
if (sourcePartUriFromRelationshipPartUri is not null)
32+
{
33+
var contentType = GetContentType(sourcePartUriFromRelationshipPartUri);
34+
if (!string.IsNullOrEmpty(contentType?.ToString()))
35+
{
36+
return contentType!.ToString();
37+
}
38+
}
39+
return UnknownType;
40+
}
41+
42+
internal CompatiblePackage.ContentType? GetContentType(CompatiblePackage.PackUriHelper.ValidatedPartUri partUri)
43+
{
44+
//Step 1: Check if there is an override entry present corresponding to the
45+
//partUri provided. Override takes precedence over the default entries
46+
var overrideDictionary = _contentTypeHelper.GetOverrideDictionary();
47+
if (overrideDictionary != null)
48+
{
49+
if (overrideDictionary.ContainsKey(partUri))
50+
return overrideDictionary[partUri];
51+
}
52+
53+
//Step 2: 通过路径进行特殊判断
54+
// 例如 Slide 几的页面路径
55+
var uri = partUri.OriginalString;
56+
if (Regex.IsMatch(uri, @"/ppt/slides/slide\d+.xml"))
57+
{
58+
// "/ppt/slides/slide0.xml"
59+
return new CompatiblePackage.ContentType(
60+
"application/vnd.openxmlformats-officedocument.presentationml.slide+xml");
61+
}
62+
63+
if (Regex.IsMatch(uri, @"/ppt/slideLayouts/slideLayout\d+\.xml"))
64+
{
65+
// "/ppt/slideLayouts/slideLayout0.xml"
66+
return new CompatiblePackage.ContentType("application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml");
67+
}
68+
69+
if (Regex.IsMatch(uri, @"/ppt/slideMasters/slideMaster\d\.xml"))
70+
{
71+
// "/ppt/slideMasters/slideMaster0.xml"
72+
return new CompatiblePackage.ContentType("application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml");
73+
}
74+
75+
if (Regex.IsMatch(uri, @"/ppt/theme/theme\d+\.xml"))
76+
{
77+
// "/ppt/theme/theme0.xml"
78+
return new CompatiblePackage.ContentType("application/vnd.openxmlformats-officedocument.theme+xml");
79+
}
80+
81+
if (Regex.IsMatch(uri, @"/ppt/notesMasters/notesMaster\d+\.xml"))
82+
{
83+
// "/ppt/notesMasters/notesMaster0.xml"
84+
return new CompatiblePackage.ContentType("application/vnd.openxmlformats-officedocument.presentationml.notesMaster+xml");
85+
}
86+
87+
//Step 3: Check if there is a default entry corresponding to the
88+
//extension of the partUri provided.
89+
string extension = partUri.PartUriExtension;
90+
91+
var defaultDictionary = _contentTypeHelper.GetDefaultDictionary();
92+
if (defaultDictionary.ContainsKey(extension))
93+
return defaultDictionary[extension];
94+
95+
//Step 4: 使用后缀名默认
96+
extension = extension.ToLowerInvariant();
97+
var extensionToContentType = ExtensionToContentType(extension);
98+
if (extensionToContentType != null)
99+
{
100+
return new CompatiblePackage.ContentType(extensionToContentType);
101+
}
102+
103+
//Step 5: If we did not find an entry in the override and the default
104+
//dictionaries, this is an error condition
105+
return null;
106+
}
107+
108+
protected override Stream GetStreamCore(FileMode mode, FileAccess access)
109+
{
110+
var memoryStream = new MemoryStream();
111+
return memoryStream;
112+
}
113+
114+
private static string? ExtensionToContentType(string extensionWithLowerInvariant)
115+
=> MimeUtility.LookupType(extensionWithLowerInvariant);
116+
}
117+
}

0 commit comments

Comments
 (0)