yavsc/Yavsc/Startup/Startup.FileServer.cs

77 lines
3 KiB
C#
Raw Normal View History

using System.IO;
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;
2018-03-26 21:31:20 +02:00
using Yavsc.Abstract.FileSystem;
using Yavsc.ViewModels.Auth;
namespace Yavsc
{
public partial class Startup
{
2016-09-23 12:32:17 +02:00
public static FileServerOptions UserFilesOptions { get; private set; }
2018-06-10 20:40:11 +02:00
public static FileServerOptions GitOptions { get; private set; }
2016-11-07 19:34:56 +01:00
2016-12-01 17:34:29 +01:00
public static FileServerOptions AvatarsOptions { get; set; }
public void ConfigureFileServerApp(IApplicationBuilder app,
SiteSettings siteSettings, IHostingEnvironment env, IAuthorizationService authorizationService)
{
2018-06-10 20:40:11 +02:00
var userFilesDirInfo = new DirectoryInfo( siteSettings.Blog );
2018-03-26 21:31:20 +02:00
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),
2016-12-01 17:34:29 +01:00
RequestPath = new PathString(Constants.UserFilesPath),
2018-01-26 13:51:09 +01:00
EnableDirectoryBrowsing = env.IsDevelopment(),
2017-04-11 01:19:35 +02:00
2016-09-23 12:32:17 +02:00
};
2017-04-11 01:19:35 +02:00
UserFilesOptions.EnableDefaultFiles=true;
UserFilesOptions.StaticFileOptions.ServeUnknownFileTypes=true;
UserFilesOptions.StaticFileOptions.OnPrepareResponse += async context =>
{
var uname = context.Context.User.GetUserName();
var path = context.Context.Request.Path;
2017-04-11 01:19:35 +02:00
var result = await authorizationService.AuthorizeAsync(context.Context.User, new ViewFileContext
{ UserName = uname, File = context.File, Path = path } , new ViewRequirement());
};
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),
RequestPath = new PathString(Constants.AvatarsPath),
EnableDirectoryBrowsing = env.IsDevelopment()
};
2018-06-10 20:40:11 +02:00
var gitdirinfo = new DirectoryInfo(Startup.SiteSetup.GitRepository);
GitDirName = gitdirinfo.FullName;
if (!gitdirinfo.Exists) gitdirinfo.Create();
2018-06-10 20:40:11 +02:00
GitOptions = new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(GitDirName),
RequestPath = new PathString(Constants.GitPath),
EnableDirectoryBrowsing = env.IsDevelopment()
};
2016-09-23 12:32:17 +02:00
app.UseFileServer(UserFilesOptions);
2016-12-01 17:34:29 +01:00
app.UseFileServer(AvatarsOptions);
app.UseFileServer(GitOptions);
app.UseStaticFiles();
}
}
}