2016-06-07 15:59:14 +02:00
|
|
|
|
using System.IO;
|
2017-01-13 16:31:40 +01:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
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;
|
|
|
|
|
|
using Microsoft.AspNet.StaticFiles;
|
2017-01-19 12:59:49 +01:00
|
|
|
|
using Yavsc.ViewModels;
|
2017-01-13 16:31:40 +01:00
|
|
|
|
using Yavsc.ViewModels.Auth;
|
2016-06-07 15:59:14 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Yavsc
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class Startup
|
|
|
|
|
|
{
|
|
|
|
|
|
public static string UserFilesDirName { get; private set; }
|
2016-09-23 12:32:17 +02:00
|
|
|
|
public static FileServerOptions UserFilesOptions { 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; }
|
2016-06-07 15:59:14 +02:00
|
|
|
|
public void ConfigureFileServerApp(IApplicationBuilder app,
|
2017-01-13 16:31:40 +01:00
|
|
|
|
SiteSettings siteSettings, IHostingEnvironment env, IAuthorizationService authorizationService)
|
2016-06-07 15:59:14 +02:00
|
|
|
|
{
|
2016-12-01 17:34:29 +01:00
|
|
|
|
var userFilesDirInfo = new DirectoryInfo( siteSettings.UserFiles.Blog );
|
2016-11-14 12:17:07 +01:00
|
|
|
|
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
|
|
|
|
{
|
2016-09-23 12:32:17 +02:00
|
|
|
|
FileProvider = new PhysicalFileProvider(UserFilesDirName),
|
2016-12-01 17:34:29 +01:00
|
|
|
|
RequestPath = new PathString(Constants.UserFilesPath),
|
2016-08-03 16:38:25 +02:00
|
|
|
|
EnableDirectoryBrowsing = env.IsDevelopment()
|
2016-09-23 12:32:17 +02:00
|
|
|
|
};
|
2017-01-13 16:31:40 +01:00
|
|
|
|
|
|
|
|
|
|
UserFilesOptions.StaticFileOptions.OnPrepareResponse += async 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());
|
|
|
|
|
|
};
|
2016-12-01 17:34:29 +01:00
|
|
|
|
var avatarsDirInfo = new DirectoryInfo(Startup.SiteSetup.UserFiles.Avatars);
|
|
|
|
|
|
if (!avatarsDirInfo.Exists) avatarsDirInfo.Create();
|
|
|
|
|
|
AvatarsDirName = avatarsDirInfo.FullName;
|
|
|
|
|
|
|
|
|
|
|
|
AvatarsOptions = new FileServerOptions()
|
|
|
|
|
|
{
|
|
|
|
|
|
FileProvider = new PhysicalFileProvider(AvatarsDirName),
|
|
|
|
|
|
RequestPath = new PathString(Constants.AvatarsPath),
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2016-06-07 15:59:14 +02:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|