|
| 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