Пример #1
0
        /// <summary>
        /// 如果请求/asset/的文件
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseEmbeddedAsset(this IApplicationBuilder app)
        {
            var assetList    = new EmbeddedAssetProvider(_pathToAssetMap);
            var thisAssembly = typeof(FilterExtention).Assembly;

            foreach (var resourceName in thisAssembly.GetManifestResourceNames())
            {
                if (!resourceName.StartsWith("spa.Asset"))
                {
                    continue;                                        // original assets only
                }
                var path = resourceName.Replace("\\", "/");
                _pathToAssetMap[path.ToLower()] = new EmbeddedAssetDescriptor(thisAssembly, resourceName, false);
            }

            return(app.UseWhen(
                       c =>
            {
                var url = c.Request.Path.Value !.ToLower();
                return url.Contains("/asset/");
            },
                       _ => _.Run((async context =>
            {
                using var webAsset = assetList.GetAsset(context.Request.Path.Value !.ToLower());
                if (webAsset != null)
                {
                    context.Response.ContentType = webAsset.MediaType;
                    await webAsset.Stream.CopyToAsync(context.Response.Body);
                }
            }))));
        }
Пример #2
0
        public void GetAsset_empty()
        {
            var prov = new EmbeddedAssetProvider(
                new Dictionary <string, EmbeddedAssetDescriptor>(),
                new Dictionary <string, string>());

            Assert.Throws <AssetNotFound>(() => prov.GetAsset("", ""));
        }
Пример #3
0
        public void GetEmbeddedResourceStreamFor_null()
        {
            var prov = new EmbeddedAssetProvider(
                new Dictionary <string, EmbeddedAssetDescriptor>(),
                new Dictionary <string, string>());
            var ad = new EmbeddedAssetDescriptor(typeof(EmbeddedAssetProviderTests).Assembly, "123", false);

            Assert.Throws <AssetNotFound>(() => prov.GetEmbeddedResourceStreamFor(ad, ""));
        }
Пример #4
0
        public void GetAsset_ext_empty()
        {
            var prov = new EmbeddedAssetProvider(
                new Dictionary <string, EmbeddedAssetDescriptor>(),
                new Dictionary <string, string>());
            var a = prov.GetAsset("", "index/ext");

            Assert.AreEqual("application/json", a.MediaType);
            Assert.AreEqual(2, a.Stream.Length);
        }
Пример #5
0
        /// <summary>
        /// 内置管理页面路由
        /// </summary>
        /// <param name="app"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseEmbeddedPage(this IApplicationBuilder app, string path)
        {
            var assetList = new EmbeddedAssetProvider(_pathToAssetMap);

            return(app.UseWhen(
                       c =>
            {
                var url = c.Request.Path.Value !.ToLower();
                return url.EndsWith($"spa.{path.ToLower()}");
            },
                       _ => _.Run((async context =>
            {
                var authCheck = ApiMiddleware.AuthCheck(context, path);
                if (authCheck == null)
                {
                    return;
                }

                using var webAsset = assetList.GetAsset($"/spa/asset/{path.ToLower()}/{path.ToLower()}.html");
                context.Response.ContentType = webAsset.MediaType;
                await webAsset.Stream.CopyToAsync(context.Response.Body);
            }))));
        }
Пример #6
0
 public void InferMediaTypeFrom(string file, string mediaType)
 {
     Assert.AreEqual(mediaType, EmbeddedAssetProvider.InferMediaTypeFrom(file));
 }
Пример #7
0
        public void GetAsset_ext_null()
        {
            var prov = new EmbeddedAssetProvider(null, null);

            Assert.Throws <ArgumentNullException>(() => prov.GetAsset("", "index/ext"));
        }
Пример #8
0
        public void GetAsset_null()
        {
            var prov = new EmbeddedAssetProvider(null, null);

            Assert.Throws <NullReferenceException>(() => prov.GetAsset("", ""));
        }