private static PnPInfo LoadPnPPackage(PnPPackage package) { PnPInfo pnpInfo = new PnPInfo(); pnpInfo.Manifest = package.Manifest; pnpInfo.Properties = package.Properties; pnpInfo.FilesMap = package.FilesMap; pnpInfo.Files = new List <PnPFileInfo>(); foreach (KeyValuePair <String, PnPPackageFileItem> file in package.Files) { pnpInfo.Files.Add( new PnPFileInfo { InternalName = file.Key, OriginalName = package.FilesMap != null ? (String.IsNullOrEmpty(file.Value.Folder) ? package.FilesMap.Map[file.Key] : package.FilesMap.Map[file.Key].Replace(file.Value.Folder + '/', "")) : file.Key, Folder = file.Value.Folder, Content = file.Value.Content, }); } return(pnpInfo); }
public static Byte[] PackTemplate(this PnPInfo pnpInfo) { using (MemoryStream stream = PackTemplateAsStream(pnpInfo)) { return(stream.ToArray()); } }
/// <summary> /// OpenXMLConnector constructor. Allows to manage a .PNP OpenXML package file through a supporting persistence connector. /// </summary> /// <param name="packageFileName">The name of the .PNP package file. If the .PNP extension is missing, it will be added</param> /// <param name="persistenceConnector">The FileConnector object that will be used for physical persistence of the file</param> /// <param name="author">The Author of the .PNP package file, if any. Optional</param> /// <param name="signingCertificate">The X.509 certificate to use for digital signature of the template, optional</param> public OpenXMLConnector(string packageFileName, FileConnectorBase persistenceConnector, String author = null, X509Certificate2 signingCertificate = null) : base() { if (String.IsNullOrEmpty(packageFileName)) { throw new ArgumentException("packageFileName"); } else if (!packageFileName.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase)) { // Check for file extension packageFileName = packageFileName.EndsWith(".") ? (packageFileName + "pnp") : (packageFileName + ".pnp"); } this.packageFileName = packageFileName; if (persistenceConnector == null) { throw new ArgumentException("persistenceConnector"); } this.persistenceConnector = persistenceConnector; // Try to load the .PNP package file var packageStream = persistenceConnector.GetFileStream(packageFileName); if (packageStream != null) { // If the .PNP package exists unpack it into PnP OpenXML package info object using (StreamReader sr = new StreamReader(packageStream)) { Byte[] buffer = new Byte[packageStream.Length]; // TODO: Handle large files with chunking packageStream.Read(buffer, 0, (Int32)packageStream.Length); this.pnpInfo = buffer.UnpackTemplate(); } } else { // Otherwsie initialize a fresh new PnP OpenXML package info object this.pnpInfo = new PnPInfo() { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = PnPCoreUtilities.PnPCoreVersionTag, Author = !String.IsNullOrEmpty(author) ? author : String.Empty, }, }; } }
public static MemoryStream PackTemplateAsStream(this PnPInfo pnpInfo) { MemoryStream stream = new MemoryStream(); using (PnPPackage package = PnPPackage.Open(stream, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { SavePnPPackage(pnpInfo, package); } stream.Position = 0; return(stream); }
public static Byte[] PackTemplate(this PnPInfo pnpInfo) { Byte[] fileBytes; using (MemoryStream stream = new MemoryStream()) { using (PnPPackage package = PnPPackage.Open(stream, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { SavePnPPackage(pnpInfo, package); } fileBytes = stream.ToArray(); } return(fileBytes); }
private static void SavePnPPackage(PnPInfo pnpInfo, PnPPackage package) { package.Manifest = pnpInfo.Manifest; package.Properties = pnpInfo.Properties; package.ClearFiles(); if (pnpInfo.Files != null) { foreach (PnPFileInfo file in pnpInfo.Files) { package.AddFile(file.Name, file.Folder, file.Content); } } }
/// <summary> /// OpenXMLConnector constructor. Allows to manage a .PNP OpenXML package file through a supporting persistence connector. /// </summary> /// <param name="packageFileName">The name of the .PNP package file. If the .PNP extension is missing, it will be added</param> /// <param name="persistenceConnector">The FileConnector object that will be used for physical persistence of the file</param> /// <param name="author">The Author of the .PNP package file, if any. Optional</param> /// <param name="signingCertificate">The X.509 certificate to use for digital signature of the template, optional</param> /// <param name="templateFileName">The name of the tempalte file, optional</param> public OpenXMLConnector(string packageFileName, FileConnectorBase persistenceConnector, string author = null, X509Certificate2 signingCertificate = null, string templateFileName = null) : base() { if (string.IsNullOrEmpty(packageFileName)) { throw new ArgumentException("packageFileName"); } if (!packageFileName.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase)) { // Check for file extension packageFileName = packageFileName.TrimEnd('.') + ".pnp"; } this.packageFileName = packageFileName; if (persistenceConnector == null) { throw new ArgumentException("persistenceConnector"); } this.persistenceConnector = persistenceConnector; // Try to load the .PNP package file var packageStream = persistenceConnector.GetFileStream(packageFileName); if (packageStream != null) { // If the .PNP package exists unpack it into PnP OpenXML package info object MemoryStream ms = packageStream.ToMemoryStream(); this.pnpInfo = ms.UnpackTemplate(); } else { // Otherwsie initialize a fresh new PnP OpenXML package info object this.pnpInfo = new PnPInfo() { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = PnPCoreUtilities.PnPCoreVersionTag, Author = !string.IsNullOrEmpty(author) ? author : string.Empty, TemplateFileName = templateFileName ?? "" }, }; } }
private static void SavePnPPackage(PnPInfo pnpInfo, PnPPackage package) { package.Manifest = pnpInfo.Manifest; package.Properties = pnpInfo.Properties; package.FilesMap = new PnPFilesMap(pnpInfo.Files.ToDictionary(f => f.InternalName, f => f.OriginalName)); package.ClearFiles(); if (pnpInfo.Files != null) { foreach (PnPFileInfo file in pnpInfo.Files) { package.AddFile(file.InternalName, file.Folder, file.Content); } } }
private static void SavePnPPackage(PnPInfo pnpInfo, PnPPackage package) { package.Manifest = pnpInfo.Manifest; package.Properties = pnpInfo.Properties; Debug.Assert(pnpInfo.Files.TrueForAll(f => !string.IsNullOrWhiteSpace(f.InternalName)), "All files need an InternalFileName"); package.FilesMap = new PnPFilesMap(pnpInfo.Files.ToDictionary(f => f.InternalName, f => Path.Combine(f.Folder, f.OriginalName).Replace('\\', '/').TrimStart('/'))); package.ClearFiles(); if (pnpInfo.Files != null) { foreach (PnPFileInfo file in pnpInfo.Files) { package.AddFile(file.InternalName, file.Content); } } }
/// <summary> /// OpenXMLConnector constructor. Allows to manage a .PNP OpenXML package through an in memory stream. /// </summary> /// <param name="packageStream">The package stream</param> public OpenXMLConnector(Stream packageStream) : base() { if (packageStream == null) { throw new ArgumentNullException(nameof(packageStream)); } if (!packageStream.CanRead) { throw new ArgumentException("package"); } // If the .PNP package exists unpack it into PnP OpenXML package info object MemoryStream ms = packageStream.ToMemoryStream(); this.pnpInfo = ms.UnpackTemplate(); }
private byte[] CreatePnPPackageFile(string ctId) { PnPInfo info = new PnPInfo { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = PnP.Framework.Utilities.PnPCoreUtilities.PnPCoreVersionTag, Author = string.Empty, }, Files = new List <PnPFileInfo>() }; DirectoryInfo dirInfo = new DirectoryInfo(Path.GetFullPath(Folder)); string templateFileName = Path.GetFileNameWithoutExtension(Out) + ".xml"; var xml = CreateXmlAsStringFrom(ctId); PnPFileInfo templateInfo = new PnPFileInfo { InternalName = templateFileName.AsInternalFilename(), OriginalName = templateFileName, Folder = "", Content = System.Text.Encoding.UTF8.GetBytes(xml) }; info.Files.Add(templateInfo); foreach (var currentFile in dirInfo.GetFiles("*.*", SearchOption.AllDirectories)) { var folder = GetFolderName(currentFile, dirInfo); PnPFileInfo fileInfo = new PnPFileInfo { InternalName = currentFile.Name.AsInternalFilename(), OriginalName = currentFile.Name, Folder = folder, Content = System.IO.File.ReadAllBytes(currentFile.FullName) }; WriteVerbose("Adding file:" + currentFile.Name + " - " + folder); info.Files.Add(fileInfo); } byte[] pack = info.PackTemplate().ToArray(); return(pack); }
private byte[] CreatePnPPackageFile() { PnPInfo info = new PnPInfo { Manifest = new PnPManifest() { Type = PackageType.Full }, Properties = new PnPProperties() { Generator = OfficeDevPnP.Core.Utilities.PnPCoreUtilities.PnPCoreVersionTag, Author = string.Empty, }, Files = new List <PnPFileInfo>() }; DirectoryInfo dirInfo = new DirectoryInfo(Path.GetFullPath(Folder)); string templateFileName = Path.GetFileNameWithoutExtension(Out) + ".xml"; bool templateFileMissing = dirInfo.GetFiles(templateFileName, SearchOption.TopDirectoryOnly).Length == 0; if (templateFileMissing) { throw new InvalidOperationException("You need an xml template file (" + templateFileName + ") with the same name as the .pnp outfile in order to pack a folder to a .pnp package file."); } info.Properties.TemplateFileName = templateFileName; foreach (var currentFile in dirInfo.GetFiles("*.*", SearchOption.AllDirectories)) { var folder = GetFolderName(currentFile, dirInfo); PnPFileInfo fileInfo = new PnPFileInfo { InternalName = currentFile.Name.AsInternalFilename(), OriginalName = currentFile.Name, Folder = folder, Content = File.ReadAllBytes(currentFile.FullName) }; WriteVerbose("Adding file:" + currentFile.Name + " - " + folder); info.Files.Add(fileInfo); } byte[] pack = info.PackTemplate().ToArray(); return(pack); }
private static PnPInfo LoadPnPPackage(PnPPackage package) { PnPInfo pnpInfo = new PnPInfo(); pnpInfo.Manifest = package.Manifest; pnpInfo.Properties = package.Properties; pnpInfo.Files = new List <PnPFileInfo>(); foreach (KeyValuePair <String, PnPPackageFileItem> file in package.Files) { pnpInfo.Files.Add( new PnPFileInfo { Name = file.Key, Folder = file.Value.Folder, Content = file.Value.Content, }); } return(pnpInfo); }