feat: enhance thumbnail generation with category support and update storage paths
This commit is contained in:
@@ -106,7 +106,8 @@ public class SendMessageCommandHandler : IBaseCommandHandler<SendMessageCommand,
|
||||
uploadedFile.SetImageDimensions(dimensions.Value.Width, dimensions.Value.Height);
|
||||
}
|
||||
|
||||
var thumbnail = await _thumbnailService.GenerateImageThumbnailAsync(uploadResult.StoragePath);
|
||||
var thumbnail = await _thumbnailService
|
||||
.GenerateImageThumbnailAsync(uploadResult.StoragePath, category: "TaskChatMessage");
|
||||
if (thumbnail.HasValue)
|
||||
{
|
||||
uploadedFile.SetThumbnail(thumbnail.Value.ThumbnailUrl);
|
||||
|
||||
@@ -9,15 +9,17 @@ public interface IThumbnailGeneratorService
|
||||
/// تولید thumbnail برای تصویر
|
||||
/// </summary>
|
||||
Task<(string ThumbnailPath, string ThumbnailUrl)?> GenerateImageThumbnailAsync(
|
||||
string imagePath,
|
||||
int width = 200,
|
||||
string imagePath,
|
||||
string category,
|
||||
int width = 200,
|
||||
int height = 200);
|
||||
|
||||
/// <summary>
|
||||
/// تولید thumbnail برای ویدیو
|
||||
/// </summary>
|
||||
Task<(string ThumbnailPath, string ThumbnailUrl)?> GenerateVideoThumbnailAsync(
|
||||
string videoPath);
|
||||
string videoPath,
|
||||
string category);
|
||||
|
||||
/// <summary>
|
||||
/// حذف thumbnail
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using GozareshgirProgramManager.Application.Services.FileManagement;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
@@ -14,12 +15,21 @@ public class ThumbnailGeneratorService : IThumbnailGeneratorService
|
||||
private readonly string _thumbnailBasePath;
|
||||
private readonly string _baseUrl;
|
||||
|
||||
public ThumbnailGeneratorService(IConfiguration configuration)
|
||||
public ThumbnailGeneratorService(IConfiguration configuration,
|
||||
IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_thumbnailBasePath = configuration["FileStorage:ThumbnailPath"]
|
||||
?? Path.Combine(Directory.GetCurrentDirectory(), "Uploads", "Thumbnails");
|
||||
?? Path.Combine(Directory.GetCurrentDirectory(), "storage", "Thumbnails");
|
||||
var request = httpContextAccessor.HttpContext?.Request;
|
||||
|
||||
_baseUrl = configuration["FileStorage:BaseUrl"] ?? "http://localhost:5000/uploads";
|
||||
if (request != null)
|
||||
{
|
||||
_baseUrl = $"{request.Scheme}://{request.Host}/storage";
|
||||
}
|
||||
else
|
||||
{
|
||||
_baseUrl = configuration["FileStorage:BaseUrl"] ?? "http://localhost:5000/storage";
|
||||
}
|
||||
|
||||
if (!Directory.Exists(_thumbnailBasePath))
|
||||
{
|
||||
@@ -28,7 +38,8 @@ public class ThumbnailGeneratorService : IThumbnailGeneratorService
|
||||
}
|
||||
|
||||
public async Task<(string ThumbnailPath, string ThumbnailUrl)?> GenerateImageThumbnailAsync(
|
||||
string imagePath,
|
||||
string imagePath,
|
||||
string category,
|
||||
int width = 200,
|
||||
int height = 200)
|
||||
{
|
||||
@@ -54,14 +65,21 @@ public class ThumbnailGeneratorService : IThumbnailGeneratorService
|
||||
var extension = Path.GetExtension(imagePath);
|
||||
var thumbnailFileName = $"{fileName}_thumb{extension}";
|
||||
|
||||
var categoryFolder = string.IsNullOrWhiteSpace(category) ? "general" : category;
|
||||
var categoryPath = Path.Combine(_thumbnailBasePath, categoryFolder);
|
||||
if (!Directory.Exists(categoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(categoryPath);
|
||||
}
|
||||
|
||||
// مسیر ذخیره thumbnail
|
||||
var thumbnailPath = Path.Combine(_thumbnailBasePath, thumbnailFileName);
|
||||
var thumbnailPath = Path.Combine(categoryPath, thumbnailFileName);
|
||||
|
||||
// ذخیره thumbnail با کیفیت 80
|
||||
await image.SaveAsync(thumbnailPath, new JpegEncoder { Quality = 80 });
|
||||
|
||||
// URL thumbnail
|
||||
var thumbnailUrl = $"{_baseUrl}/thumbnails/{thumbnailFileName}";
|
||||
var thumbnailUrl = $"{_baseUrl}/thumbnails/{categoryFolder}/{thumbnailFileName}";
|
||||
|
||||
return (thumbnailPath, thumbnailUrl);
|
||||
}
|
||||
@@ -72,7 +90,7 @@ public class ThumbnailGeneratorService : IThumbnailGeneratorService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(string ThumbnailPath, string ThumbnailUrl)?> GenerateVideoThumbnailAsync(string videoPath)
|
||||
public async Task<(string ThumbnailPath, string ThumbnailUrl)?> GenerateVideoThumbnailAsync(string videoPath, string category = "general")
|
||||
{
|
||||
// TODO: برای Video thumbnail باید از FFmpeg استفاده کنیم
|
||||
// فعلاً یک placeholder image برمیگردانیم
|
||||
@@ -108,4 +126,3 @@ public class ThumbnailGeneratorService : IThumbnailGeneratorService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user