refacto site settings
Some checks failed
Dotnet build and test / build (pull_request) Failing after 8m29s
Some checks failed
Dotnet build and test / build (pull_request) Failing after 8m29s
This commit is contained in:
parent
3e7e76edf2
commit
3bc4142383
33 changed files with 399 additions and 315 deletions
|
|
@ -5,6 +5,8 @@ using System.Security.Claims;
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Yavsc;
|
||||
using Yavsc.Blogspot;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Access;
|
||||
|
|
@ -19,72 +21,77 @@ public class BlogSpotService
|
|||
private readonly ApplicationDbContext _context;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IFileSystemAuthManager fileSystemAuthManager;
|
||||
private readonly SiteSettings siteSettings;
|
||||
|
||||
public BlogSpotService(
|
||||
ApplicationDbContext context,
|
||||
IAuthorizationService authorizationService,
|
||||
IFileSystemAuthManager fileSystemAuthManager)
|
||||
IFileSystemAuthManager fileSystemAuthManager,
|
||||
IOptions<SiteSettings> siteSettings)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_context = context;
|
||||
this.fileSystemAuthManager = fileSystemAuthManager;
|
||||
this.siteSettings = siteSettings.Value;
|
||||
}
|
||||
|
||||
public BlogPost Create(string userId, BlogPost post, IFormFileCollection files)
|
||||
public void AttachFiles(IFormFileCollection files, string userId, long postId)
|
||||
{
|
||||
// Traiter les fichiers attaches s'il y en a
|
||||
if (files != null && files.Count > 0)
|
||||
{
|
||||
var user = _context.Users.FirstOrDefault(u => u.Id == userId);
|
||||
if (user == null)
|
||||
throw new InvalidOperationException($"Utilisateur {userId} non trouvé.");
|
||||
|
||||
try
|
||||
{
|
||||
string blogFilesSubdir = $"blogs/{postId}";
|
||||
string destDir = Path.Combine(
|
||||
siteSettings.Blog,
|
||||
user.UserName,
|
||||
blogFilesSubdir);
|
||||
var di = new DirectoryInfo(destDir);
|
||||
if (!di.Exists) di.Create();
|
||||
|
||||
foreach (var formFile in files)
|
||||
{
|
||||
var fileInfo = user.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 = postId,
|
||||
FileId = uploadedFile.Id
|
||||
};
|
||||
_context.BlogAttachedFiles.Add(attachment);
|
||||
}
|
||||
}
|
||||
_context.SaveChanges(userId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
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);
|
||||
|
||||
// Traiter les fichiers attaches s'il y en a
|
||||
if (files != null && files.Count > 0)
|
||||
{
|
||||
var user = _context.Users.FirstOrDefault(u => u.Id == userId);
|
||||
if (user != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
string blogFilesSubdir = $"blogs/{post.Id}";
|
||||
string destDir = Path.Combine(
|
||||
AbstractFileSystemHelpers.UserFilesDirName,
|
||||
user.UserName,
|
||||
blogFilesSubdir);
|
||||
var di = new DirectoryInfo(destDir);
|
||||
if (!di.Exists) di.Create();
|
||||
|
||||
foreach (var formFile in files)
|
||||
{
|
||||
var fileInfo = user.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 = post.Id,
|
||||
FileId = uploadedFile.Id
|
||||
};
|
||||
_context.BlogAttachedFiles.Add(attachment);
|
||||
}
|
||||
}
|
||||
_context.SaveChanges(userId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"Erreur lors du traitement des fichiers : {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
|
|
@ -186,58 +193,6 @@ 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(
|
||||
AbstractFileSystemHelpers.UserFilesDirName,
|
||||
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<IEnumerable<IBlogPost>> Index(ClaimsPrincipal user, string id, int skip = 0, int take = 25)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue