Beispiel #1
0
        private async Task <string> GetFileContentAsync(string path, string fileName)
        {
            ITemplateFile contentFile = (await _sourceProvider.GetAsync(path, WriteLog)).FindFile(fileName);

            if (contentFile == null)
            {
                return(null);
            }

            ITemplateFile cloneContentFile = (await _cloneProvider.GetAsync(path, WriteLog)).FindFile(fileName);

            // Get the file content
            using (var httpClient = new HttpClient())
            {
                // Get the content
                var contentStream = await contentFile.DownloadAsync();

                using (var sr = new StreamReader(contentStream))
                {
                    var content = await sr.ReadToEndAsync();

                    // Change relative uris
                    return(ChangeUris(cloneContentFile.DownloadUri, content));
                }
            }
        }
Beispiel #2
0
        public static async Task <T> DownloadAsJsonAsync <T>(this ITemplateFile file, T sample)
        {
            string json;

            // Download the file
            using (Stream stream = await file.DownloadAsync())
            {
                json = await new StreamReader(stream).ReadToEndAsync();
            }

            // Deserialize the json
            return(JsonConvert.DeserializeAnonymousType(json, sample));
        }
Beispiel #3
0
        private async Task FillPackageAsync(DomainModel.Package package, ITemplateFile packageFile)
        {
            using (Stream stream = await packageFile.DownloadAsync())
            {
                // Crate a copy of the source stream
                MemoryStream mem = new MemoryStream();
                await stream.CopyToAsync(mem);

                mem.Position = 0;

                // Prepare the output hierarchy
                ProvisioningHierarchy hierarchy = null;

                if (packageFile.Path.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase))
                {
                    // That's an XML Provisioning Template file

                    XDocument xml = XDocument.Load(mem);
                    mem.Position = 0;

                    // Deserialize the stream into a provisioning hierarchy reading any
                    // dependecy with the Azure Blob Storage connector
                    var formatter           = XMLPnPSchemaFormatter.GetSpecificFormatter(xml.Root.Name.NamespaceName);
                    var templateLocalFolder = $"{ConfigurationManager.AppSettings["BlobTemplatesProvider:ContainerName"]}/{packageFile.Path.Substring(0, packageFile.Path.LastIndexOf('/'))}";

                    var provider = new XMLAzureStorageTemplateProvider(
                        ConfigurationManager.AppSettings["BlobTemplatesProvider:ConnectionString"],
                        templateLocalFolder);
                    formatter.Initialize(provider);

                    // Get the full hierarchy
                    hierarchy = ((IProvisioningHierarchyFormatter)formatter).ToProvisioningHierarchy(mem);
                }
                else if (packageFile.Path.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase))
                {
                    // That's a PnP Package file

                    // Get a provider based on the in-memory .PNP Open XML file
                    OpenXMLConnector    openXmlConnector = new OpenXMLConnector(mem);
                    XMLTemplateProvider provider         = new XMLOpenXMLTemplateProvider(
                        openXmlConnector);

                    // Get the .xml provisioning template file name
                    var xmlTemplateFileName = openXmlConnector.Info?.Properties?.TemplateFileName ??
                                              packageFile.Path.Substring(packageFile.Path.LastIndexOf('/') + 1)
                                              .ToLower().Replace(".pnp", ".xml");

                    // Get the full hierarchy
                    hierarchy = provider.GetHierarchy(xmlTemplateFileName);
                }

                if (hierarchy != null)
                {
                    // We se the DisplayName to the DisplayName of the hierarchy if we don't have a siteTitle in the settings.json file
                    if (string.IsNullOrEmpty(package.DisplayName))
                    {
                        package.DisplayName = hierarchy.DisplayName;
                    }
                    package.ImagePreviewUrl = ChangeUri(packageFile.DownloadUri, hierarchy?.ImagePreviewUrl ?? String.Empty);
                    package.Description     = await GetDescriptionAsync(packageFile.GetDirectoryPath()) ?? hierarchy?.Description ?? "";

                    package.Version           = hierarchy?.Version.ToString();
                    package.PackageProperties = JsonConvert.SerializeObject(hierarchy.Parameters);
                }
            }
        }