summary refs log tree commit diff
path: root/ReferenceClientProxyImplementation/Helpers/Resolvers.cs
blob: 813ffcbb285dd04fda0bdb6ea0cd5f618eb84331 (plain) (blame)
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
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;

namespace ReferenceClientProxyImplementation.Helpers;

public static class Resolvers {
    private static readonly string Navbar = File.Exists("Resources/Parts/Navbar.html") ? File.ReadAllText("Resources/Parts/Navbar.html") : "Navbar not found!";

    public static object ReturnFile(string path) {
        if (!File.Exists(path)) return new NotFoundObjectResult("File doesn't exist!");
        var ext = path.Split(".").Last();
        var contentType = ext switch {
            //text types
            "html" => "text/html",
            "js" => "text/javascript",
            "css" => "text/css",
            "txt" => "text/plain",
            "csv" => "text/csv",
            //image types
            "apng" => "image/apng",
            "gif" => "image/gif",
            "jpg" => "image/jpeg",
            "png" => "image/png",
            "svg" => "image/svg+xml",
            "webp" => "image/webp",
            "ico" => "image/x-icon",
            _ => "application/octet-stream"
        };
        switch (ext) {
            case "html":
                return new ContentResult {
                    ContentType = contentType,
                    Content = File.ReadAllText(path)
                };
            case "js":
            case "css":
            case "txt":
            case "csv":
            case "svg":
                return new ContentResult {
                    ContentType = contentType,
                    Content = File.ReadAllText(path)
                };
            case "png":
            case "webp":
            case "jpg":
            case "gif":
            case "apng":
            case "7z":
            case "gz":
            case "tar":
            case "rar":
            case "zip":
            case "webm":
            case "woff":
            case "jar":
            case "mp3":
            case "mp4":
                return new PhysicalFileResult(Path.GetFullPath(path), contentType);
            default:
                Console.WriteLine($"Unsupported filetype: {ext} ({path})");
                return new PhysicalFileResult(Path.GetFullPath(path), "application/octet-stream");
        }
    }

    public static object ReturnFileWithVars(string path, Dictionary<string, object>? customVars = null) {
        if (!File.Exists(path)) return new NotFoundObjectResult(Debugger.IsAttached ? $"File {path} doesn't exist!" : "File doesn't exist!");
        var result = ReturnFile(path);
        if (result.GetType() != typeof(ContentResult)) return result;
        var contentResult = (ContentResult)result;
        contentResult.Content = contentResult.Content?.Replace("$NAVBAR", Navbar);
        if (customVars != null)
            foreach (var (key, value) in customVars)
                contentResult.Content = contentResult.Content?.Replace(key, value.ToString());

        result = contentResult;

        return result;
    }
}