summaryrefslogtreecommitdiffstats
path: root/api/WhatApi/Endpoints/UploadContentEndpoint.cs
diff options
context:
space:
mode:
authorivar <i@oiee.no>2025-10-20 00:26:34 +0200
committerivar <i@oiee.no>2025-10-20 00:26:34 +0200
commita1f0518d0cd123a791adde64f4f11bd8e44276c7 (patch)
tree675a7dff8262eea877ec800ff1efe9b92f5d7e7d /api/WhatApi/Endpoints/UploadContentEndpoint.cs
downloadwhat-a1f0518d0cd123a791adde64f4f11bd8e44276c7.tar.xz
what-a1f0518d0cd123a791adde64f4f11bd8e44276c7.zip
Initial commit
Diffstat (limited to 'api/WhatApi/Endpoints/UploadContentEndpoint.cs')
-rw-r--r--api/WhatApi/Endpoints/UploadContentEndpoint.cs49
1 files changed, 49 insertions, 0 deletions
diff --git a/api/WhatApi/Endpoints/UploadContentEndpoint.cs b/api/WhatApi/Endpoints/UploadContentEndpoint.cs
new file mode 100644
index 0000000..82fb71b
--- /dev/null
+++ b/api/WhatApi/Endpoints/UploadContentEndpoint.cs
@@ -0,0 +1,49 @@
+using Microsoft.AspNetCore.Http.Extensions;
+using Microsoft.AspNetCore.Mvc;
+using NetTopologySuite;
+using NetTopologySuite.Geometries;
+
+namespace WhatApi.Endpoints;
+
+public class UploadContentEndpoint(Database db) : BaseEndpoint
+{
+ public record UploadContent(IFormFile File, string LatLong);
+
+ [HttpPost("~/upload")]
+ public async Task<ActionResult> HandleAsync([FromForm] UploadContent request, CancellationToken ct = default) {
+ if (string.IsNullOrWhiteSpace(Request.GetMultipartBoundary())) {
+ return StatusCode(415, "Unsupported Media Type");
+ }
+
+ var blobId = Guid.NewGuid();
+ var contentId = Guid.NewGuid();
+
+ var latitude = request.LatLong.Split(',')[0];
+ var longitude = request.LatLong.Split(',')[1];
+
+ var gf = NtsGeometryServices.Instance.CreateGeometryFactory(srid: Constants.Wgs84SpatialReferenceId);
+ var point = gf.CreatePoint(new Coordinate(double.Parse(longitude), double.Parse(latitude)));
+
+ var place = new Tables.Place() {
+ ContentId = contentId,
+ Location = point
+ };
+
+ var content = new Tables.Content() {
+ Id = contentId,
+ Mime = request.File.ContentType,
+ Created = DateTime.UtcNow,
+ BlobId = blobId,
+ Ip = GetIp()
+ };
+
+ var path = Path.Combine(Directory.GetCurrentDirectory(), "files", blobId.ToString());
+
+ await using var writer = new FileStream(path, FileMode.CreateNew);
+ await request.File.CopyToAsync(writer, ct);
+ await db.Content.AddAsync(content, ct);
+ await db.Places.AddAsync(place, ct);
+ await db.SaveChangesAsync(ct);
+ return Ok(contentId);
+ }
+} \ No newline at end of file