From b1e7022a3f5f64244bb166266b485f9bc8388d0b Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 26 Mar 2018 21:31:20 +0200 Subject: [PATCH] fixe l'execution (apparemment) --- Yavsc.Server/Helpers/DbHelpers.cs | 8 ++--- Yavsc.Server/Models/ApplicationDbContext.cs | 2 -- Yavsc/Helpers/FileSystemHelpers.cs | 2 +- Yavsc/Startup/Startup.FileServer.cs | 6 ++-- Yavsc/Startup/Startup.cs | 31 ++++++++++++++----- .../ViewComponents/DirectoryViewComponent.cs | 3 +- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/Yavsc.Server/Helpers/DbHelpers.cs b/Yavsc.Server/Helpers/DbHelpers.cs index 73677c4f..707e34d0 100644 --- a/Yavsc.Server/Helpers/DbHelpers.cs +++ b/Yavsc.Server/Helpers/DbHelpers.cs @@ -2,10 +2,8 @@ namespace Yavsc.Server.Helpers { public static class DbHelpers { - static string _connectionString = null; - public static string ConnectionString { - get { return _connectionString = null; } - set { _connectionString = value; } - } + // FIXME BUG [fr] DependencyInjection Pourquoi ce champ ne pourrait pas devenir une propriété ? + // : casse à l'execution d'un controlleur, se plaignant d'une valeur nule + public static string ConnectionString = null; } } \ No newline at end of file diff --git a/Yavsc.Server/Models/ApplicationDbContext.cs b/Yavsc.Server/Models/ApplicationDbContext.cs index 4f084bc8..a48ce22f 100644 --- a/Yavsc.Server/Models/ApplicationDbContext.cs +++ b/Yavsc.Server/Models/ApplicationDbContext.cs @@ -65,10 +65,8 @@ namespace Yavsc.Models et.FindProperty("DateCreated").IsReadOnlyAfterSave = true; } } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseNpgsql(DbHelpers.ConnectionString); } diff --git a/Yavsc/Helpers/FileSystemHelpers.cs b/Yavsc/Helpers/FileSystemHelpers.cs index 6be6baee..d9bf768f 100644 --- a/Yavsc/Helpers/FileSystemHelpers.cs +++ b/Yavsc/Helpers/FileSystemHelpers.cs @@ -89,7 +89,7 @@ public static FileRecievedInfo ReceiveProSignature(this ClaimsPrincipal user, st public static void DeleteUserFile(this ApplicationUser user, string fileName) { - var root = Path.Combine(Startup.UserFilesDirName, user.UserName); + var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.UserName); var fi = new FileInfo(Path.Combine(root, fileName)); if (!fi.Exists) return ; fi.Delete(); diff --git a/Yavsc/Startup/Startup.FileServer.cs b/Yavsc/Startup/Startup.FileServer.cs index 9551ba7f..f8476bfd 100644 --- a/Yavsc/Startup/Startup.FileServer.cs +++ b/Yavsc/Startup/Startup.FileServer.cs @@ -6,13 +6,13 @@ using Microsoft.AspNet.FileProviders; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.StaticFiles; +using Yavsc.Abstract.FileSystem; using Yavsc.ViewModels.Auth; namespace Yavsc { public partial class Startup { - public static string UserFilesDirName { get; private set; } public static FileServerOptions UserFilesOptions { get; private set; } public static FileServerOptions AvatarsOptions { get; set; } @@ -20,13 +20,13 @@ namespace Yavsc SiteSettings siteSettings, IHostingEnvironment env, IAuthorizationService authorizationService) { var userFilesDirInfo = new DirectoryInfo( siteSettings.UserFiles.Blog ); - UserFilesDirName = userFilesDirInfo.FullName; + AbstractFileSystemHelpers.UserFilesDirName = userFilesDirInfo.FullName; if (!userFilesDirInfo.Exists) userFilesDirInfo.Create(); UserFilesOptions = new FileServerOptions() { - FileProvider = new PhysicalFileProvider(UserFilesDirName), + FileProvider = new PhysicalFileProvider(AbstractFileSystemHelpers.UserFilesDirName), RequestPath = new PathString(Constants.UserFilesPath), EnableDirectoryBrowsing = env.IsDevelopment(), diff --git a/Yavsc/Startup/Startup.cs b/Yavsc/Startup/Startup.cs index b7dad17d..b5d4fb35 100755 --- a/Yavsc/Startup/Startup.cs +++ b/Yavsc/Startup/Startup.cs @@ -36,11 +36,11 @@ namespace Yavsc using ViewModels.Auth.Handlers; using Yavsc.Abstract.FileSystem; using Yavsc.Helpers; + using Yavsc.Server.Helpers; using static System.Environment; public partial class Startup { - public static string ConnectionString { get; private set; } public static string AvatarsDirName { private set; get; } public static string Authority { get; private set; } public static string Temp { get; set; } @@ -73,7 +73,12 @@ namespace Yavsc builder.AddEnvironmentVariables(); Configuration = builder.Build(); - ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"]; + + var auth = Configuration["Site:Authority"]; + var cxstr = Configuration["Data:DefaultConnection:ConnectionString"]; + DbHelpers.ConnectionString = cxstr; + + Console.WriteLine($"# {auth} ConnectionString: {cxstr}"); } public static GoogleAuthSettings GoogleSettings { get; set; } @@ -82,6 +87,9 @@ namespace Yavsc // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + + // Database connection + services.AddOptions(); var siteSettings = Configuration.GetSection("Site"); services.Configure(siteSettings); @@ -146,10 +154,11 @@ namespace Yavsc // DataProtection ConfigureProtectionServices(services); + // Add framework services. services.AddEntityFramework() .AddNpgsql() - .AddDbContext(options => options.UseNpgsql(ConnectionString)) + .AddDbContext() ; ConfigureOAuthServices(services); @@ -255,7 +264,6 @@ namespace Yavsc IOptions siteSettings, IOptions localizationOptions, IOptions oauth2SettingsContainer, - RoleManager roleManager, IAuthorizationService authorizationService, IOptions payPalSettings, IOptions googleSettings, @@ -266,15 +274,22 @@ namespace Yavsc GlobalLocalizer = localizer; SiteSetup = siteSettings.Value; Authority = siteSettings.Value.Authority; - AbstractFileSystemHelpers.UserFilesDirName = new DirectoryInfo(siteSettings.Value.UserFiles.Blog).FullName; - AbstractFileSystemHelpers.UserBillsDirName = new DirectoryInfo(siteSettings.Value.UserFiles.Bills).FullName; - Startup.Temp = siteSettings.Value.TempDir; + var blogsDir = siteSettings.Value.UserFiles.Blog; + if (blogsDir==null) throw new Exception ("blogsDir==null"); + var billsDir = siteSettings.Value.UserFiles.Bills; + if (billsDir==null) throw new Exception ("billsDir==null"); + + AbstractFileSystemHelpers.UserFilesDirName = new DirectoryInfo(blogsDir).FullName; + AbstractFileSystemHelpers.UserBillsDirName = new DirectoryInfo(billsDir).FullName; + Temp = siteSettings.Value.TempDir; PayPalSettings = payPalSettings.Value; // TODO implement an installation & upgrade procedure // Create required directories - foreach (string dir in new string[] { UserFilesDirName, AbstractFileSystemHelpers.UserBillsDirName, SiteSetup.TempDir }) + foreach (string dir in new string[] { AbstractFileSystemHelpers.UserFilesDirName, AbstractFileSystemHelpers.UserBillsDirName, SiteSetup.TempDir }) { + if (dir==null) throw new Exception (nameof(dir)); + DirectoryInfo di = new DirectoryInfo(dir); if (!di.Exists) di.Create(); } diff --git a/Yavsc/ViewComponents/DirectoryViewComponent.cs b/Yavsc/ViewComponents/DirectoryViewComponent.cs index 8b97167f..70dc2bb7 100644 --- a/Yavsc/ViewComponents/DirectoryViewComponent.cs +++ b/Yavsc/ViewComponents/DirectoryViewComponent.cs @@ -3,6 +3,7 @@ using Microsoft.AspNet.Mvc; namespace Yavsc.ViewComponents { using System.Threading.Tasks; + using Yavsc.Abstract.FileSystem; using Yavsc.ViewModels.UserFiles; public class DirectoryViewComponent : ViewComponent @@ -12,7 +13,7 @@ namespace Yavsc.ViewComponents IViewComponentResult result = null; await Task.Run(() => { - result = View(new UserDirectoryInfo(Startup.UserFilesDirName, User.Identity.Name, dirname)); + result = View(new UserDirectoryInfo(AbstractFileSystemHelpers.UserFilesDirName, User.Identity.Name, dirname)); }); return result; }