forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageCache.cs
69 lines (59 loc) · 2.48 KB
/
ImageCache.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
using System;
using System.IO;
using System.Net;
using System.Linq;
namespace VRCX
{
class ImageCache
{
private static readonly string cacheLocation = Path.Combine(Program.AppDataDirectory, "ImageCache");
public static string GetImage(string url, string fileId, string version, string appVersion)
{
var imageHost = "api.vrchat.cloud";
var imageHost1 = "files.vrchat.cloud";
var imageHost2 = "d348imysud55la.cloudfront.net";
var directoryLocation = Path.Combine(cacheLocation, fileId);
var fileLocation = Path.Combine(directoryLocation, $"{version}.png");
if (File.Exists(fileLocation))
{
Directory.SetLastWriteTime(directoryLocation, DateTime.Now);
return fileLocation;
}
if (Directory.Exists(directoryLocation))
Directory.Delete(directoryLocation, true);
Directory.CreateDirectory(directoryLocation);
Uri uri = new Uri(url);
if (uri.Host != imageHost && uri.Host != imageHost1 && uri.Host != imageHost2)
throw new ArgumentException("Invalid image host", url);
using (var client = new WebClient())
{
string cookieString = String.Empty;
if (WebApi.Instance != null && WebApi.Instance._cookieContainer != null)
{
CookieCollection cookies = WebApi.Instance._cookieContainer.GetCookies(new Uri($"https://{imageHost}"));
foreach (Cookie cookie in cookies)
cookieString += $"{cookie.Name}={cookie.Value};";
}
client.Headers.Add(HttpRequestHeader.Cookie, cookieString);
client.Headers.Add("user-agent", appVersion);
client.DownloadFile(url, fileLocation);
}
int cacheSize = Directory.GetDirectories(cacheLocation).Length;
if (cacheSize > 1100)
CleanImageCache();
return fileLocation;
}
private static void CleanImageCache()
{
DirectoryInfo dirInfo = new DirectoryInfo(cacheLocation);
var folders = dirInfo.GetDirectories().OrderBy(p => p.LastWriteTime);
int i = 0;
foreach (DirectoryInfo folder in folders.Reverse())
{
i++;
if (i > 1000)
folder.Delete(true);
}
}
}
}