divan/DivanRazor/Areas/Admin/Pages/PublicDataImport.cshtml.cs
Anas Rashid 532d005cdb Rename Ganjoor -> Divan throughout the code
Projects (GanjooRazor -> DivanRazor, GanjoorService.sln -> DivanService.sln), files,
folders, classes, namespaces, settings, API routes (/api/divan) and DB tables (Divan*).
External addresses (ganjoor.net, github.com/ganjoor) unchanged. Migrations renamed
consistently (no pending model changes); requires a fresh database.
Upstream RUNNING_LOCALLY/SEMANTIC_SEARCH docs archived unmodified under docs/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 23:49:01 +02:00

76 lines
2.7 KiB
C#

using DivanRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace DivanRazor.Areas.Admin.Pages
{
/// <summary>
/// (Re)build local Divan content from the public data export — local git clone or a URL
/// (e.g. jsDelivr) — mainly meant to make setting up a fork or local dev copy easier than the
/// old per-poet SQLite import.
/// </summary>
public class PublicDataImportModel : PageModel
{
/// <summary>
/// last message
/// </summary>
public string LastMessage { get; set; }
public IActionResult OnGet()
{
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
return Redirect("/");
LastMessage = "";
return Page();
}
/// <summary>
/// trigger the import job (runs in the background — check the Jobs page for progress)
/// </summary>
/// <param name="useHttp">true: location is a URL fetched over HTTP. false: location is a local folder path.</param>
/// <param name="location">base URL or local folder path of the exported data tree</param>
/// <param name="poetId">0 imports every poet; a specific poet id imports only that poet</param>
public async Task<IActionResult> OnPostImportAsync(bool useHttp, string location, int poetId)
{
if (string.IsNullOrWhiteSpace(location))
{
return BadRequest("مسیر یا نشانی نمی‌تواند خالی باشد.");
}
using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response)))
{
if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response))
{
var body = new
{
useHttp,
location,
poetId
};
var response = await secureClient.PostAsync
(
$"{APIRoot.Url}/api/divan/publicdata/import",
new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json")
);
if (!response.IsSuccessStatusCode)
{
return BadRequest(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
return new OkObjectResult(true);
}
}
return new OkObjectResult(false);
}
}
}