mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-18 22:33:50 +02:00
Fix cookie bug
This commit is contained in:
@@ -5,11 +5,13 @@ using System.Net;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
|
||||
namespace VRCX;
|
||||
|
||||
internal static class ImageCache
|
||||
{
|
||||
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
||||
private static readonly string cacheLocation;
|
||||
private static readonly HttpClient httpClient;
|
||||
private static readonly List<string> ImageHosts =
|
||||
@@ -47,6 +49,31 @@ internal static class ImageCache
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<Stream> FetchImage(string url)
|
||||
{
|
||||
var uri = new Uri(url);
|
||||
if (!ImageHosts.Contains(uri.Host))
|
||||
throw new ArgumentException("Invalid image host", url);
|
||||
|
||||
var cookieString = string.Empty;
|
||||
if (WebApi.Instance != null &&
|
||||
WebApi.Instance._cookieContainer != null &&
|
||||
uri.Host == "api.vrchat.cloud")
|
||||
{
|
||||
CookieCollection cookies = WebApi.Instance._cookieContainer.GetCookies(new Uri("https://api.vrchat.cloud"));
|
||||
foreach (Cookie cookie in cookies)
|
||||
cookieString += $"{cookie.Name}={cookie.Value};";
|
||||
}
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
if (!string.IsNullOrEmpty(cookieString))
|
||||
request.Headers.Add("Cookie", cookieString);
|
||||
|
||||
using var response = await httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
return await response.Content.ReadAsStreamAsync();
|
||||
}
|
||||
|
||||
public static async Task<string> GetImage(string url, string fileId, string version)
|
||||
{
|
||||
var directoryLocation = Path.Join(cacheLocation, fileId);
|
||||
@@ -62,29 +89,17 @@ internal static class ImageCache
|
||||
Directory.Delete(directoryLocation, true);
|
||||
Directory.CreateDirectory(directoryLocation);
|
||||
|
||||
var uri = new Uri(url);
|
||||
if (!ImageHosts.Contains(uri.Host))
|
||||
throw new ArgumentException("Invalid image host", url);
|
||||
|
||||
var cookieString = string.Empty;
|
||||
if (WebApi.Instance != null && WebApi.Instance._cookieContainer != null)
|
||||
try
|
||||
{
|
||||
CookieCollection cookies = WebApi.Instance._cookieContainer.GetCookies(new Uri("https://api.vrchat.cloud"));
|
||||
foreach (Cookie cookie in cookies)
|
||||
cookieString += $"{cookie.Name}={cookie.Value};";
|
||||
var stream = await FetchImage(url);
|
||||
await using var fileStream =
|
||||
new FileStream(fileLocation, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
await stream.CopyToAsync(fileStream);
|
||||
}
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
if (!string.IsNullOrEmpty(cookieString))
|
||||
request.Headers.Add("Cookie", cookieString);
|
||||
|
||||
using (var response = await httpClient.SendAsync(request))
|
||||
catch (Exception ex)
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
await using (var fileStream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
await response.Content.CopyToAsync(fileStream);
|
||||
}
|
||||
logger.Error(ex, "Failed to fetch image");
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var cacheSize = Directory.GetDirectories(cacheLocation).Length;
|
||||
@@ -94,6 +109,13 @@ internal static class ImageCache
|
||||
return fileLocation;
|
||||
}
|
||||
|
||||
public static async Task SaveImageToFile(string url, string path)
|
||||
{
|
||||
var stream = await FetchImage(url);
|
||||
await using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
await stream.CopyToAsync(fileStream);
|
||||
}
|
||||
|
||||
private static void CleanImageCache()
|
||||
{
|
||||
var dirInfo = new DirectoryInfo(cacheLocation);
|
||||
@@ -103,31 +125,4 @@ internal static class ImageCache
|
||||
folder.Delete(true);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<bool> SaveImageToFile(string url, string path)
|
||||
{
|
||||
var uri = new Uri(url);
|
||||
if (!ImageHosts.Contains(uri.Host))
|
||||
throw new ArgumentException("Invalid image host", url);
|
||||
|
||||
var cookieString = string.Empty;
|
||||
if (WebApi.Instance != null && WebApi.Instance._cookieContainer != null)
|
||||
{
|
||||
var cookies = WebApi.Instance._cookieContainer.GetCookies(new Uri("https://api.vrchat.cloud"));
|
||||
foreach (Cookie cookie in cookies)
|
||||
cookieString += $"{cookie.Name}={cookie.Value};";
|
||||
}
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
if (!string.IsNullOrEmpty(cookieString))
|
||||
request.Headers.Add("Cookie", cookieString);
|
||||
|
||||
using var response = await httpClient.SendAsync(request);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return false;
|
||||
|
||||
await using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
await response.Content.CopyToAsync(fileStream);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user