mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-19 23:03:51 +02:00
Image cache fixes
This commit is contained in:
@@ -14,6 +14,7 @@ using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using CefSharp;
|
||||
using librsync.net;
|
||||
@@ -154,9 +155,9 @@ namespace VRCX
|
||||
/// <param name="fileId">The ID of the file associated with the image.</param>
|
||||
/// <param name="version">The version of the file associated with the image.</param>
|
||||
/// <returns>A string representing the file location of the cached image.</returns>
|
||||
public string GetImage(string url, string fileId, string version)
|
||||
public async Task<string> GetImage(string url, string fileId, string version)
|
||||
{
|
||||
return ImageCache.GetImage(url, fileId, version);
|
||||
return await ImageCache.GetImage(url, fileId, version);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -59,6 +59,7 @@ namespace VRCX
|
||||
|
||||
//CefSharpSettings.WcfEnabled = true; // TOOD: REMOVE THIS LINE YO (needed for synchronous configRepository)
|
||||
CefSharpSettings.ShutdownOnExit = false;
|
||||
CefSharpSettings.ConcurrentTaskExecution = true;
|
||||
|
||||
if (Cef.Initialize(cefSettings, false) == false)
|
||||
{
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VRCX
|
||||
{
|
||||
class ImageCache
|
||||
{
|
||||
private static readonly string cacheLocation = Path.Combine(Program.AppDataDirectory, "ImageCache");
|
||||
private static readonly WebClient webClient = new WebClient();
|
||||
private static readonly HttpClient httpClient = new HttpClient();
|
||||
private static readonly List<string> _imageHosts =
|
||||
[
|
||||
"api.vrchat.cloud",
|
||||
"files.vrchat.cloud",
|
||||
"d348imysud55la.cloudfront.net",
|
||||
"assets.vrchat.com"
|
||||
];
|
||||
|
||||
private const string IMAGE_HOST1 = "api.vrchat.cloud";
|
||||
private const string IMAGE_HOST2 = "files.vrchat.cloud";
|
||||
private const string IMAGE_HOST3 = "d348imysud55la.cloudfront.net";
|
||||
|
||||
public static string GetImage(string url, string fileId, string version)
|
||||
public static async Task<string> GetImage(string url, string fileId, string version)
|
||||
{
|
||||
var directoryLocation = Path.Combine(cacheLocation, fileId);
|
||||
var fileLocation = Path.Combine(directoryLocation, $"{version}.png");
|
||||
@@ -29,23 +35,30 @@ namespace VRCX
|
||||
Directory.Delete(directoryLocation, true);
|
||||
Directory.CreateDirectory(directoryLocation);
|
||||
|
||||
Uri uri = new Uri(url);
|
||||
if (uri.Host != IMAGE_HOST1 && uri.Host != IMAGE_HOST2 && uri.Host != IMAGE_HOST3)
|
||||
var uri = new Uri(url);
|
||||
if (!_imageHosts.Contains(uri.Host))
|
||||
throw new ArgumentException("Invalid image host", url);
|
||||
|
||||
string cookieString = string.Empty;
|
||||
var cookieString = string.Empty;
|
||||
if (WebApi.Instance != null && WebApi.Instance._cookieContainer != null)
|
||||
{
|
||||
CookieCollection cookies = WebApi.Instance._cookieContainer.GetCookies(new Uri($"https://{IMAGE_HOST1}"));
|
||||
CookieCollection cookies = WebApi.Instance._cookieContainer.GetCookies(new Uri("https://api.vrchat.cloud"));
|
||||
foreach (Cookie cookie in cookies)
|
||||
cookieString += $"{cookie.Name}={cookie.Value};";
|
||||
}
|
||||
|
||||
webClient.Headers[HttpRequestHeader.Cookie] = cookieString;
|
||||
webClient.Headers[HttpRequestHeader.UserAgent] = Program.Version;
|
||||
webClient.DownloadFile(url, fileLocation);
|
||||
httpClient.DefaultRequestHeaders.Add("Cookie", cookieString);
|
||||
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(Program.Version);
|
||||
using (var response = await httpClient.GetAsync(url))
|
||||
{
|
||||
response.EnsureSuccessStatusCode();
|
||||
await using (var fileStream = new FileStream(fileLocation, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
await response.Content.CopyToAsync(fileStream);
|
||||
}
|
||||
}
|
||||
|
||||
int cacheSize = Directory.GetDirectories(cacheLocation).Length;
|
||||
var cacheSize = Directory.GetDirectories(cacheLocation).Length;
|
||||
if (cacheSize > 1100)
|
||||
CleanImageCache();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user