< Summary

Information
Class: Api.Files.Endpoints.UploadFileEndpoint
Assembly: Api
File(s): /home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Files/Endpoints/UploadFileEndpoint.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 64
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UploadFileAsync()100%44100%

File(s)

/home/runner/work/ProjectRead.ing/ProjectRead.ing/Api/Files/Endpoints/UploadFileEndpoint.cs

#LineLine coverage
 1// SPDX-FileCopyrightText: 2026 Alper Çelik <alper@alper-celik.dev>
 2//
 3// SPDX-License-Identifier: AGPL-3.0-or-later
 4
 5// Mostly Ai Generated - Start
 6using Api.Auth.Handlers;
 7using Api.Auth.Models;
 8using Api.Auth.Utils;
 9using Api.Database;
 10using Api.Database.Utils;
 11using Api.Files.FileProviders;
 12using Microsoft.AspNetCore.Http.HttpResults;
 13using Microsoft.AspNetCore.Mvc;
 14using Microsoft.EntityFrameworkCore;
 15
 16namespace Api.Files.Endpoints;
 17
 18public static class UploadFileEndpoint
 19{
 20    [PermissionCheckAuthorize(UserPermissionBits.FileWrite)]
 21    [RequestFormLimits(MultipartBodyLengthLimit = 5L * 1024 * 1024 * 1024)]
 22    public static async Task<
 23        Results<NoContent, NotFound, Conflict, UnprocessableEntity<string>>
 24    > UploadFileAsync(
 25        Guid id,
 26        IFormFile file,
 27        [FromServices] PGContext db,
 28        [FromServices] ICurrentUserId userId,
 29        [FromServices] IEFTransactionDIAccessorService txGetter,
 30        [FromServices] UserFileRouter router,
 31        CancellationToken ct
 32    )
 133    {
 134        var ownerId = userId.Id!.Value;
 35
 136        var uploaded = await db
 137            .FileRecords.Where(f => f.OwnerId == ownerId && f.Id == id)
 138            .Select(f => (bool?)f.Uploaded)
 139            .FirstOrDefaultAsync(ct);
 40
 141        if (uploaded is null)
 142        {
 143            return TypedResults.NotFound();
 44        }
 45
 146        if (uploaded.Value)
 147        {
 148            return TypedResults.Conflict();
 49        }
 50
 151        var tx = await txGetter.BeginOrGetTransactionAsync(ct);
 52
 153        if (await router.SetFileAsync(ownerId, id, file.OpenReadStream(), ct) is null)
 154        {
 155            return TypedResults.UnprocessableEntity(
 156                "File content did not match the declared size or SHA256"
 157            );
 58        }
 59
 160        await tx.CommitAsync(ct);
 161        return TypedResults.NoContent();
 162    }
 63}
 64// Mostly Ai Generated - End

Methods/Properties

UploadFileAsync()