diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index 4f8ea027..545a17c3 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -513,6 +513,24 @@ namespace RMuseum.Controllers } } + /// + /// generate missing book covers + /// + /// + [HttpPut("generatemissingbookcovers")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.Unauthorized)] + [ProducesResponseType((int)HttpStatusCode.Forbidden, Type = typeof(string))] + public async Task GenerateMissingBookCoversAsync() + { + var res = await _ganjoorService.GenerateMissingBookCoversAsync(); + if(!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(); + } + /// /// list of books /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index f9d7c8f8..629492d5 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -996,6 +996,12 @@ only RImageId field is valid + + + generate missing book covers + + + list of books @@ -12532,6 +12538,12 @@ + + + generate missing book covers + + + get page by url @@ -15724,6 +15736,12 @@ + + + generate missing book covers + + + get cat by id diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index f7be0ee5..14697be1 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -79,6 +79,12 @@ namespace RMuseum.Services /// Task> GetBooksAsync(); + /// + /// generate missing book covers + /// + /// + Task> GenerateMissingBookCoversAsync(); + /// /// get page by url /// diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 1b46dc6f..ac24d47b 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -24,6 +24,10 @@ using RMuseum.Models.Auth.Memory; using System.IO; using RSecurityBackend.Models.Image; using FluentFTP; +using NAudio.Gui; +using System.Drawing; +using static System.Net.Mime.MediaTypeNames; +using Microsoft.IdentityModel.Tokens; namespace RMuseum.Services.Implementation { @@ -273,6 +277,58 @@ namespace RMuseum.Services.Implementation } } + /// + /// generate missing book covers + /// + /// + public async Task> GenerateMissingBookCoversAsync() + { + try + { + var books = await _context.GanjoorCategories.Where(c => !string.IsNullOrEmpty(c.BookName) && c.RImageId == null).ToListAsync(); + foreach (var book in books) + { + using (System.Drawing.Image coverImg = System.Drawing.Image.FromFile(Configuration.GetSection("Ganjoor")["BooksCoverTemplate"])) + { + using (Graphics g = Graphics.FromImage(coverImg)) + { + g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; + g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + using(Font font = new Font(Configuration.GetSection("Ganjoor")["BooksCoverTitleFontName"], float.Parse(Configuration.GetSection("Ganjoor")["BooksCoverTitleFontSize"]))) + { + SizeF sz = g.MeasureString(book.BookName, font); + using (SolidBrush brsh = new SolidBrush(Color.FromArgb(51, 0, 0))) + g.DrawString(book.BookName, font, brsh, new PointF(200.0f - sz.Width / 2, 230.0f - sz.Height / 2)); + } + + } + MemoryStream coverData = new MemoryStream(); + coverImg.Save(coverData, System.Drawing.Imaging.ImageFormat.Png); + RServiceResult imageRes = await _imageFileService.Add(null, coverData, $"Book-{book.Id}", Path.Combine(Configuration.GetSection("PictureFileService")["StoragePath"], "CategoryImages")); + if(!string.IsNullOrEmpty(imageRes.ExceptionString)) + { + return new RServiceResult(false, imageRes.ExceptionString); + } + imageRes = await _imageFileService.Store(imageRes.Result); + if (!string.IsNullOrEmpty(imageRes.ExceptionString)) + { + return new RServiceResult(false, imageRes.ExceptionString); + } + var finalRes = await SetCategoryExtraInfo(book.Id, book.BookName, imageRes.Result.Id, book.SumUpSubsGeoLocations, book.MapName); + if(!string.IsNullOrEmpty(finalRes.ExceptionString)) + { + return new RServiceResult(false, finalRes.ExceptionString); + } + } + } + return new RServiceResult(true); + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + } + /// /// get cat by id /// diff --git a/RMuseum/appsettings.json b/RMuseum/appsettings.json index 1b9ab939..488c5984 100644 --- a/RMuseum/appsettings.json +++ b/RMuseum/appsettings.json @@ -62,7 +62,10 @@ "GDBStorage": "D:\\My Documents\\My Web Design\\ganjoor\\www\\i\\android\\sgdb", "GDBStorageImageSource": "D:\\My Documents\\My Web Design\\ganjoor\\www\\i\\android\\img", "GDBListXMLFile": "D:\\My Documents\\My Web Design\\ganjoor\\www\\i\\android\\sitepoets.xml", - "GDBPreExisitingListXMLFile": "D:\\My Documents\\My Web Design\\ganjoor\\www\\i\\android\\programpoets.xml" + "GDBPreExisitingListXMLFile": "D:\\My Documents\\My Web Design\\ganjoor\\www\\i\\android\\programpoets.xml", + "BooksCoverTemplate": "C:\\inetpub\\epub\\res\\cover-image.png", + "BooksCoverTitleFontName": "IranNastaliq", + "BooksCoverTitleFontSize": "40" }, "ExternalFTPServer": { "Host": "localhost",