diff --git a/src/Yavsc.Blogs.Tests/BlogApiTests.cs b/src/Yavsc.Blogs.Tests/BlogApiTests.cs index b2989ef50..de10cac30 100644 --- a/src/Yavsc.Blogs.Tests/BlogApiTests.cs +++ b/src/Yavsc.Blogs.Tests/BlogApiTests.cs @@ -11,7 +11,6 @@ using Yavsc.Server.Helpers; using Yavsc.Tests.Shared; using Yavsc.Blogs.Tests.Fixtures; using BlogPostDto = Yavsc.Blogspot.BlogPostDto; -using NuGet.Protocol; namespace Yavsc.Blogs.Tests; @@ -339,9 +338,9 @@ public sealed class BlogApiTests : IClassFixture ResetAndSeedDefaultUser(); using var http = NewClient(subject: "tester"); - // AttachFiles writes under the fixture-configured - // SiteSettings.Blog (BlogFilesRoot), not a local temp dir. - var filesRoot = _fixture.BlogFilesRoot; + var previousRoot = "blog"; + var tempRoot = Path.Combine(Path.GetTempPath(), "yavsc-blogs-tests-files-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tempRoot); try { @@ -374,10 +373,8 @@ public sealed class BlogApiTests : IClassFixture DateModified = DateTime.UtcNow }; - var form = new MultipartFormDataContent - { - { new StringContent(JsonSerializer.Serialize(update)), "blog" } - }; + var form = new MultipartFormDataContent(); + form.Add(new StringContent(JsonSerializer.Serialize(update)), "blog"); var fileBytes = System.Text.Encoding.UTF8.GetBytes("payload test"); var fileContent = new ByteArrayContent(fileBytes); @@ -407,9 +404,7 @@ public sealed class BlogApiTests : IClassFixture } finally { - // BlogFilesRoot is the fixture-shared temp root (one per - // test run); leave it in place for the other multipart - // tests. The OS reclaims it. + try { Directory.Delete(tempRoot, recursive: true); } catch { } } } @@ -483,9 +478,9 @@ public sealed class BlogApiTests : IClassFixture ResetAndSeedDefaultUser(); using var http = NewClient(subject: "tester"); - // AttachFiles writes under the fixture-configured - // SiteSettings.Blog (BlogFilesRoot), not a local temp dir. - var filesRoot = _fixture.BlogFilesRoot; + var previousRoot = "blog"; + var tempRoot = Path.Combine(Path.GetTempPath(), "yavsc-blogs-tests-files-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(tempRoot); try { @@ -571,18 +566,16 @@ public sealed class BlogApiTests : IClassFixture // markdown link points: // {UserFilesDirName}/{user}/blogs/{postId}/{fileName}. var expectedFile = Path.Combine( - filesRoot, "tester", "blogs", created.Id.ToString(), "note.txt"); - var written = Directory.Exists(filesRoot) - ? string.Join(", ", Directory.EnumerateFiles(filesRoot, "*", SearchOption.AllDirectories)) + tempRoot, "tester", "blogs", created.Id.ToString(), "note.txt"); + var written = Directory.Exists(tempRoot) + ? string.Join(", ", Directory.EnumerateFiles(tempRoot, "*", SearchOption.AllDirectories)) : ""; Assert.True(File.Exists(expectedFile), $"Expected uploaded file at {expectedFile}. Actually written: {written}"); } finally { - // BlogFilesRoot is the fixture-shared temp root (one per - // test run); leave it in place for the other multipart - // tests. The OS reclaims it. + try { Directory.Delete(tempRoot, recursive: true); } catch { } } } diff --git a/src/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs b/src/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs index f464a1caa..e0606f5d7 100644 --- a/src/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs +++ b/src/Yavsc.Blogs.Tests/Fixtures/BlogsWebServerFixture.cs @@ -63,16 +63,6 @@ public sealed class BlogsWebServerFixture : WebHostFixture public long PostId { get; private set; } public string DefaultUserLogin { get => "alice"; } - /// Root directory under which - /// BlogSpotService.AttachFiles writes uploaded files - /// during tests (the configured SiteSettings.Blog). - /// Attachments land at - /// {BlogFilesRoot}/{userName}/blogs/{postId}/{fileName}. - /// A fresh per-run temp directory, so tests can assert on the - /// physical file without touching the developer's real - /// user-files tree. - public string BlogFilesRoot { get; private set; } = string.Empty; - // A single SqliteConnection held open at the static level, // mirroring how Yavsc.Org.Tests.WebServerFixture hoists its // shared configuration into static slots. Closing the @@ -122,18 +112,6 @@ public sealed class BlogsWebServerFixture : WebHostFixture builder.Services.AddSingleton( new NoopFileSystemAuthManager()); - // BlogSpotService.AttachFiles writes uploaded files under - // siteSettings.Blog. Without a configured SiteSettings the - // IOptions value has Blog == null and - // Path.Combine(null, …) throws inside AttachFiles' catch-all, - // silently persisting no attachment. Point Blog at a - // per-run temp directory so the multipart tests can assert - // on real files. - builder.Services.Configure(s => - { - s.Blog = BlogFilesRoot; - }); - // Real BlogSpotService — same instance the production host // builds (ApplicationDbContext, IAuthorizationService, // IFileSystemAuthManager). With PermissionHandler registered diff --git a/src/Yavsc.Blogs/Controllers/BlogApiController.cs b/src/Yavsc.Blogs/Controllers/BlogApiController.cs index 7aaf221e4..97c070a3c 100644 --- a/src/Yavsc.Blogs/Controllers/BlogApiController.cs +++ b/src/Yavsc.Blogs/Controllers/BlogApiController.cs @@ -57,31 +57,12 @@ namespace Yavsc.Blogs.Controllers [HttpPut("{id}")] public async Task PutBlog(long id) { - var blog = await ReadBlogRequestAsync(); + var blog = await ReadPutBlogRequestAsync(); if (blog is null) { return BadRequest(ModelState); } - // Without [FromBody], the data-annotations attributes - // ([Required] on Title, etc.) are no longer evaluated by - // the model binder — the body is read manually. Run - // validation explicitly so an empty Title still yields - // 400 (and not a 500 from the DB NOT NULL constraint). - var validationContext = new System.ComponentModel.DataAnnotations.ValidationContext(blog); - var validationResults = new List(); - if (!System.ComponentModel.DataAnnotations.Validator.TryValidateObject( - blog, validationContext, validationResults, validateAllProperties: true)) - { - foreach (var result in validationResults) - { - ModelState.AddModelError( - result.MemberNames.FirstOrDefault() ?? string.Empty, - result.ErrorMessage ?? "Invalid value."); - } - return BadRequest(ModelState); - } - // These properties are server-managed or optional graph members and // should not block JSON payloads coming from API clients. ModelState.Remove(nameof(Models.Blog.BlogPost.Author)); @@ -112,8 +93,7 @@ namespace Yavsc.Blogs.Controllers try { - await blogSpotService.Modify(User, blog); - blogSpotService.AttachFiles(files, User.GetUserId(), id); + await blogSpotService.Modify(User, blog, files); } catch (AuthorizationFailureException) { @@ -125,14 +105,8 @@ namespace Yavsc.Blogs.Controllers // POST: api/v1/blogspot [HttpPost] - public async Task PostBlog() + public IActionResult PostBlog([FromBody] Models.Blog.BlogPost blog) { - var blog = await ReadBlogRequestAsync(); - if (blog is null) - { - return BadRequest(ModelState); - } - // These properties are server-managed or optional graph members and // should not block JSON payloads coming from API clients. ModelState.Remove(nameof(Models.Blog.BlogPost.Author)); @@ -141,46 +115,35 @@ namespace Yavsc.Blogs.Controllers ModelState.Remove(nameof(Models.Blog.BlogPost.UserCreated)); ModelState.Remove(nameof(Models.Blog.BlogPost.UserModified)); - // Without [FromBody], the data-annotations attributes - // ([Required] on Title, etc.) are no longer evaluated by - // the model binder — the body is read manually. Run - // validation explicitly so an empty Title still yields - // 400 (and not a 500 from the DB NOT NULL constraint). - var validationContext = new System.ComponentModel.DataAnnotations.ValidationContext(blog); - var validationResults = new List(); - if (!System.ComponentModel.DataAnnotations.Validator.TryValidateObject( - blog, validationContext, validationResults, validateAllProperties: true)) - { - foreach (var result in validationResults) - { - ModelState.AddModelError( - result.MemberNames.FirstOrDefault() ?? string.Empty, - result.ErrorMessage ?? "Invalid value."); - } - return BadRequest(ModelState); - } - if (!ModelState.IsValid) { return BadRequest(ModelState); } + // The BlogSpotService.Create() signature requires an + // IFormFileCollection for file uploads. Reading + // Request.Form.Files when the request is a plain JSON + // body (e.g. from PostIt) throws + // "This request does not have a Content-Type header. + // Forms are available from requests with bodies like + // POSTs and a form Content-Type of either + // application/x-www-form-urlencoded or + // multipart/form-data." + // // Two valid use cases for this endpoint: // 1. JSON body only (no files) — PostIt path. // 2. multipart/form-data with a 'blog' field + 0..N - // files — PostIt "create with attachments" path. + // files — future browser / server-rendered path. // // Branch on HasFormContentType: pass the form files when - // present, pass an empty collection otherwise. Reading - // Request.Form.Files on a plain JSON request throws - // "This request does not have a Content-Type header…", - // and AttachFiles short-circuits on an empty collection. + // present, pass an empty collection otherwise. The + // FileSystem branch in BlogSpotService.Create then + // short-circuits to "no files to handle". var files = Request.HasFormContentType ? Request.Form.Files : (IFormFileCollection)new FormFileCollection(); var uid = User.GetUserId(); - var post = blogSpotService.Create(uid, blog); - blogSpotService.AttachFiles(files, uid, post.Id); + var post = blogSpotService.Create(uid, blog, files); return CreatedAtRoute("GetBlog", new { id = post.Id }, post.GetPayload()); } @@ -247,17 +210,7 @@ namespace Yavsc.Blogs.Controllers base.Dispose(disposing); } - /// - /// Read a from either a - /// JSON body or a multipart/form-data request carrying the - /// payload in a blog form field. Shared by - /// and : both - /// accept the two shapes so PostIt can attach files on - /// create as well as on update. Returns null (with - /// the reason recorded in ModelState) when the - /// multipart form has no usable blog field. - /// - private async Task ReadBlogRequestAsync() + private async Task ReadPutBlogRequestAsync() { if (!Request.HasFormContentType) { diff --git a/src/Yavsc.Org.Tests/appsettings.json b/src/Yavsc.Org.Tests/appsettings.json index f21f348ae..ef95fef61 100644 --- a/src/Yavsc.Org.Tests/appsettings.json +++ b/src/Yavsc.Org.Tests/appsettings.json @@ -9,10 +9,6 @@ "FavIcon": "/favicon.ico", "Icon": "/images/yavsc.png", "GitRepository": "testingrepo", - "Blog": "blog", - "Bills": "bills", - "TempDir": "temp", - "Avatars": "avatars", "Owner": { "Name": "Site Owner Name", "EMail": "your@email", diff --git a/src/Yavsc.Org/Controllers/Communicating/BlogspotController.cs b/src/Yavsc.Org/Controllers/Communicating/BlogspotController.cs index b55171879..98611e5fe 100644 --- a/src/Yavsc.Org/Controllers/Communicating/BlogspotController.cs +++ b/src/Yavsc.Org/Controllers/Communicating/BlogspotController.cs @@ -109,9 +109,8 @@ namespace Yavsc.Org.Controllers { if (ModelState.IsValid) { - var uid = User.GetUserId(); - Models.Blog.BlogPost post = blogSpotService.Create(uid, blogInput); - blogSpotService.AttachFiles(Request.Form.Files, uid, post.Id); + Models.Blog.BlogPost post = blogSpotService.Create(User.GetUserId(), + blogInput, Request.Form.Files); return RedirectToAction("Index"); } return View("Edit", blogInput); diff --git a/src/Yavsc.Org/Extensions/HostingExtensions.cs b/src/Yavsc.Org/Extensions/HostingExtensions.cs index 8c1db2a73..567d05233 100644 --- a/src/Yavsc.Org/Extensions/HostingExtensions.cs +++ b/src/Yavsc.Org/Extensions/HostingExtensions.cs @@ -1241,26 +1241,6 @@ ADD COLUMN IF NOT EXISTS ""Moderated"" boolean NOT NULL DEFAULT FALSE;"); SiteSettings settings, bool enableDirectoryBrowsing = false) { - if (settings.GitRepository == null) - { - throw new InvalidOperationException("GitRepository path is not configured in SiteSettings."); - } - if (settings.Blog == null) - { - throw new InvalidOperationException("Blog path is not configured in SiteSettings."); - } - if (settings.Avatars == null) - { - throw new InvalidOperationException("Avatars path is not configured in SiteSettings."); - } - if (settings.Bills == null) - { - throw new InvalidOperationException("Bills path is not configured in SiteSettings."); - } - if (settings.TempDir == null) - { - throw new InvalidOperationException("TempDir path is not configured in SiteSettings."); - } var userFilesDirInfo = new DirectoryInfo(settings.Blog); diff --git a/src/Yavsc.Org/appsettings-org.json b/src/Yavsc.Org/appsettings-org.json index 58aa287ae..add67eca4 100644 --- a/src/Yavsc.Org/appsettings-org.json +++ b/src/Yavsc.Org/appsettings-org.json @@ -33,8 +33,7 @@ "Bills": "bills", "Blog": "blog", "TempDir": "temp", - "DataDir": "data", - "GitRepository": "git" + "DataDir": "data" }, "Smtp": { "Host": "[YOURSMTPHOST]", diff --git a/src/Yavsc.Server/Services/BlogSpotService.cs b/src/Yavsc.Server/Services/BlogSpotService.cs index a0507ffe0..3d845658c 100644 --- a/src/Yavsc.Server/Services/BlogSpotService.cs +++ b/src/Yavsc.Server/Services/BlogSpotService.cs @@ -35,8 +35,14 @@ public class BlogSpotService this.siteSettings = siteSettings.Value; } - public void AttachFiles(IFormFileCollection files, string userId, long postId) + public BlogPost Create(string userId, BlogPost post, IFormFileCollection files) { + // Sauvegarder le post d'abord pour obtenir son ID + // Le createur vient de l'authentification, donc on ne le prend pas du post + post.AuthorId = userId; + _context.BlogSpot.Add(post); + _context.SaveChanges(userId); + // Traiter les fichiers attaches s'il y en a if (files != null && files.Count > 0) { @@ -46,7 +52,7 @@ public class BlogSpotService try { - string blogFilesSubdir = $"blogs/{postId}"; + string blogFilesSubdir = $"blogs/{post.Id}"; string destDir = Path.Combine( siteSettings.Blog, user.UserName, @@ -70,7 +76,7 @@ public class BlogSpotService var attachment = new BlogAttachedFile { - PostId = postId, + PostId = post.Id, FileId = uploadedFile.Id }; _context.BlogAttachedFiles.Add(attachment); @@ -83,15 +89,7 @@ public class BlogSpotService Debug.WriteLine($"Erreur lors du traitement des fichiers : {ex.Message}"); } } - } - public BlogPost Create(string userId, BlogPost post ) - { - // Sauvegarder le post d'abord pour obtenir son ID - // Le createur vient de l'authentification, donc on ne le prend pas du post - post.AuthorId = userId; - _context.BlogSpot.Add(post); - _context.SaveChanges(userId); return post; } @@ -193,6 +191,58 @@ public class BlogSpotService _context.SaveChanges(user.GetUserId()); } + public async Task Modify(ClaimsPrincipal user, BlogPost blog, IFormFileCollection files) + { + await Modify(user, blog); + + if (files == null || files.Count == 0) + return; + + var userId = user.GetUserId(); + var userEntity = _context.Users.FirstOrDefault(u => u.Id == userId); + if (userEntity == null) + return; + + try + { + string blogFilesSubdir = $"blogs/{blog.Id}"; + string destDir = Path.Combine( + siteSettings.Blog, + userEntity.UserName, + blogFilesSubdir); + var di = new DirectoryInfo(destDir); + if (!di.Exists) di.Create(); + + foreach (var formFile in files) + { + var fileInfo = userEntity.ReceiveUserFile(destDir, formFile); + if (fileInfo != null && !fileInfo.QuotaOffense) + { + var uploadedFile = new UploadedFile + { + Path = fileInfo.FileName, + ContentType = formFile.ContentType, + Length = formFile.Length + }; + _context.UploadedFiles.Add(uploadedFile); + _context.SaveChanges(userId); + + var attachment = new BlogAttachedFile + { + PostId = blog.Id, + FileId = uploadedFile.Id + }; + _context.BlogAttachedFiles.Add(attachment); + } + } + + _context.SaveChanges(userId); + } + catch (Exception ex) + { + Debug.WriteLine($"Erreur lors du traitement des fichiers : {ex.Message}"); + } + } public async Task> Index(ClaimsPrincipal user, string id, int skip = 0, int take = 25) {