-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
72 lines (62 loc) · 2.56 KB
/
Helpers.cs
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
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Xml;
using System.Xml.Linq;
using CBZ_To_Telegraph.ChapterParserAndExtractor;
using CBZ_To_Telegraph.Settings;
using CBZ_To_Telegraph.TelegramBot;
using CBZ_To_Telegraph.UserBot;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace CBZ_To_Telegraph
{
public static class Helpers
{
public static IServiceProvider BuildServiceProvider(IConfiguration configuration)
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IConfiguration>(configuration);
services.AddSingleton<UserBotConfig>();
services.AddSingleton<UserBotWrapper>();
services.AddSingleton<TelegramBotWrapperSettings>(
(provider) => configuration.GetRequiredSection("TelegramBotSettings").Get<TelegramBotWrapperSettings>() ?? throw new InvalidOperationException());
services.AddSingleton<TelegramBotWrapper>();
services.AddSingleton<ArticleUploaderSettings>(provider =>
configuration.GetRequiredSection("ArticleUploaderSettings").Get<ArticleUploaderSettings>() ?? throw new InvalidOperationException());
services.AddSingleton<ArticleUploader>();
services.AddSingleton<MessageWithFileHandler>();
services.AddSingleton<ImageConverter>();
services.AddSingleton<ChapterParser>();
services.AddSingleton<MessageFormatter>();
return services.BuildServiceProvider();
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var memStream = new MemoryStream())
{
using (var w = XmlWriter.Create(memStream))
{
xmlDocument.WriteContentTo(w);
}
memStream.Seek(0, SeekOrigin.Begin);
using (var r = XmlReader.Create(memStream))
{
return XDocument.Load(r);
}
}
}
public static string GetMediaContentType(string pathtofile)
{
var filetype = pathtofile.Split(".").Last() switch
{
"jpg" => "image/jpeg",
"png" => "image/png",
"webp" => "image/webp",
_ => throw new ArgumentException("Not Valid File Type")
};
return filetype;
}
}
}