microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
aamirj/StackOverflowTest

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/ApplicationBuilder.Tabs.cs

92lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Reflection;
5
6using Microsoft.AspNetCore.Builder;
7using Microsoft.AspNetCore.Http;
8using Microsoft.AspNetCore.Routing;
9using Microsoft.AspNetCore.StaticFiles;
10using Microsoft.Extensions.FileProviders;
11
12namespace Microsoft.Teams.Plugins.AspNetCore.Extensions;
13
14public static partial class ApplicationBuilderExtensions
15{
16 /// <summary>
17 /// add/update a static tab.
18 /// the tab will be hosted at
19 /// <code>http://localhost:{{PORT}}/tabs/{{name}}</code> or
20 /// <code>https://{{BOT_DOMAIN}}/tabs/{{name}}</code>
21 /// </summary>
22 /// <param name="name">A unique identifier for the entity which the tab displays</param>
23 /// <param name="provider">The file provider used to serve static assets</param>
24 public static IApplicationBuilder AddTab(this IApplicationBuilder builder, string name, IFileProvider provider)
25 {
26 var contentTypeProvider = new FileExtensionContentTypeProvider();
27
28 IResult OnGet(string path)
29 {
30 var file = provider.GetFileInfo(path);
31
32 if (!file.Exists)
33 {
34 return Results.NotFound($"file \"{path}\" not found");
35 }
36
37 if (!contentTypeProvider.TryGetContentType(file.Name, out string? contentType))
38 {
39 contentType = "text/html";
40 }
41
42 return Results.File(file.CreateReadStream(), contentType);
43 }
44
45 builder.UseStaticFiles(new StaticFileOptions()
46 {
47 FileProvider = provider,
48 ServeUnknownFileTypes = true,
49 RequestPath = $"/tabs/{name}"
50 });
51
52 builder.UseEndpoints(endpoints =>
53 {
54 endpoints.MapGet($"/tabs/{name}", async context =>
55 {
56 await OnGet("index.html").ExecuteAsync(context);
57 });
58
59 endpoints.MapGet($"/tabs/{name}/{{*path}}", async context =>
60 {
61 var path = context.GetRouteData().Values["path"]?.ToString();
62
63 if (path is null)
64 {
65 await Results.NotFound().ExecuteAsync(context);
66 return;
67 }
68
69 await OnGet(path).ExecuteAsync(context);
70 });
71 });
72
73 return builder;
74 }
75
76 /// <summary>
77 /// add/update a static tab.
78 /// the tab will be hosted at
79 /// <code>http://localhost:{{PORT}}/tabs/{{name}}</code> or
80 /// <code>https://{{BOT_DOMAIN}}/tabs/{{name}}</code>
81 /// </summary>
82 /// <param name="name">A unique identifier for the entity which the tab displays</param>
83 /// <param name="path">The filepath to use when creating a file provider</param>
84 /// <remarks>
85 /// The default file provider type is <code>ManifestEmbeddedFileProvider</code>,
86 /// to use your own file provider use see <see cref="AddTab" />
87 /// </remarks>
88 public static IApplicationBuilder AddTab(this IApplicationBuilder builder, string name, string path)
89 {
90 return builder.AddTab(name, new ManifestEmbeddedFileProvider(Assembly.GetCallingAssembly(), path));
91 }
92}