forked from revenz/Fenrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyController.cs
137 lines (122 loc) · 4.32 KB
/
ProxyController.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Microsoft.AspNetCore.Mvc;
using SixLabors.ImageSharp.Formats.Webp;
namespace Fenrus.Controllers;
/// <summary>
/// Proxy Controller
/// </summary>
[Route("proxy")]
public class ProxyController: BaseController
{
/// <summary>
/// Proxies a resource
/// </summary>
/// <param name="url">the base64 encoded resource</param>
/// <returns>the proxied resource</returns>
[HttpGet("{url}")]
public async Task Get([FromRoute] string url)
{
if (string.IsNullOrEmpty(url))
{
Response.StatusCode = 404;
return;
}
// check if guest is allow and if not, check if user is logged in
var settings = GetUserSettings();
if (settings == null)
{
// need to check global settings since the user is not signed in
var system = GetSystemSettings();
if (system?.AllowGuest != true)
{
// unauthorized
Response.StatusCode = 401;
return;
}
}
// decode the url
url = url.Replace("-", "/");
url = new Helpers.AppHelpers.Utils().base64Decode(url);
if (string.IsNullOrWhiteSpace(url))
{
Response.StatusCode = 404;
return;
}
try
{
var result = await Globals.Client.SendAsync(new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Get
}, new CancellationTokenSource(5000).Token);
int code = (int)result.StatusCode;
Response.StatusCode = code;
if (code >= 200 && code <= 299)
{
Response.Headers.Add("Cache-Control", "public, max-age=" + (31 * 24 * 60 * 60));
}
if(result.Content.Headers.ContentType != null)
Response.Headers.Add("Content-Type", result.Content.Headers.ContentType.MediaType);
using var responseStream = await result.Content.ReadAsStreamAsync().ConfigureAwait(false);
await responseStream.CopyToAsync(Response.Body, 1024).ConfigureAwait(false);// 81920)).ConfigureAwait(false);
}
catch (Exception)
{
Response.StatusCode = 500;
}
}
/// <summary>
/// A proxy to a safe image
/// </summary>
/// <param name="url">the url of the image</param>
/// <returns>the image download</returns>
[HttpGet("safe-image/{url}")]
public async Task SafeImageProxy([FromRoute] string url)
{
if (string.IsNullOrEmpty(url))
{
Response.StatusCode = 404;
return;
}
url = url.Replace("-", "/");
url = new Helpers.AppHelpers.Utils().base64Decode(url);
if (string.IsNullOrWhiteSpace(url))
{
Response.StatusCode = 404;
return;
}
string mimeType = null;
try
{
var result = await Globals.Client.SendAsync(new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Get
}, new CancellationTokenSource(5000).Token);
int code = (int)result.StatusCode;
Response.StatusCode = code;
if (code >= 200 && code <= 299)
{
Response.Headers.Add("Cache-Control", "public, max-age=" + (31 * 24 * 60 * 60));
}
mimeType = result.Content.Headers.ContentType.MediaType;
using var responseStream = await result.Content.ReadAsStreamAsync().ConfigureAwait(false);
using var image = Image.Load(responseStream);
// Resize the image if it's too large
if (image.Width > 800 || image.Height > 800)
{
image.Mutate(x => x.Resize(new ResizeOptions
{
Size = new Size(800, 800),
Mode = ResizeMode.Max
}));
}
Response.Headers.Add("Content-Type", "image/webp");
await image.SaveAsync(Response.Body, new WebpEncoder()).ConfigureAwait(false);
//await responseStream.CopyToAsync(Response.Body, 1024).ConfigureAwait(false);
}
catch (Exception ex)
{
Response.StatusCode = 500;
}
}
}