summaryrefslogtreecommitdiffstats
path: root/api/WhatApi/Endpoints/DownloadContentEndpoint.cs
blob: 34e51e8ea2a31871024a3229e04194586d720429 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
namespace WhatApi.Endpoints;

public class DownloadContentEndpoint : BaseEndpoint
{
    [HttpGet("~/{id:guid}")]
    public async Task<ActionResult> HandleAsync(Guid id, CancellationToken ct = default) {
        try {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "files", id.ToString());
            await using var file = new FileStream(path, FileMode.Open);
            if (!file.CanRead) return NotFound();
            return File(file, "application/octet-stream", id.ToString());
        } catch (Exception e) {
            if (e is not FileNotFoundException) Console.WriteLine(e);
            return NotFound();
        }
    }
}