From 83cd3172038f71af9889b2760ebe4aedd958aeb5 Mon Sep 17 00:00:00 2001 From: "Amir.H Khademi" Date: Wed, 17 Apr 2024 16:19:40 +0330 Subject: [PATCH] CHANGE PROJECT NAME TO "NETINA" --- .../appsettings.DevelopmentHamyan.json | 78 +++++++++++++++++++ ...son => appsettings.DevelopmentVesmeh.json} | 0 Netina.Api/Controller/FileController.cs | 4 +- Netina.Api/Controller/SeedController.cs | 16 ++-- Netina.Api/Program.cs | 14 +--- Netina.Core/Utilities/ImageConvertor.cs | 11 ++- .../SeedDtos/SeedBlogRequestDto.cs | 1 + .../Entities/Blogs/BlogStorageFile.cs | 5 +- .../Program.cs | 69 ++++++++++++---- .../Services/RestServices/ISeedRestApi.cs | 9 ++- .../Services/RestServices/RestWrapper.cs | 12 ++- 11 files changed, 172 insertions(+), 47 deletions(-) create mode 100644 Netina.Api/AppSettings/appsettings.DevelopmentHamyan.json rename Netina.Api/AppSettings/{appsettings.Development.json => appsettings.DevelopmentVesmeh.json} (100%) diff --git a/Netina.Api/AppSettings/appsettings.DevelopmentHamyan.json b/Netina.Api/AppSettings/appsettings.DevelopmentHamyan.json new file mode 100644 index 0000000..af5a4b3 --- /dev/null +++ b/Netina.Api/AppSettings/appsettings.DevelopmentHamyan.json @@ -0,0 +1,78 @@ +{ + "ConnectionStrings": { + "PostgresServer": "User ID=postgres;Password=root;Host=localhost;Port=5432;Database=iGarsonDB;", + "Postgres": "Host=pg-0,pg-1;Username=igarsonAgent;Password=xHTpBf4wC+bBeNg2pL6Ga7VEWKFJx7VPEUpqxwPFfOc2YYTVwFQuHfsiqoVeT9+6;Database=NetinaShopDB02;Load Balance Hosts=true;Target Session Attributes=primary;Application Name=iGLS", + "MartenDB": "Host=pg-0,pg-1;Username=igarsonAgent;Password=xHTpBf4wC+bBeNg2pL6Ga7VEWKFJx7VPEUpqxwPFfOc2YYTVwFQuHfsiqoVeT9+6;Database=NetinaShopMartenDB;" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "None", + "Microsoft.Hosting.Lifetime": "Information", + "Microsoft.AspNetCore.SignalR": "Debug", + "Microsoft.AspNetCore.Http.Connections": "Debug" + } + }, + "ShopSettings": { + "TaxesFee": 9 + }, + "SiteSettings": { + "BaseUrl": "http://localhost:32770", + "WebSiteUrl": "https://hamyanedalat.com", + "AdminPanelBaseUrl": "https://admin.hamyanedalat.com", + "StorageBaseUrl": "https://storage.hamyanedalat.com", + "KaveNegarApiKey": "3735494B4143727A794346457461576A2B4B6668414973424E333561505A694B", + "UserSetting": { + "Username": "09214802813", + "Email": "avvampier@gmail.com", + "Password": "eF79o4P4BopCUbUK", + "Phone": "09214802813", + "RoleName": "RootAdmin", + "FirstName": "همه کاره", + "LastName": "سیستم" + }, + "Manager": { + "Username": "09122171024", + "Email": "info@hamyanedalat.com", + "Password": "eF79o4P4BopCUbUK", + "Phone": "09122171024", + "RoleName": "Manager", + "FirstName": "کامران", + "LastName": "میرزایی" + }, + "StorageSetting": { + "AccessKey": "de129835-b43a-4552-8ed4-865e2635ab3d", + "SecretKey": "f00197a766336e818023f90408ba3b88b4fdc86d0e7180f74f199d9f03db7575", + "BucketKey": "hamyan-content" + }, + "JwtSettings": { + "SecretKey": "YAEMAMZAMAN_KHODET_NEGAHDAR_IN_KEY_BASH_nw+8E0EABj0Wg8c4mHg/bDBf5qGMhmBPb6u16DVe9/MzYva1e+/J1zImyIoQX2Lmra2kvzsIjGiwP7r3Znd_YA_JADE_NASABE_v+Ro/CDixScDv6EkpZnkBv9MFdPnSmFXNGMH9gA1BzQUoC1iSX9Aq+pMIw/cMKXI9WA==_YA_HUSEIN_SEYED_SHOHADA_BE_OMID_KHODET", + "Issuer": "HamaynEdalat", + "Audience": "HamaynEdalat", + "ExpireAddDay": "15" + } + }, + "IpRateLimiting": { + "EnableEndpointRateLimiting": false, + "StackBlockedRequests": false, + "RealIpHeader": "X-Real-IP", + "ClientIdHeader": "X-ClientId", + "HttpStatusCode": 429, + "IpWhitelist": [ "127.0.0.1", "::1/10", "192.168.0.0/24" ], + "EndpointWhitelist": [ "get:/api/license", "*:/api/status" ], + "ClientWhitelist": [ "dev-id-1", "dev-id-2" ], + "GeneralRules": [ + { + "Endpoint": "*", + "Period": "1m", + "Limit": 60 + }, + { + "Endpoint": "*", + "Period": "15m", + "Limit": 250 + } + ] + }, + "AllowedHosts": "*" +} \ No newline at end of file diff --git a/Netina.Api/AppSettings/appsettings.Development.json b/Netina.Api/AppSettings/appsettings.DevelopmentVesmeh.json similarity index 100% rename from Netina.Api/AppSettings/appsettings.Development.json rename to Netina.Api/AppSettings/appsettings.DevelopmentVesmeh.json diff --git a/Netina.Api/Controller/FileController.cs b/Netina.Api/Controller/FileController.cs index 4b809bf..28f8acc 100644 --- a/Netina.Api/Controller/FileController.cs +++ b/Netina.Api/Controller/FileController.cs @@ -10,8 +10,7 @@ public class FileController : ICarterModule public void AddRoutes(IEndpointRouteBuilder app) { var group = app.NewVersionedApi("File") - .MapGroup("api/file") - .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser()); + .MapGroup("api/file"); group.MapGet("", GetFilesAsync) .WithDisplayName("GetFilesAsync") @@ -20,7 +19,6 @@ public class FileController : ICarterModule group.MapPost("", UploadFileAsync) .WithDisplayName("UploadFileAsync") - .RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageFiles)) .HasApiVersion(1.0); } diff --git a/Netina.Api/Controller/SeedController.cs b/Netina.Api/Controller/SeedController.cs index 045a2c1..2f247f0 100644 --- a/Netina.Api/Controller/SeedController.cs +++ b/Netina.Api/Controller/SeedController.cs @@ -1,13 +1,7 @@ -using Netina.Common.Models.Api; -using Netina.Common.Models.Exception; -using Netina.Domain.CommandQueries.Commands; -using Netina.Domain.Dtos.RequestDtos.SeedDtos; -using Netina.Domain.Dtos.SmallDtos; +using Netina.Domain.Dtos.RequestDtos.SeedDtos; using Netina.Domain.Entities.Blogs; -using Netina.Repository.Repositories.Base; namespace Netina.Api.Controller; - public class SeedController : ICarterModule { private readonly IWebHostEnvironment _environment; @@ -63,6 +57,7 @@ public class SeedController : ICarterModule return TypedResults.Ok(); } + public async Task SeedCategoriesAsync([FromBody] List request, [FromQuery] string key, IMediator mediator,CancellationToken cancellationToken) { if (key != "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A==") @@ -112,6 +107,13 @@ public class SeedController : ICarterModule { var ent = Blog.Create(seedBlogRequestDto.Title, seedBlogRequestDto.Content, seedBlogRequestDto.Tags, seedBlogRequestDto.ReadingTime, seedBlogRequestDto.Summery, seedBlogRequestDto.IsSuggested, seedBlogRequestDto.CategoryId); + + foreach (var storageFileSDto in seedBlogRequestDto.Files) + { + ent.AddFile(storageFileSDto.Name, storageFileSDto.FileLocation, storageFileSDto.FileName, true, true, + storageFileSDto.FileType); + } + repositoryWrapper.SetRepository().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); } diff --git a/Netina.Api/Program.cs b/Netina.Api/Program.cs index a01021e..ee51792 100644 --- a/Netina.Api/Program.cs +++ b/Netina.Api/Program.cs @@ -1,12 +1,3 @@ -using Netina.Api.WebFramework.Configurations; -using Netina.Api.WebFramework.MiddleWares; -using Netina.Api.WebFramework.Swagger; -using Netina.Common.Models; -using Netina.Core; -using Netina.Domain; -using Netina.Domain.Models.Settings; -using Netina.Infrastructure; -using Netina.Repository; using Netina.Repository.Behaviors; var builder = WebApplication.CreateBuilder(args); @@ -15,10 +6,9 @@ builder.Host.UseSerilog(); LoggerConfig.ConfigureSerilog(); string env = builder.Environment.IsDevelopment() == true ? "Development" : "Production"; builder.Host.UseContentRoot(Directory.GetCurrentDirectory()); +string projectName = "Hamyan"; if (builder.Environment.IsDevelopment()) - builder.Configuration - .AddJsonFile($"AppSettings/appsettings.json") - .AddJsonFile($"AppSettings/appsettings.{env}.json"); + builder.Configuration.AddJsonFile($"AppSettings/appsettings.json").AddJsonFile($"AppSettings/appsettings.{env}{projectName}.json"); if (builder.Environment.IsProduction()) builder.Configuration.AddJsonFile($"AppSettings/Production/appsettings.{env}.json"); diff --git a/Netina.Core/Utilities/ImageConvertor.cs b/Netina.Core/Utilities/ImageConvertor.cs index 5c7cce8..4cb3253 100644 --- a/Netina.Core/Utilities/ImageConvertor.cs +++ b/Netina.Core/Utilities/ImageConvertor.cs @@ -9,10 +9,13 @@ public static class ImageConvertor public static async Task ImageResize(this FileUploadRequest fileUpload, Stream input, Stream output, int newWidth) { using var image = await Image.LoadAsync(input); - var height_width = image.Width /image.Height; - var new_Height = newWidth * height_width; - image.Mutate(x => x.Resize(newWidth, new_Height)); - image.Mutate(x => x.Resize(newWidth, new_Height)); + if (image.Width > newWidth) + { + double ratio = (double)newWidth / image.Width; + int new_Height = (int)Math.Round(image.Height * ratio); + image.Mutate(x => x.Resize(newWidth, new_Height)); + image.Mutate(x => x.Resize(newWidth, new_Height)); + } await image.SaveAsJpegAsync(output); return output; } diff --git a/Netina.Domain/Dtos/RequestDtos/SeedDtos/SeedBlogRequestDto.cs b/Netina.Domain/Dtos/RequestDtos/SeedDtos/SeedBlogRequestDto.cs index d121fdf..c832cb5 100644 --- a/Netina.Domain/Dtos/RequestDtos/SeedDtos/SeedBlogRequestDto.cs +++ b/Netina.Domain/Dtos/RequestDtos/SeedDtos/SeedBlogRequestDto.cs @@ -9,6 +9,7 @@ public class SeedBlogRequestDto public string Summery { get; set; } = string.Empty; public bool IsSuggested { get; set; } public Guid CategoryId { get; set; } + public List Files { get; set; } = new(); } public class SeedBlogCategoryRequestDto diff --git a/Netina.Domain/Entities/Blogs/BlogStorageFile.cs b/Netina.Domain/Entities/Blogs/BlogStorageFile.cs index f402411..ee1cc68 100644 --- a/Netina.Domain/Entities/Blogs/BlogStorageFile.cs +++ b/Netina.Domain/Entities/Blogs/BlogStorageFile.cs @@ -1,7 +1,4 @@ -using Netina.Domain.Entities.StorageFiles; -using Netina.Domain.Enums; - -namespace Netina.Domain.Entities.Blogs; +namespace Netina.Domain.Entities.Blogs; public partial class BlogStorageFile : StorageFile { diff --git a/Tools/NetinaShop.WordPressDBConverter/Program.cs b/Tools/NetinaShop.WordPressDBConverter/Program.cs index 217e0b7..00b0f14 100644 --- a/Tools/NetinaShop.WordPressDBConverter/Program.cs +++ b/Tools/NetinaShop.WordPressDBConverter/Program.cs @@ -1,10 +1,21 @@ using Netina.Common.Extensions; -using Netina.Domain.CommandQueries.Commands; using Netina.Domain.Dtos.RequestDtos.SeedDtos; -using Netina.Domain.Dtos.SmallDtos; +using Netina.Domain.Entities.Blogs; using Netina.WordPressDBConverter.Models; -using Netina.WordPressDBConverter.Services.RestServices; using Newtonsoft.Json; +using Netina.Common.Models.Api; +using Netina.Domain.Dtos.SmallDtos; +using Netina.Domain.Enums; +using Netina.WordPressDBConverter.Services.RestServices; +using Refit; + + +var postMetaReader = new StreamReader("F:\\wp_postmeta.json"); +var postMetaJson = postMetaReader.ReadToEnd(); +Console.WriteLine("Terms File Read Success !"); +var postMetas = JsonConvert.DeserializeObject>(postMetaJson); +if (postMetas == null) + throw new Exception("Terms is null"); var termReader = new StreamReader("F:\\wp_terms.json"); @@ -37,8 +48,10 @@ if (posts == null) throw new Exception("Posts is null"); + //CREATE CATEGORY PART List categories = new List(); + List tags = new List(); foreach (var taxonomy in termTaxonomies) { @@ -48,8 +61,8 @@ foreach (var taxonomy in termTaxonomies) if (term != null) categories.Add(new SeedBlogCategoryRequestDto { - BaseCategoryId = term.term_id.ToInt(), - Description = taxonomy.description, + BaseCategoryId = term.term_id.ToInt(), + Description = taxonomy.description, Name = term.name }); } @@ -64,15 +77,17 @@ foreach (var taxonomy in termTaxonomies) Tag = term.name }); } -} -var categoriesRest = await RestWrapper.Instance.SeedRestApi.SeedBlogCategoriesAsync(categories, "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A=="); -Console.WriteLine($"{categories.Count} ProductCategory Added Success !"); +} +var categoriesRest = new Dictionary(); +categoriesRest = await RestWrapper.Instance.SeedRestApi.SeedBlogCategoriesAsync(categories, "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A=="); +Console.WriteLine($"{categories.Count} BlogCategory Added Success !"); //CREATE BLOG PART List blogs = new List(); var random = new Random(1532); +int thumbCounter = 0; foreach (var wordPressPostDto in posts.Where(p => p.post_type == "post")) { SeedBlogRequestDto blog; @@ -94,6 +109,35 @@ foreach (var wordPressPostDto in posts.Where(p => p.post_type == "post")) if (categoryId == default) categoryId = categoriesRest.FirstOrDefault(c => c.Key == 0).Value; + var files = new List(); + + var thumbnailMeta = postMetas.FirstOrDefault(pm => pm.post_id == wordPressPostDto.ID && pm.meta_key == "_thumbnail_id"); + if (thumbnailMeta != null) + { + var thumbnail = posts.FirstOrDefault(p => p.ID.ToInt() == thumbnailMeta.meta_value.ToInt()); + if (thumbnail != null) + { + using var httpClient = new HttpClient(); + var imageBytes = await httpClient.GetByteArrayAsync(thumbnail.guid); + var uploadFile = new FileUploadRequest + { + ContentType = thumbnail.post_mime_type, + FileName = $"{StringExtensions.GetId(10)}.jpeg", + FileUploadType = FileUploadType.Image, + StringBaseFile = Convert.ToBase64String(imageBytes), + }; + var fileRest = await RestWrapper.Instance.UploadRestApi.UploadAsync(uploadFile); + + files.Add(new StorageFileSDto + { + FileLocation = fileRest.FileLocation, + FileName = fileRest.FileName, + Name = fileRest.FileName, + FileType = StorageFileType.Image + }); + Console.WriteLine($"Thumbnail uploaded : {thumbCounter++}"); + } + } blog = new SeedBlogRequestDto { @@ -102,17 +146,14 @@ foreach (var wordPressPostDto in posts.Where(p => p.post_type == "post")) IsSuggested = false, ReadingTime = random.Next(1, 15), Title = wordPressPostDto.post_title, + Files = files }; blogs.Add(blog); -} -for (int i = 0; i < blogs.Count / 50; i++) -{ - await RestWrapper.Instance.SeedRestApi.SeedBlogsAsync(blogs.Skip(i * 50).Take(50).ToList(), - "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A=="); + await RestWrapper.Instance.SeedRestApi.SeedBlogsAsync(new List{blog}, "kKAYskyG8xPxKnJrHkuYxub4Ao2bnz7AOmNtwDT0RaqzaG7ZvbvaP29tCrC8wJ823RczJFXOIQT2bDOec4F38A=="); - Console.WriteLine($"{i * 50} / {blogs.Count} Product Added Success !"); + Console.WriteLine($"{thumbCounter++ } / {blogs.Count} Blog Added Success !"); } diff --git a/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/ISeedRestApi.cs b/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/ISeedRestApi.cs index 5180052..ca04551 100644 --- a/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/ISeedRestApi.cs +++ b/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/ISeedRestApi.cs @@ -1,4 +1,5 @@ -using Netina.Domain.CommandQueries.Commands; +using Netina.Common.Models.Api; +using Netina.Domain.CommandQueries.Commands; using Netina.Domain.Dtos.RequestDtos.SeedDtos; using Refit; @@ -21,4 +22,10 @@ public interface ISeedRestApi [Post("/blogs")] Task SeedBlogsAsync([Body] List request, [Query] string key); +} + +public interface IUploadRestApi +{ + [Post("")] + Task UploadAsync([Body] FileUploadRequest uploadRequest); } \ No newline at end of file diff --git a/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/RestWrapper.cs b/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/RestWrapper.cs index 0106c2d..95261e8 100644 --- a/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/RestWrapper.cs +++ b/Tools/NetinaShop.WordPressDBConverter/Services/RestServices/RestWrapper.cs @@ -1,11 +1,19 @@ -using Refit; +using Newtonsoft.Json; +using Refit; namespace Netina.WordPressDBConverter.Services.RestServices; public class RestWrapper { + private static RefitSettings setting = new RefitSettings(new NewtonsoftJsonContentSerializer(new JsonSerializerSettings + { + Formatting = Newtonsoft.Json.Formatting.Indented, + ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, + + })); private static RestWrapper? _instance; public static RestWrapper Instance => _instance = _instance ?? new RestWrapper(); - public ISeedRestApi SeedRestApi => RestService.For("http://localhost:32770/api/seed"); + public ISeedRestApi SeedRestApi => RestService.For("http://localhost:32770/api/seed", setting); + public IUploadRestApi UploadRestApi => RestService.For("http://localhost:32770/api/file", setting); } \ No newline at end of file