1
0
voyager-api/ScrapperAPI/Controllers/ContentController.cs

66 lines
2.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using ScrapperAPI.Interfaces;
using ScrapperAPI.Utils;
namespace ScrapperAPI.Controllers;
[ApiController]
public sealed class ContentController : ControllerBase
{
private readonly IContentRepository _content;
public ContentController(IContentRepository content)
{
_content = content;
}
// ✅ Retorna HTML DESCOMPRIMIDO
// GET /queue/{queueId}/content
[HttpGet("queue/{queueId:int}/content")]
public async Task<IActionResult> GetDecompressedHtml(int queueId, CancellationToken ct)
{
var row = await _content.GetCompressedByQueueIdAsync(queueId, ct);
if (row is null || row.ContentBytes is null || row.ContentBytes.Length == 0)
return NotFound(new { message = "Content not found for this queueId." });
if (!string.Equals(row.ContentEncoding, "gzip", StringComparison.OrdinalIgnoreCase))
return StatusCode(415, new { message = $"Unsupported encoding: {row.ContentEncoding}" });
string html;
try
{
html = CompressionUtils.GzipDecompressUtf8(row.ContentBytes);
}
catch (Exception ex)
{
// Se o payload estiver corrompido/errado
return StatusCode(500, new { message = "Failed to decompress content.", error = ex.Message });
}
// Headers úteis pra debug
Response.Headers["X-Content-Id"] = row.Id.ToString();
Response.Headers["X-Queue-Id"] = row.QueueId.ToString();
Response.Headers["X-Content-Encoding"] = row.ContentEncoding;
if (row.OriginalLength is not null) Response.Headers["X-Original-Length"] = row.OriginalLength.Value.ToString();
if (row.CompressedLength is not null) Response.Headers["X-Compressed-Length"] = row.CompressedLength.Value.ToString();
// Retorna como HTML (o browser / front consegue renderizar se quiser)
return Content(html, "text/html; charset=utf-8");
}
// (Opcional) debug: retorna descomprimido como texto
// GET /queue/{queueId}/content/raw
[HttpGet("queue/{queueId:int}/content/raw")]
public async Task<IActionResult> GetDecompressedRaw(int queueId, CancellationToken ct)
{
var row = await _content.GetCompressedByQueueIdAsync(queueId, ct);
if (row is null || row.ContentBytes is null || row.ContentBytes.Length == 0)
return NotFound(new { message = "Content not found for this queueId." });
if (!string.Equals(row.ContentEncoding, "gzip", StringComparison.OrdinalIgnoreCase))
return StatusCode(415, new { message = $"Unsupported encoding: {row.ContentEncoding}" });
var text = CompressionUtils.GzipDecompressUtf8(row.ContentBytes);
return Content(text, "text/plain; charset=utf-8");
}
}