-
Notifications
You must be signed in to change notification settings - Fork 1
/
StaticFIles.cs
75 lines (69 loc) · 2.58 KB
/
StaticFIles.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MimeTypes;
using Microsoft.Extensions.Configuration;
namespace Valo.Teams.StaticFiles
{
public class ServeStaticFile
{
private const string staticFilesFolder = "wwwroot\\assets";
private readonly string defaultPage;
// The configuration is available for injection.
// The used settings can be in any config (environment, host.json local.settings.json)
public ServeStaticFile(IConfiguration configuration)
{
this.defaultPage = configuration.GetValue<string>("DEFAULT_PAGE", "index.html");
}
[FunctionName("ServeStaticFile")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ExecutionContext context, ILogger log)
{
try
{
var filePath = GetFilePath(context, req.Query["file"]);
if (File.Exists(filePath))
{
var stream = File.OpenRead(filePath);
return new FileStreamResult(stream, GetMimeType(filePath))
{
LastModified = File.GetLastWriteTime(filePath)
};
} else
{
return new NotFoundResult();
}
}
catch
{
return new BadRequestResult();
}
}
private string GetFilePath(ExecutionContext context, string pathValue)
{
var path = pathValue ?? "";
string contentRoot = Path.Combine(context.FunctionAppDirectory, staticFilesFolder);
string fullPath = Path.GetFullPath(Path.Combine(contentRoot, pathValue));
if (!IsInDirectory(contentRoot, fullPath))
{
throw new ArgumentException("Invalid path");
}
if (Directory.Exists(fullPath))
{
fullPath = Path.Combine(fullPath, defaultPage);
}
return fullPath;
}
private static bool IsInDirectory(string parentPath, string childPath) => childPath.StartsWith(parentPath);
private static string GetMimeType(string filePath)
{
var fileInfo = new FileInfo(filePath);
return MimeTypeMap.GetMimeType(fileInfo.Extension);
}
}
}