88 lines
2.9 KiB
C#
88 lines
2.9 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using AS1024.NetworkQuality.Server;
|
|
|
|
namespace YourNamespace.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1")]
|
|
public class NetworkQualityController : ControllerBase
|
|
{
|
|
private readonly ILogger<NetworkQualityController> _logger;
|
|
private readonly JsonSerializerOptions _jsonOptions;
|
|
|
|
public NetworkQualityController(ILogger<NetworkQualityController> logger)
|
|
{
|
|
_logger = logger;
|
|
_jsonOptions = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = new SnakeCaseNamingPolicy(), // Use snake_case naming policy
|
|
PropertyNameCaseInsensitive = true // Optional: case insensitive deserialization
|
|
};
|
|
}
|
|
|
|
// GET: /api/v1/config
|
|
[HttpGet("config")]
|
|
public IActionResult GetConfig()
|
|
{
|
|
var config = new ConfigResponse
|
|
{
|
|
Urls = new Urls
|
|
{
|
|
SmallDownloadUrl = Url.Action("Small", "NetworkQuality", null, Request.Scheme),
|
|
LargeDownloadUrl = Url.Action("Large", "NetworkQuality", null, Request.Scheme),
|
|
UploadUrl = Url.Action("Upload", "NetworkQuality", null, Request.Scheme)
|
|
}
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(config, _jsonOptions); // Serialize with snake_case
|
|
return Content(json, "application/json");
|
|
}
|
|
|
|
// GET: /api/v1/small
|
|
[HttpGet("small")]
|
|
public IActionResult Small()
|
|
{
|
|
// Return 1 byte (content is irrelevant)
|
|
Response.ContentType = "application/octet-stream";
|
|
return File(new byte[1], "application/octet-stream");
|
|
}
|
|
|
|
// GET: /api/v1/large
|
|
[HttpGet("large")]
|
|
public IActionResult Large()
|
|
{
|
|
try
|
|
{
|
|
var largeStream = new LargeStreamResult();
|
|
return new FileStreamResult(largeStream, "application/octet-stream");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "An error occurred while writing the large file content.");
|
|
return StatusCode(500, JsonSerializer.Serialize(new { error = "An unexpected error occurred." }, _jsonOptions));
|
|
}
|
|
}
|
|
|
|
// POST: /api/v1/upload
|
|
[HttpPost("upload")]
|
|
public async Task<IActionResult> Upload()
|
|
{
|
|
try
|
|
{
|
|
await Request.Body.CopyToAsync(Stream.Null);
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "An unexpected error occurred.");
|
|
return StatusCode(500, JsonSerializer.Serialize(new { error = "An unexpected error occurred." }, _jsonOptions));
|
|
}
|
|
}
|
|
}
|
|
}
|