Example #1
0
        private static void mapStorage(IApplicationBuilder appBuilder)
        {
            var manifestEmbeddedProvider =
                new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly());

            appBuilder.Map("/blocklyStorage", app =>
            {
                app.Run(async cnt =>
                {
                    string nameFile = "extensions/SaveToLocalStorage.js";
                    IFileInfo f     = new PhysicalFileInfo(new FileInfo("wwwroot/" + nameFile));

                    if (!f.Exists)
                    {
                        f = manifestEmbeddedProvider.GetFileInfo("blocklyFiles/" + nameFile);
                    }
                    //TODO: add corect mime type for js files
                    using var stream = new MemoryStream();
                    using var cs     = f.CreateReadStream();
                    byte[] buffer    = new byte[2048]; // read in chunks of 2KB
                    int bytesRead;
                    while ((bytesRead = cs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                    }
                    byte[] result = stream.ToArray();
                    // TODO: do something with the result
                    var m = new Memory <byte>(result);
                    await cnt.Response.BodyWriter.WriteAsync(m);
                });
            });
        }
        /// <summary>
        /// Enable the KafkaFlow dashboard
        /// </summary>
        /// <param name="app">Instance of <see cref="IApplicationBuilder"/></param>
        /// <returns></returns>
        public static IApplicationBuilder UseKafkaFlowDashboard(this IApplicationBuilder app)
        {
            app.Map(
                "/kafka-flow",
                builder =>
            {
                var provider = new ManifestEmbeddedFileProvider(
                    Assembly.GetAssembly(typeof(ApplicationBuilderExtensions)),
                    "ClientApp/dist");

                builder.UseStaticFiles(new StaticFileOptions {
                    FileProvider = provider
                });

                builder.Run(
                    async context =>
                {
                    if (context.Request.Path == "/" || context.Request.Path == string.Empty)
                    {
                        await context.Response.SendFileAsync(provider.GetFileInfo("index.html"));
                    }
                });
            });

            return(app);
        }
        private static void mapStorage(IApplicationBuilder appBuilder)
        {
            var manifestEmbeddedProvider =
                new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly());

            appBuilder.Map("/blocklyStorage", app =>
            {
                app.Run(async cnt =>
                {
                    cnt.Response.ContentType = "application/javascript";

                    string nameFile = "extensions/SaveToSqliteStorage.js";

                    var f = manifestEmbeddedProvider.GetFileInfo("blocklyFiles/" + nameFile);

                    using var stream = new MemoryStream();
                    using var cs     = f.CreateReadStream();
                    byte[] buffer    = new byte[2048]; // read in chunks of 2KB
                    int bytesRead;
                    while ((bytesRead = cs.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                    }
                    byte[] result = stream.ToArray();
                    var m         = new ReadOnlyMemory <byte>(result);
                    await cnt.Response.BodyWriter.WriteAsync(m);
                });
            });
        }
Example #4
0
        private static ImmutableList <QuestionModel> Init()
        {
            var cfg = new Configuration()
            {
                Encoding        = Encoding.UTF8,
                HasHeaderRecord = false,
                Delimiter       = ",",
                Quote           = '"',
            };

            var qm = new List <QuestionModel>();
            var id = 1;

            var fp = new ManifestEmbeddedFileProvider(typeof(StaticQuestionFactory).Assembly);
            var fi = fp.GetFileInfo("questions.csv");

            using (var fs = fi.CreateReadStream())
                using (var sr = new StreamReader(fs, cfg.Encoding)) {
                    var cr = new CsvReader(sr, cfg);
                    while (cr.Read())
                    {
                        qm.Add(new QuestionModel {
                            Id            = id++,
                            Question      = cr.GetField <string>(0),
                            YesConclusion = cr.GetField <string>(1),
                            NoConclusion  = cr.GetField <string>(2),
                        });
                    }
                }

            return(qm.ToImmutableList());
        }
Example #5
0
        private IActionResult GetFile(string fileName, string downloadName = null, string contentType = null)
        {
            var fp     = new ManifestEmbeddedFileProvider(typeof(StaticQuestionFactory).Assembly);
            var fi     = fp.GetFileInfo(fileName);
            var stream = fi.CreateReadStream();

            return(File(stream, contentType ?? "application/vnd.android.package-archive", downloadName ?? fileName));
        }
        public static IFileInfo GetFileInfo(Assembly assembly, string fileName)
        {
            var manifestEmbeddedProvider =
                new ManifestEmbeddedFileProvider(assembly);

            var fileInfo = manifestEmbeddedProvider.GetFileInfo(fileName);

            return(fileInfo);
        }
