using DiwanRazor.Utils; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RMuseum.Models.Diwan; using RMuseum.Models.Diwan.ViewModels; using RSecurityBackend.Models.Auth.ViewModels; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace DiwanRazor.Areas.Admin.Pages { public class ModifyPageModel : PageModel { /// /// HttpClient instance /// private readonly HttpClient _httpClient; /// /// constructor /// /// public ModifyPageModel(HttpClient httpClient) { _httpClient = httpClient; } /// /// Corresponding Ganojoor Page /// /// /// api model /// [BindProperty] public DiwanModifyPageViewModel ModifyModel { get; set; } /// /// rythm /// public DiwanMetre[] Rhythms { get; set; } /// /// page /// public DiwanPageCompleteViewModel PageInformation { get; set; } /// /// diwan toc /// [BindProperty] public DiwanTOC DiwanTOC { get; set; } /// /// last message /// public string LastMessage { get; set; } public string DefaultHtmlText { get; set; } private static string _DefHtmlText(DiwanVerseViewModel[] verses) { string htmlText = ""; int coupletIndex = 0; VersePosition position = VersePosition.Right; for (int vIndex = 0; vIndex < verses.Length; vIndex++) { DiwanVerseViewModel v = verses[vIndex]; if (position == VersePosition.Right) { coupletIndex++; htmlText += $"

{v.Text}

{Environment.NewLine}"; position = VersePosition.Left; } else { htmlText += $"

{v.Text}

{Environment.NewLine}"; position = VersePosition.Right; } } return htmlText.Trim(); } private async Task PreparePage() { var rhythmResponse = await _httpClient.GetAsync($"{APIRoot.Url}/api/diwan/rhythms"); if (!rhythmResponse.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await rhythmResponse.Content.ReadAsStringAsync()); return false; } Rhythms = JsonConvert.DeserializeObject(await rhythmResponse.Content.ReadAsStringAsync()); if (Request.Query["id"] == "0") { ModifyModel = new DiwanModifyPageViewModel(); PageInformation = new DiwanPageCompleteViewModel(); return true; } var pageUrlResponse = await _httpClient.GetAsync($"{APIRoot.Url}/api/diwan/pageurl?id={Request.Query["id"]}"); if (!pageUrlResponse.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await pageUrlResponse.Content.ReadAsStringAsync()); return false; } var pageUrl = JsonConvert.DeserializeObject(await pageUrlResponse.Content.ReadAsStringAsync()); var pageQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/diwan/page?url={pageUrl}"); if (!pageQuery.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await pageQuery.Content.ReadAsStringAsync()); return false; } PageInformation = JObject.Parse(await pageQuery.Content.ReadAsStringAsync()).ToObject(); if (PageInformation.Poem != null) { DefaultHtmlText = _DefHtmlText(PageInformation.Poem.Verses); } ModifyModel = new DiwanModifyPageViewModel() { Title = PageInformation.Title, UrlSlug = PageInformation.UrlSlug, HtmlText = PageInformation.HtmlText, Note = "", SourceName = PageInformation.Poem == null ? null : PageInformation.Poem.SourceName, SourceUrlSlug = PageInformation.Poem == null ? null : PageInformation.Poem.SourceUrlSlug, OldTag = PageInformation.Poem == null ? null : PageInformation.Poem.OldTag, OldTagPageUrl = PageInformation.Poem == null ? null : PageInformation.Poem.OldTagPageUrl, NoIndex = PageInformation.NoIndex, RedirectFromFullUrl = PageInformation.RedirectFromFullUrl, Language = PageInformation.Poem == null ? null : PageInformation.Poem.Language, MixedModeOrder = PageInformation.Poem == null ? (PageInformation.DiwanPageType == DiwanPageType.CatPage || PageInformation.DiwanPageType == DiwanPageType.PoetPage ? PageInformation.PoetOrCat.Cat.MixedModeOrder : 0) : PageInformation.Poem.MixedModeOrder, Published = PageInformation.Poem == null ? (PageInformation.DiwanPageType == DiwanPageType.CatPage || PageInformation.DiwanPageType == DiwanPageType.PoetPage ? PageInformation.PoetOrCat.Cat.Published : true) : PageInformation.Poem.Published, TableOfContentsStyle = PageInformation.DiwanPageType == DiwanPageType.CatPage || PageInformation.DiwanPageType == DiwanPageType.PoetPage ? PageInformation.PoetOrCat.Cat.TableOfContentsStyle : DiwanTOC.Analyse, CatType = PageInformation.DiwanPageType == DiwanPageType.CatPage || PageInformation.DiwanPageType == DiwanPageType.PoetPage ? PageInformation.PoetOrCat.Cat.CatType : DiwanCatType.Default, Description = PageInformation.DiwanPageType == DiwanPageType.CatPage || PageInformation.DiwanPageType == DiwanPageType.PoetPage ? PageInformation.PoetOrCat.Cat.Description : "", DescriptionHtml = PageInformation.DiwanPageType == DiwanPageType.CatPage || PageInformation.DiwanPageType == DiwanPageType.PoetPage ? PageInformation.PoetOrCat.Cat.DescriptionHtml : "" }; DiwanTOC = ModifyModel.TableOfContentsStyle; return true; } public async Task OnGetAsync() { if (string.IsNullOrEmpty(Request.Cookies["Token"])) return Redirect("/"); LastMessage = Request.Query["edit"] == "true" ? "ویرایش انجام شد." : ""; if (string.IsNullOrEmpty(Request.Query["id"])) { return StatusCode(StatusCodes.Status500InternalServerError, "شناسهٔ صفحه مشخص نیست."); } DiwanTOC = DiwanTOC.Analyse; await PreparePage(); return Page(); } public async Task OnGetComputeRhymeAsync(int id) { var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/diwan/poem/analysisrhyme/{id}"); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } var rhyme = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return new OkObjectResult(rhyme.Rhyme); } public async Task OnPostAsync(DiwanModifyPageViewModel ModifyModel) { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { if(Request.Query["id"] == "0") { var response = await secureClient.PostAsync($"{APIRoot.Url}/api/diwan/page", new StringContent(JsonConvert.SerializeObject(ModifyModel), Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } else { var newPage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return Redirect($"/Admin/ModifyPage?id={newPage.Id}&edit=true"); } } else { var response = await secureClient.PutAsync($"{APIRoot.Url}/api/diwan/page/{Request.Query["id"]}", new StringContent(JsonConvert.SerializeObject(ModifyModel), Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } else { return Redirect($"/Admin/ModifyPage?id={Request.Query["id"]}&edit=true"); } } } else { LastMessage = "لطفاً از دیوان خارج و مجددا به آن وارد شوید."; } } return Page(); } public async Task OnPostGenerateCatPageAsync(DiwanTOC DiwanTOC) { if (!(await PreparePage())) return Page(); var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/diwan/cat?url={PageInformation.FullUrl}&poems=true"); if (!response.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return Page(); } var Cat = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); if (Request.Form["directInsert"].Count == 1) { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { var htmlRes = await secureClient.PutAsync($"{APIRoot.Url}/api/diwan/cat/toc/{Cat.Cat.Id}/{(int)DiwanTOC}", null); if (!htmlRes.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await htmlRes.Content.ReadAsStringAsync()); return Page(); } return Redirect($"/Admin/ModifyPage?id={Request.Query["id"]}&edit=true"); } else { LastMessage = "لطفاً از دیوان خارج و مجددا به آن وارد شوید."; } } return Page(); } else { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { var htmlRes = await secureClient.GetAsync($"{APIRoot.Url}/api/diwan/cat/toc/{Cat.Cat.Id}/{(int)DiwanTOC}"); if (!htmlRes.IsSuccessStatusCode) { LastMessage = JsonConvert.DeserializeObject(await htmlRes.Content.ReadAsStringAsync()); return Page(); } ModifyModel.HtmlText = await htmlRes.Content.ReadAsStringAsync(); LastMessage = $"متن تولیدی دریافت شد. لطفا آن را کپی کنید و سپس اینجا کلیک کنید و آن را درج نمایید."; } else { LastMessage = "لطفاً از دیوان خارج و مجددا به آن وارد شوید."; } } return Page(); } } public async Task OnPostRebuildSitemapAsync() { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/diwan/sitemap", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostRebuildStatsAsync() { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PutAsync($"{APIRoot.Url}/api/diwan/rebuild/stats", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } /// /// (re)generate the public git-tracked data export (poets/categories/poems as JSON, /// pushed to the configured git remote) — runs as a background job, same as the other /// rebuild/regenerate tools on this page; check the Jobs page for progress /// public async Task OnPostRegeneratePublicDataAsync() { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/diwan/publicdata/batchexport", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostRefillSectionsCoupletCountsAsync() { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/diwan/fillsectioncoupletcounts", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostRebuildRelatedPagesAsync() { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PutAsync($"{APIRoot.Url}/api/diwan/quoted/pages/generate", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostRebuildDigitalSourcesStatsAsync() { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/diwan/source/stats/rebuild", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostCleanCacheAsync(int id) { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/diwan/page/cache/{id}"); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostRebuildWordCountsAsync(int poetId) { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/diwan/wordcounts/rebuild/{poetId}", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostImportNaskbanPaperSourcesAsync(int poetId, string username, string password) { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { LoginViewModel model = new LoginViewModel() { Username = username, Password = password, ClientAppName = "DiwanRazor", Language = "ur-PK" }; var stringContent = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"); HttpResponseMessage response = await secureClient.PutAsync($"{APIRoot.Url}/api/diwan/naskban/import/poetbooks/{poetId}", stringContent); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } public async Task OnPostImportMuseumPaperSourcesAsync(int poetId) { using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response))) { if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PutAsync($"{APIRoot.Url}/api/diwan/papersources/import/{poetId}", null); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new OkObjectResult(false); } } }