2020-10-17 23:40:37 +01:00
|
|
|
using System;
|
2016-06-07 15:59:14 +02:00
|
|
|
using System.IO;
|
2019-08-04 11:45:00 +02:00
|
|
|
using System.Security.Claims;
|
2020-10-17 23:40:37 +01:00
|
|
|
using System.Threading.Tasks;
|
2017-01-13 16:31:40 +01:00
|
|
|
using Microsoft.AspNet.Authorization;
|
2016-06-07 15:59:14 +02:00
|
|
|
using Microsoft.AspNet.Builder;
|
|
|
|
|
using Microsoft.AspNet.FileProviders;
|
|
|
|
|
using Microsoft.AspNet.Hosting;
|
|
|
|
|
using Microsoft.AspNet.Http;
|
2020-10-17 23:40:37 +01:00
|
|
|
using Microsoft.AspNet.Http.Features;
|
2016-06-07 15:59:14 +02:00
|
|
|
using Microsoft.AspNet.StaticFiles;
|
2018-06-20 02:15:42 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-08-21 17:23:58 +01:00
|
|
|
using Yavsc.Helpers;
|
2019-08-04 11:45:00 +02:00
|
|
|
using Yavsc.Services;
|
|
|
|
|
using Yavsc.ViewModels.Auth;
|
2016-06-07 15:59:14 +02:00
|
|
|
|
|
|
|
|
namespace Yavsc
|
|
|
|
|
{
|
2020-10-17 23:40:37 +01:00
|
|
|
|
|
|
|
|
public static class YaFileSServerExtenstions
|
|
|
|
|
{
|
|
|
|
|
public static IApplicationBuilder UseYaFileServer(this IApplicationBuilder builder, FileServerOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (options == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("options");
|
|
|
|
|
}
|
|
|
|
|
if (options.EnableDefaultFiles)
|
|
|
|
|
{
|
|
|
|
|
builder = builder.UseDefaultFiles(options.DefaultFilesOptions);
|
|
|
|
|
}
|
|
|
|
|
if (options.EnableDirectoryBrowsing)
|
|
|
|
|
{
|
|
|
|
|
builder = builder.UseDirectoryBrowser(options.DirectoryBrowserOptions);
|
|
|
|
|
}
|
|
|
|
|
return builder.UseYaSendFileFallback().UseStaticFiles(options.StaticFileOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IApplicationBuilder UseYaSendFileFallback(this IApplicationBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
return builder.UseMiddleware<YaSendFileMiddleware>(new object[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-07 15:59:14 +02:00
|
|
|
public partial class Startup
|
|
|
|
|
{
|
2020-10-17 23:40:37 +01:00
|
|
|
public static FileServerOptions UserFilesOptions { get; private set; }
|
|
|
|
|
public static FileServerOptions GitOptions { get; private set; }
|
|
|
|
|
|
|
|
|
|
public static FileServerOptions AvatarsOptions { get; set; }
|
|
|
|
|
|
|
|
|
|
static IAuthorizationService AuthorizationService { get; set; }
|
2016-11-07 19:34:56 +01:00
|
|
|
|
2016-06-07 15:59:14 +02:00
|
|
|
public void ConfigureFileServerApp(IApplicationBuilder app,
|
2019-08-04 11:45:00 +02:00
|
|
|
SiteSettings siteSettings, IHostingEnvironment env,
|
|
|
|
|
IAuthorizationService authorizationService)
|
2016-06-07 15:59:14 +02:00
|
|
|
{
|
2020-10-17 23:40:37 +01:00
|
|
|
AuthorizationService = authorizationService;
|
|
|
|
|
var userFilesDirInfo = new DirectoryInfo(siteSettings.Blog);
|
|
|
|
|
AbstractFileSystemHelpers.UserFilesDirName = userFilesDirInfo.FullName;
|
2016-07-29 15:49:30 +02:00
|
|
|
|
2016-11-14 12:17:07 +01:00
|
|
|
if (!userFilesDirInfo.Exists) userFilesDirInfo.Create();
|
2016-07-29 15:49:30 +02:00
|
|
|
|
2016-09-23 12:32:17 +02:00
|
|
|
UserFilesOptions = new FileServerOptions()
|
2016-07-29 15:49:30 +02:00
|
|
|
{
|
2018-03-26 21:31:20 +02:00
|
|
|
FileProvider = new PhysicalFileProvider(AbstractFileSystemHelpers.UserFilesDirName),
|
2019-10-09 02:22:38 +01:00
|
|
|
RequestPath = PathString.FromUriComponent(Constants.UserFilesPath),
|
2018-01-26 13:51:09 +01:00
|
|
|
EnableDirectoryBrowsing = env.IsDevelopment(),
|
2016-09-23 12:32:17 +02:00
|
|
|
};
|
2020-10-17 23:40:37 +01:00
|
|
|
UserFilesOptions.EnableDefaultFiles = true;
|
|
|
|
|
UserFilesOptions.StaticFileOptions.ServeUnknownFileTypes = true;
|
|
|
|
|
|
|
|
|
|
UserFilesOptions.StaticFileOptions.OnPrepareResponse = OnPrepareUserFileResponse;
|
|
|
|
|
|
2018-06-10 20:40:11 +02:00
|
|
|
var avatarsDirInfo = new DirectoryInfo(Startup.SiteSetup.Avatars);
|
2016-12-01 17:34:29 +01:00
|
|
|
if (!avatarsDirInfo.Exists) avatarsDirInfo.Create();
|
|
|
|
|
AvatarsDirName = avatarsDirInfo.FullName;
|
|
|
|
|
|
|
|
|
|
AvatarsOptions = new FileServerOptions()
|
|
|
|
|
{
|
|
|
|
|
FileProvider = new PhysicalFileProvider(AvatarsDirName),
|
2019-10-09 02:22:38 +01:00
|
|
|
RequestPath = PathString.FromUriComponent(Constants.AvatarsPath),
|
2016-12-01 17:34:29 +01:00
|
|
|
EnableDirectoryBrowsing = env.IsDevelopment()
|
|
|
|
|
};
|
2018-06-11 13:27:12 +02:00
|
|
|
|
|
|
|
|
|
2018-06-10 20:40:11 +02:00
|
|
|
var gitdirinfo = new DirectoryInfo(Startup.SiteSetup.GitRepository);
|
2018-06-11 13:27:12 +02:00
|
|
|
GitDirName = gitdirinfo.FullName;
|
|
|
|
|
if (!gitdirinfo.Exists) gitdirinfo.Create();
|
2018-06-10 20:40:11 +02:00
|
|
|
GitOptions = new FileServerOptions()
|
|
|
|
|
{
|
|
|
|
|
FileProvider = new PhysicalFileProvider(GitDirName),
|
2019-10-09 02:22:38 +01:00
|
|
|
RequestPath = PathString.FromUriComponent(Constants.GitPath),
|
2018-06-20 02:15:42 +02:00
|
|
|
EnableDirectoryBrowsing = env.IsDevelopment(),
|
2018-06-10 20:40:11 +02:00
|
|
|
};
|
2018-06-20 02:15:42 +02:00
|
|
|
GitOptions.DefaultFilesOptions.DefaultFileNames.Add("index.md");
|
|
|
|
|
GitOptions.StaticFileOptions.ServeUnknownFileTypes = true;
|
2020-10-17 23:40:37 +01:00
|
|
|
_logger.LogInformation($"{GitDirName}");
|
|
|
|
|
GitOptions.StaticFileOptions.OnPrepareResponse += OnPrepareGitRepoResponse;
|
2018-06-10 20:40:11 +02:00
|
|
|
|
2016-09-23 12:32:17 +02:00
|
|
|
app.UseFileServer(UserFilesOptions);
|
2016-12-01 17:34:29 +01:00
|
|
|
|
|
|
|
|
app.UseFileServer(AvatarsOptions);
|
|
|
|
|
|
2018-06-11 13:27:12 +02:00
|
|
|
app.UseFileServer(GitOptions);
|
2016-06-07 15:59:14 +02:00
|
|
|
app.UseStaticFiles();
|
|
|
|
|
}
|
2020-10-17 23:40:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnPrepareUserFileResponse(StaticFileResponseContext context)
|
|
|
|
|
{
|
|
|
|
|
var uname = context.Context.User?.GetUserName();
|
|
|
|
|
var path = context.Context.Request.Path;
|
|
|
|
|
var result = await AuthorizationService.AuthorizeAsync(context.Context.User,
|
|
|
|
|
new ViewFileContext{ UserName = uname, File = context.File, Path = path }, new ViewRequirement());
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("403");
|
|
|
|
|
// TODO prettier
|
|
|
|
|
context.Context.Response.StatusCode = 403;
|
2020-10-18 10:15:08 +01:00
|
|
|
context.Context.Response.Redirect("/Home/Status/403", false);
|
2020-10-17 23:40:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-06-20 02:15:42 +02:00
|
|
|
|
|
|
|
|
static void OnPrepareGitRepoResponse(StaticFileResponseContext context)
|
|
|
|
|
{
|
|
|
|
|
if (context.File.Name.EndsWith(".ansi.log"))
|
|
|
|
|
{
|
2020-10-17 23:40:37 +01:00
|
|
|
context.Context.Response.Redirect("/Git" + context.Context.Request.Path);
|
2018-06-20 02:15:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-06-07 15:59:14 +02:00
|
|
|
}
|
|
|
|
|
}
|