Example #7
0
        private void SaveEmbeddedFilesToTempFolder(params string[] embeddedFileNames)
        {
            var executingAssembly    = Assembly.GetExecutingAssembly();
            var embeddedFileProvider = new ManifestEmbeddedFileProvider(executingAssembly);

            foreach (var embeddedFileName in embeddedFileNames)
            {
                using var reader = embeddedFileProvider.GetFileInfo(embeddedFileName).CreateReadStream();
                using var writer = new StreamWriter(Path.Combine(CollectionFolder, Path.GetFileName(embeddedFileName)));
                reader.CopyTo(writer.BaseStream);
            }
        }
Example #8
0
    public void GetFileInfo_CanResolveFilesInsideAFolder()
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js"),
                                                    TestEntry.File("jquery.min.js"),
                                                    TestEntry.File("site.css"))));

        // Act
        var provider = new ManifestEmbeddedFileProvider(assembly);

        // Assert
        var jqueryValidate = provider.GetFileInfo(Path.Combine("wwwroot", "jquery.validate.js"));

        Assert.True(jqueryValidate.Exists);
        Assert.False(jqueryValidate.IsDirectory);
        Assert.Equal("jquery.validate.js", jqueryValidate.Name);
        Assert.Null(jqueryValidate.PhysicalPath);
        Assert.Equal(0, jqueryValidate.Length);

        var jqueryMin = provider.GetFileInfo(Path.Combine("wwwroot", "jquery.min.js"));

        Assert.True(jqueryMin.Exists);
        Assert.False(jqueryMin.IsDirectory);
        Assert.Equal("jquery.min.js", jqueryMin.Name);
        Assert.Null(jqueryMin.PhysicalPath);
        Assert.Equal(0, jqueryMin.Length);

        var siteCss = provider.GetFileInfo(Path.Combine("wwwroot", "site.css"));

        Assert.True(siteCss.Exists);
        Assert.False(siteCss.IsDirectory);
        Assert.Equal("site.css", siteCss.Name);
        Assert.Null(siteCss.PhysicalPath);
        Assert.Equal(0, siteCss.Length);
    }
Example #9
0
    public void GetFileInfo_EscapingFromTheRootFolder_ReturnsNotFound()
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js"),
                                                    TestEntry.File("jquery.min.js"),
                                                    TestEntry.File("site.css"))));

        // Act
        var provider = new ManifestEmbeddedFileProvider(assembly);

        // Assert
        var jqueryValidate = provider.GetFileInfo(Path.Combine("..", "wwwroot", "jquery.validate.js"));

        Assert.IsType <NotFoundFileInfo>(jqueryValidate);
    }
Example #10
0
    public void GetFileInfo_ResolveNonExistingFile_ReturnsNotFoundFileInfo()
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js"),
                                                    TestEntry.File("jquery.min.js"),
                                                    TestEntry.File("site.css"))));

        var provider = new ManifestEmbeddedFileProvider(assembly);

        // Act
        var file = provider.GetFileInfo("some/non/existing/file.txt");

        // Assert
        Assert.IsType <NotFoundFileInfo>(file);
    }
Example #11
0
    public void ScopedFileProvider_DoesNotReturnFilesOutOfScope(string path)
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js")),
                                TestEntry.File("site.css")));

        var provider       = new ManifestEmbeddedFileProvider(assembly);
        var scopedProvider = new ManifestEmbeddedFileProvider(assembly, provider.Manifest.Scope("wwwroot"), DateTimeOffset.UtcNow);

        // Act
        var jqueryValidate = scopedProvider.GetFileInfo(path);

        // Assert
        Assert.IsType <NotFoundFileInfo>(jqueryValidate);
    }
