< Summary

Information
Class: Services.LabelServices.LabelFileStorage
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/LabelFileStorage.cs
Line coverage
92%
Covered lines: 35
Uncovered lines: 3
Coverable lines: 38
Total lines: 79
Line coverage: 92.1%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
Path(...)100%11100%
ExistsAsync(...)100%11100%
FindExistingFileNameAsync()75%4483.33%
TryDownloadAsync()100%2288.88%
OpenReadAsync(...)100%210%
UploadAsync()100%22100%
ListAsync(...)100%11100%
DeleteAsync()100%22100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/LabelFileStorage.cs

#LineLine coverage
 1using Microsoft.Extensions.Options;
 2using Services.FileStorageServices;
 3
 4namespace Services.LabelServices;
 5
 6public sealed class LabelFileStorage : ILabelFileStorage
 7{
 8    private readonly IServerFileStorage _storage;
 9    private readonly string _folder;
 10    private readonly ILabelCatalogCache? _catalogCache;
 11
 612    public LabelFileStorage(
 613        IServerFileStorage storage,
 614        IOptions<LabelStorageOptions> options,
 615        ILabelCatalogCache? catalogCache = null)
 16    {
 617        _storage = storage;
 618        _catalogCache = catalogCache;
 619        _folder = string.IsNullOrWhiteSpace(options.Value.RelativeFolder)
 620            ? "labels"
 621            : options.Value.RelativeFolder.Trim('/');
 622    }
 23
 1624    private string Path(string fileName) => $"{_folder}/{fileName}";
 25
 26    public Task<bool> ExistsAsync(string fileName, CancellationToken cancellationToken = default) =>
 427        _storage.ExistsAsync(Path(fileName), cancellationToken);
 28
 29    public async Task<string?> FindExistingFileNameAsync(
 30        IEnumerable<string> candidateFileNames, CancellationToken cancellationToken = default)
 31    {
 532        foreach (var fileName in candidateFileNames)
 33        {
 234            if (await ExistsAsync(fileName, cancellationToken))
 35            {
 136                return fileName;
 37            }
 138        }
 39
 040        return null;
 141    }
 42
 43    public async Task<byte[]?> TryDownloadAsync(string fileName, CancellationToken cancellationToken = default)
 44    {
 45        try
 46        {
 347            var download = await _storage.OpenReadAsync(Path(fileName), cancellationToken);
 248            await using var stream = download.Content;
 249            using var memoryStream = new MemoryStream();
 250            await stream.CopyToAsync(memoryStream, cancellationToken);
 251            return memoryStream.ToArray();
 052        }
 153        catch (FileNotFoundException)
 54        {
 155            return null;
 56        }
 357    }
 58
 59    public Task<ServerFileDownload> OpenReadAsync(string fileName, CancellationToken cancellationToken = default) =>
 060        _storage.OpenReadAsync(Path(fileName), cancellationToken);
 61
 62    public async Task UploadAsync(byte[] content, string fileName, bool overwrite = true, CancellationToken cancellation
 63    {
 764        using var stream = new MemoryStream(content);
 765        await _storage.UploadAsync(
 766            new ServerFileUpload(Path(fileName), stream, "application/pdf", overwrite),
 767            cancellationToken);
 768        _catalogCache?.Invalidate();
 769    }
 70
 71    public Task<IReadOnlyList<ServerFileInfo>> ListAsync(CancellationToken cancellationToken = default) =>
 172        _storage.ListAsync(_folder, cancellationToken);
 73
 74    public async Task DeleteAsync(string fileName, CancellationToken cancellationToken = default)
 75    {
 276        await _storage.DeleteAsync(Path(fileName), cancellationToken);
 277        _catalogCache?.Invalidate();
 278    }
 79}