// Tries to load liquid templates from local folder
        private static async Task<LiquidTemplatesMap> GetTabTemplatesFromFolderAsync(string folderName)
        {
            var result = new LiquidTemplatesMap();

            string binFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string templatesFolder = Path.Combine(binFolder, "..", folderName, Globals.TabTemplateFolderName);

            if (!Directory.Exists(templatesFolder))
            {
                return result;
            }

            foreach(var templateFilePath in Directory.EnumerateFiles(templatesFolder, "*.liquid"))
            {
                var nameParts = Path.GetFileName(templateFilePath).Split('.');
                if(nameParts.Length < 2)
                {
                    continue;
                }

                string tabName = nameParts[0];
                string entityTypeName = nameParts.Length > 2 ? nameParts[1] : string.Empty;
                string templateText = await File.ReadAllTextAsync(templateFilePath);

                result.GetOrAdd(entityTypeName, new ConcurrentDictionary<string, string>())[tabName] = templateText;
            }

            return result;
        }
예제 #2
0
        // Tries to load liquid templates from underlying Azure Storage
        private static async Task <LiquidTemplatesMap> GetTabTemplatesFromStorageAsync()
        {
            var result = new LiquidTemplatesMap();

            try
            {
                string connectionString = Environment.GetEnvironmentVariable(EnvVariableNames.AzureWebJobsStorage);
                var    blobClient       = CloudStorageAccount.Parse(connectionString).CreateCloudBlobClient();

                // Listing all blobs in durable-functions-monitor/tab-templates folder
                var container = blobClient.GetContainerReference(Globals.TemplateContainerName);

                string templateFolderName = Globals.TabTemplateFolderName + "/";
                var    templateNames      = await container.ListBlobsAsync(templateFolderName);

                // Loading blobs in parallel
                await Task.WhenAll(templateNames.Select(async templateName =>
                {
                    var blob = await blobClient.GetBlobReferenceFromServerAsync(templateName.Uri);

                    // Expecting the blob name to be like "[Tab Name].[EntityTypeName].liquid" or just "[Tab Name].liquid"
                    var nameParts = blob.Name.Substring(templateFolderName.Length).Split('.');
                    if (nameParts.Length < 2 || nameParts.Last() != "liquid")
                    {
                        return;
                    }

                    string tabName        = nameParts[0];
                    string entityTypeName = nameParts.Length > 2 ? nameParts[1] : string.Empty;

                    using (var stream = new MemoryStream())
                    {
                        await blob.DownloadToStreamAsync(stream);
                        string templateText = Encoding.UTF8.GetString(stream.ToArray());

                        result.GetOrAdd(entityTypeName, new ConcurrentDictionary <string, string>())[tabName] = templateText;
                    }
                }));
            }
            catch (Exception)
            {
                // Intentionally swallowing all exceptions here
            }
            return(result);
        }