Example #12
0
    public void GetFileInfo_ReturnsNotFoundfileInfo_ForPathsWithInvalidCharacters(string path)
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js"),
                                                    TestEntry.File("jquery.min.js"),
                                                    TestEntry.File("site.css"))));

        // Act
        var provider = new ManifestEmbeddedFileProvider(assembly);

        // Assert
        var file = provider.GetFileInfo(path);

        Assert.IsType <NotFoundFileInfo>(file);
        Assert.Equal(path, file.Name);
    }
        protected void Generate(FromScriptsMigrationOperation fromScriptsMigrationOperation, MigrationCommandListBuilder builder)
        {
            Regex         regex        = new Regex(@"[\s\r]*create([\s\r]+or[\s\r]+alter)?([\s\r]+clustered)?([\s\r]+unique)?[\s\r]+(?<type>\w+)[\s\r]+(?<name>[^\s\r]+)[\s\r]+(?<body>.*)", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            IFileProvider fileProvider = new ManifestEmbeddedFileProvider(this._assembly, fromScriptsMigrationOperation.RootPath);
            var           scripts      = (fromScriptsMigrationOperation.OperationType == OperationType.Create) ? fromScriptsMigrationOperation.Scripts.ToList() : fromScriptsMigrationOperation.Scripts.Reverse().ToList();

            foreach (var script in scripts)
            {
                var fileInfo = fileProvider.GetFileInfo($"{script}.sql");
                using (var sr = new StreamReader(fileInfo.CreateReadStream()))
                {
                    var fileContent = sr.ReadToEnd();//[^\s\r]
                    var match       = regex.Match(fileContent);
                    if (match.Success)
                    {
                        Generate(fromScriptsMigrationOperation.OperationType, match.Groups["type"].Value, match.Groups["name"].Value, match.Groups["body"].Value, builder);
                    }
                }
            }
        }
Example #14
0
    public void GetFileInfo_ResolvesFiles_WithDifferentCasing(string folder, string file)
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js"),
                                                    TestEntry.File("jquery.min.js"),
                                                    TestEntry.File("site.css"))));

        // Act
        var provider = new ManifestEmbeddedFileProvider(assembly);

        // Assert
        var jqueryValidate = provider.GetFileInfo(Path.Combine(folder, file));

        Assert.True(jqueryValidate.Exists);
        Assert.False(jqueryValidate.IsDirectory);
        Assert.Equal("jquery.validate.js", jqueryValidate.Name);
        Assert.Null(jqueryValidate.PhysicalPath);
        Assert.Equal(0, jqueryValidate.Length);
    }
Example #15
0
    public void Contructor_CanScopeManifestToAFolder()
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js")),
                                TestEntry.File("site.css")));

        var provider       = new ManifestEmbeddedFileProvider(assembly);
        var scopedProvider = new ManifestEmbeddedFileProvider(assembly, provider.Manifest.Scope("wwwroot"), DateTimeOffset.UtcNow);

        // Act
        var jqueryValidate = scopedProvider.GetFileInfo("jquery.validate.js");

        // Assert
        Assert.True(jqueryValidate.Exists);
        Assert.False(jqueryValidate.IsDirectory);
        Assert.Equal("jquery.validate.js", jqueryValidate.Name);
        Assert.Null(jqueryValidate.PhysicalPath);
        Assert.Equal(0, jqueryValidate.Length);
    }
Example #16
0
    public void GetFileInfo_AllowsLeadingDots_OnThePath()
    {
        // Arrange
        var assembly = new TestAssembly(
            TestEntry.Directory("unused",
                                TestEntry.Directory("wwwroot",
                                                    TestEntry.File("jquery.validate.js"),
                                                    TestEntry.File("jquery.min.js"),
                                                    TestEntry.File("site.css"))));

        // Act
        var provider = new ManifestEmbeddedFileProvider(assembly);

        // Assert
        var jqueryValidate = provider.GetFileInfo(Path.Combine(".", "wwwroot", "jquery.validate.js"));

        Assert.True(jqueryValidate.Exists);
        Assert.False(jqueryValidate.IsDirectory);
        Assert.Equal("jquery.validate.js", jqueryValidate.Name);
        Assert.Null(jqueryValidate.PhysicalPath);
        Assert.Equal(0, jqueryValidate.Length);
    }
Example #17
0
        /// <summary>
        /// Enable the KafkaFlow dashboard
        /// </summary>
        /// <param name="app">Instance of <see cref="IApplicationBuilder"/></param>
        /// <param name="dashboard">A handler to configure the dashboard.</param>
        /// <returns></returns>
        public static IApplicationBuilder UseKafkaFlowDashboard(
            this IApplicationBuilder app,
            Action <IDashboardConfigurationBuilder> dashboard)
        {
            var builder = new DashboardConfigurationBuilder();

            dashboard?.Invoke(builder);
            var configuration = builder.Build();

            app.Map(
                configuration.BasePath,
                appBuilder =>
            {
                var provider = new ManifestEmbeddedFileProvider(
                    Assembly.GetAssembly(typeof(KafkaFlow.Admin.Dashboard.ApplicationBuilderExtensions)),
                    "ClientApp/dist");

                appBuilder
                .UseStaticFiles(new StaticFileOptions {
                    FileProvider = provider
                })
                .UseRouting();

                configuration.RequestHandler?.Invoke(appBuilder);

                appBuilder.UseEndpoints(routeBuilder =>
                {
                    var endpoint = routeBuilder.Map(
                        "/",
                        async context => await context.Response.SendFileAsync(provider.GetFileInfo("index.html")));

                    configuration.EndpointHandler?.Invoke(endpoint);
                });
            });
            return(app);
        }
        public void GetFileInfo_SocketTest_FileFound()
        {
            var file = _provider.GetFileInfo(@"StaticContent\index.html");

            Assert.True(file.Exists);
        }
        public IFileInfo GetFileInfo(string subpath)
        {
            var result = _provider.GetFileInfo(_assemblyPath + subpath);

            return(result);
        }