From e88878cf3ebc70ba1ee19ab21547ead3af8ad208 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 6 Sep 2024 17:24:00 +0200 Subject: [PATCH] [backend/api] Add endpoint to get drive files by their hash (ISH-459) --- .../Controllers/Web/DriveController.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Iceshrimp.Backend/Controllers/Web/DriveController.cs b/Iceshrimp.Backend/Controllers/Web/DriveController.cs index 7503e577..99c6d661 100644 --- a/Iceshrimp.Backend/Controllers/Web/DriveController.cs +++ b/Iceshrimp.Backend/Controllers/Web/DriveController.cs @@ -120,6 +120,30 @@ public class DriveController( }; } + [HttpGet("/by-hash/{sha256}")] + [Authenticate] + [Authorize] + [Produces(MediaTypeNames.Application.Json)] + [ProducesResults(HttpStatusCode.OK)] + [ProducesErrors(HttpStatusCode.NotFound)] + public async Task GetFileByHash(string sha256) + { + var user = HttpContext.GetUserOrFail(); + var file = await db.DriveFiles.FirstOrDefaultAsync(p => p.User == user && p.Sha256 == sha256) ?? + throw GracefulException.NotFound("File not found"); + + return new DriveFileResponse + { + Id = file.Id, + Url = file.AccessUrl, + ThumbnailUrl = file.ThumbnailAccessUrl, + Filename = file.Name, + ContentType = file.Type, + Description = file.Comment, + Sensitive = file.IsSensitive + }; + } + [HttpPatch("{id}")] [Authenticate] [Authorize]