summaryrefslogtreecommitdiffstats
path: root/src/server/Api/Internal/GetSiteReportRoute.cs
blob: 58d66374f09d56bf377141c9597f42ce4acacfb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using HtmlAgilityPack;
using IOL.BookmarkThing.Server.Api.Internal.Dtos;

namespace IOL.BookmarkThing.Server.Api.Internal;

public class GetSiteReportRoute : RouteBaseInternalAsync.WithRequest<GetSiteReportRequest>.WithActionResult
{
	private readonly HttpClient _client;
	private readonly ILogger<GetSiteReportRequest> _logger;
	private readonly AppDbContext _context;

	public GetSiteReportRoute(HttpClient client, ILogger<GetSiteReportRequest> logger, AppDbContext context) {
		_client = client;
		_logger = logger;
		_context = context;
	}

	[ApiVersionNeutral]
	[ApiExplorerSettings(IgnoreApi = true)]
	[HttpPost("~/v{version:apiVersion}/site-report")]
	public override async Task<ActionResult> HandleAsync(GetSiteReportRequest request, CancellationToken cancellationToken = default) {
		var report = new SiteReportDto();
		var exists = _context.Entries.Any(c => c.Url == request.Url && c.UserId == LoggedInUser.Id);
		if (exists) {
			report.Duplicate = true;
			return Ok(report);
		}

		try {
			var http_request = await _client.GetAsync(request.Url, cancellationToken);
			if (http_request.IsSuccessStatusCode) {
				try {
					var document = new HtmlDocument();
					document.Load(await http_request.Content.ReadAsStreamAsync(cancellationToken));
					var htmlNodes = document.DocumentNode.Descendants("meta")
											.Where(p => p.GetAttributeValue("name", string.Empty).Equals("description", StringComparison.InvariantCultureIgnoreCase));
					report.Description = htmlNodes.FirstOrDefault()?.GetAttributeValue("content", string.Empty);
				} catch (Exception e) {
					_logger.LogWarning(e, "An error occured when parsing site for site report");
				}
			} else {
				report.Unreachable = true;
			}
		} catch (Exception e) {
			if (e is HttpRequestException) {
				report.Unreachable = true;
			}

			_logger.LogError(e, "An error occured when getting external site for site report");
		}

		return Ok(report);
	}
}