~ruther/NosSmooth

ref: fcd0751c2f76ffee45134260ce9d7c0bddf837d2 NosSmooth/Data/NosSmooth.Data.NOSFiles/Readers/FileReader.cs -rw-r--r-- 2.8 KiB
fcd0751c — František Boháček Merge pull request #22 from Rutherther/data 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
//  FileReader.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.InteropServices;
using NosSmooth.Data.NOSFiles.Errors;
using NosSmooth.Data.NOSFiles.Files;
using Remora.Results;

namespace NosSmooth.Data.NOSFiles.Readers;

/// <summary>
/// Reader of files.
/// </summary>
public class FileReader
{
    private readonly IReadOnlyList<IFileTypeReader> _typeReaders;

    /// <summary>
    /// Initializes a new instance of the <see cref="FileReader"/> class.
    /// </summary>
    /// <param name="typeReaders">The readers of specific types.</param>
    public FileReader(IEnumerable<IFileTypeReader> typeReaders)
    {
        _typeReaders = typeReaders.ToArray();
    }

    /// <summary>
    /// Get a file type reader for the given file.
    /// </summary>
    /// <param name="file">The file.</param>
    /// <returns>A type reader or an error.</returns>
    public Result<IFileTypeReader> GetFileTypeReader(RawFile file)
    {
        foreach (var typeReader in _typeReaders)
        {
            if (typeReader.SupportsFile(file))
            {
                return Result<IFileTypeReader>.FromSuccess(typeReader);
            }
        }

        return new UnknownFileTypeError(file);
    }

    /// <summary>
    /// Read the given file.
    /// </summary>
    /// <param name="file">The raw file.</param>
    /// <typeparam name="TContent">The type of the content to assume.</typeparam>
    /// <returns>The content or an error.</returns>
    public Result<ReadFile<TContent>> Read<TContent>(RawFile file)
    {
        var fileReaderResult = GetFileTypeReader(file);
        if (!fileReaderResult.IsSuccess)
        {
            return Result<ReadFile<TContent>>.FromError(fileReaderResult);
        }

        var fileReader = fileReaderResult.Entity;
        var readResult = fileReader.Read(file);
        if (!readResult.IsSuccess)
        {
            return Result<ReadFile<TContent>>.FromError(readResult);
        }

        try
        {
            var content = (ReadFile<TContent>)readResult.Entity;
            return content;
        }
        catch (Exception e)
        {
            return e;
        }
    }

    /// <summary>
    /// Read a file from a filesystem path.
    /// </summary>
    /// <param name="path">The path to the file.</param>
    /// <typeparam name="TContent">The type of the content to assume.</typeparam>
    /// <returns>The content or an error.</returns>
    public Result<ReadFile<TContent>> ReadFileSystemFile<TContent>(string path)
    {
        try
        {
            var readData = File.ReadAllBytes(path);
            return Read<TContent>(new RawFile(null, path, readData.Length, readData));
        }
        catch (Exception e)
        {
            return e;
        }
    }
}
Do not follow this link