2025-08-02 21:45:16 +01:00

236 lines
8.3 KiB
C#

using CatherineLynwood.Models;
using CatherineLynwood.Services;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text;
using System.Xml.Linq;
namespace CatherineLynwood.Controllers
{
public class HomeController : Controller
{
#region Private Fields
private readonly ILogger<HomeController> _logger;
private DataAccess _dataAccess;
private readonly IEmailService _emailService;
#endregion Private Fields
#region Public Constructors
public HomeController(ILogger<HomeController> logger, DataAccess dataAccess, IEmailService emailService)
{
_logger = logger;
_dataAccess = dataAccess;
_emailService = emailService;
}
#endregion Public Constructors
#region Public Methods
[HttpGet("arc-reader-application")]
public IActionResult ArcReaderApplication()
{
ArcReaderApplicationModel arcReaderApplicationModel = new ArcReaderApplicationModel();
return View(arcReaderApplicationModel);
}
[HttpPost("arc-reader-application")]
public async Task<IActionResult> ArcReaderApplication(ArcReaderApplicationModel arcReaderApplicationModel)
{
bool success = await _dataAccess.SaveARCReaderApplication(arcReaderApplicationModel);
if (success)
{
return RedirectToAction("ThankYou");
}
return View(arcReaderApplicationModel);
}
[Route("about-catherine-lynwood")]
public IActionResult AboutCatherineLynwood()
{
return View();
}
[Route("contact-catherine")]
[HttpGet]
public async Task<IActionResult> ContactCatherine()
{
ViewData["VpnWarning"] = HttpContext.Items.ContainsKey("IsVpn") && (bool)HttpContext.Items["IsVpn"];
Contact contact = new Contact();
return View(contact);
}
[Route("contact-catherine")]
[HttpPost]
public async Task<IActionResult> ContactCatherine(Contact contact)
{
if (ModelState.IsValid)
{
await _dataAccess.SaveContact(contact);
var subject = "Email from Catherine Lynwood Web Site";
var plainTextContent = $"Email from: {contact.Name} ({contact.EmailAddress})\r\n{contact.Message}";
var htmlContent = $"<strong>Email from: {contact.Name} ({contact.EmailAddress})</strong><br>{contact.Message}";
await _emailService.SendEmailAsync(subject, plainTextContent, htmlContent, contact);
return RedirectToAction("ThankYou");
}
return View(contact);
}
[Route("feed.xml")]
[Route("rss.xml")]
[Route("rss")]
[Route("feed")]
public async Task<IActionResult> DownloadRssFeed()
{
BlogIndex blogIndex = await _dataAccess.GetBlogsAsync();
XNamespace atom = "http://www.w3.org/2005/Atom"; // Define the Atom namespace
XDocument rss = new XDocument(
new XElement("rss",
new XAttribute("version", "2.0"),
new XAttribute(XNamespace.Xmlns + "atom", atom), // Add Atom namespace
new XElement("channel",
new XElement("title", "Catherine Lynwood Blog"),
new XElement("link", "https://www.catherinelynwood.com/blog"),
new XElement("description", "Latest blog posts from Catherine Lynwood"),
// Add atom:link with rel="self"
new XElement(atom + "link",
new XAttribute("href", "https://www.catherinelynwood.com/feed"),
new XAttribute("rel", "self"),
new XAttribute("type", "application/rss+xml")
),
// Update image link to match channel link
new XElement("image",
new XElement("url", "https://www.catherinelynwood.com/images/jpg/catherine-lynwood-11-400.jpg"),
new XElement("title", "Catherine Lynwood Blog"),
new XElement("link", "https://www.catherinelynwood.com/blog") // Updated to match the channel link
),
from blog in blogIndex.Blogs
select new XElement("item",
new XElement("title", blog.Title),
new XElement("link", $"https://www.catherinelynwood.com/the-alpha-flame/blog/{blog.BlogUrl}"),
// Construct the image URL with "-400.jpg"
new XElement("description", new XCData($"<img src='https://www.catherinelynwood.com/{ConvertToJpgUrl(blog.ImageUrl)}' alt='{blog.ImageAlt}' /> {blog.IndexText}")),
new XElement("pubDate", blog.PublishDate.ToString("R")),
new XElement("guid", $"https://www.catherinelynwood.com/the-alpha-flame/blog/{blog.BlogUrl}"),
// Use the 400px JPEG for the enclosure element
new XElement("enclosure",
new XAttribute("url", $"https://www.catherinelynwood.com/{ConvertToJpgUrl(blog.ImageUrl)}"),
new XAttribute("type", "image/jpeg"),
new XAttribute("length", GetFileSizeInBytes(ConvertToJpgUrl(blog.ImageUrl)))
)
)
)
)
);
byte[] rssBytes = Encoding.UTF8.GetBytes(rss.ToString());
return File(rssBytes, "application/rss+xml", "rss.xml");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[Route("collaboration-inquiry")]
public IActionResult Honeypot()
{
return View();
}
public async Task<IActionResult> Index()
{
//await SendEmail.Execute();
return View();
}
[Route("offline")]
public IActionResult Offline()
{
return View();
}
[Route("privacy")]
public IActionResult Privacy()
{
return View();
}
[Route("samantha-lynwood")]
public IActionResult SamanthaLynwood()
{
return View();
}
[Route("thankyou")]
public IActionResult ThankYou()
{
return View();
}
[Route("verostic-genre")]
public IActionResult VerosticGenre()
{
return View();
}
// Helper method to convert the original image URL to a 400px JPEG URL in the "jpg" folder
private string ConvertToJpgUrl(string originalFileName)
{
// Get the filename without the extension
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(originalFileName);
// Construct the full URL for the 400px JPEG in the "jpg" folder
string jpgUrl = $"images/jpg/{fileNameWithoutExtension}-400.jpg";
return jpgUrl;
}
// Helper method to get the file size in bytes
private long GetFileSizeInBytes(string imageUrl)
{
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", imageUrl.TrimStart('/').Replace("/", "\\"));
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Exists ? fileInfo.Length : 0;
}
#endregion Public Methods
#region Private Methods
private bool IsMobile(HttpRequest request)
{
string userAgent = request.Headers["User-Agent"].ToString().ToLower();
return userAgent.Contains("mobi") ||
userAgent.Contains("android") ||
userAgent.Contains("iphone") ||
userAgent.Contains("ipad");
}
#endregion Private Methods
}
}