mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-02 20:34:07 +02:00
37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
using System;
|
|
using System.Net;
|
|
using CefSharp;
|
|
using TweetLib.Core.Browser;
|
|
using TweetLib.Utils.Static;
|
|
|
|
namespace TweetDuck.Resources {
|
|
public class ResourceSchemeFactory : ISchemeHandlerFactory {
|
|
public const string Name = "td";
|
|
|
|
private readonly IResourceProvider<IResourceHandler> resourceProvider;
|
|
|
|
public ResourceSchemeFactory(IResourceProvider<IResourceHandler> resourceProvider) {
|
|
this.resourceProvider = resourceProvider;
|
|
}
|
|
|
|
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request) {
|
|
if (!Uri.TryCreate(request.Url, UriKind.Absolute, out var uri) || uri.Scheme != Name) {
|
|
return null;
|
|
}
|
|
|
|
string rootPath = uri.Authority switch {
|
|
"resources" => Program.ResourcesPath,
|
|
"guide" => Program.GuidePath,
|
|
_ => null
|
|
};
|
|
|
|
if (rootPath == null) {
|
|
return resourceProvider.Status(HttpStatusCode.NotFound, "Invalid URL.");
|
|
}
|
|
|
|
string filePath = FileUtils.ResolveRelativePathSafely(rootPath, uri.AbsolutePath.TrimStart('/'));
|
|
return filePath.Length == 0 ? resourceProvider.Status(HttpStatusCode.Forbidden, "File path has to be relative to the root folder.") : resourceProvider.File(filePath);
|
|
}
|
|
}
|
|
}
|