Use a proper user agent when fetching images
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
@@ -22,12 +23,14 @@ namespace PluralKit.Bot
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly EmbedService _embeds;
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public Groups(IDatabase db, ModelRepository repo, EmbedService embeds)
|
||||
public Groups(IDatabase db, ModelRepository repo, EmbedService embeds, HttpClient client)
|
||||
{
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
_embeds = embeds;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public async Task CreateGroup(Context ctx)
|
||||
@@ -200,7 +203,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
ctx.CheckOwnGroup(target);
|
||||
|
||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
|
||||
|
||||
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url}));
|
||||
|
||||
@@ -262,7 +265,7 @@ namespace PluralKit.Bot
|
||||
{
|
||||
ctx.CheckOwnGroup(target);
|
||||
|
||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
|
||||
|
||||
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {BannerImage = img.Url}));
|
||||
|
||||
|
@@ -22,15 +22,17 @@ namespace PluralKit.Bot
|
||||
public class ImportExport
|
||||
{
|
||||
private readonly DataFileService _dataFiles;
|
||||
private readonly HttpClient _client;
|
||||
private readonly JsonSerializerSettings _settings = new()
|
||||
{
|
||||
// Otherwise it'll mess up/reformat the ISO strings for ???some??? reason >.>
|
||||
DateParseHandling = DateParseHandling.None
|
||||
};
|
||||
|
||||
public ImportExport(DataFileService dataFiles)
|
||||
public ImportExport(DataFileService dataFiles, HttpClient client)
|
||||
{
|
||||
_dataFiles = dataFiles;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public async Task Import(Context ctx)
|
||||
@@ -40,57 +42,54 @@ namespace PluralKit.Bot
|
||||
|
||||
await ctx.BusyIndicator(async () =>
|
||||
{
|
||||
using (var client = new HttpClient())
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await client.GetAsync(url);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Invalid URL throws this, we just error back out
|
||||
throw Errors.InvalidImportFile;
|
||||
}
|
||||
response = await _client.GetAsync(url);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Invalid URL throws this, we just error back out
|
||||
throw Errors.InvalidImportFile;
|
||||
}
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw Errors.InvalidImportFile;
|
||||
if (!response.IsSuccessStatusCode)
|
||||
throw Errors.InvalidImportFile;
|
||||
|
||||
DataFileSystem data;
|
||||
try
|
||||
{
|
||||
var json = JsonConvert.DeserializeObject<JObject>(await response.Content.ReadAsStringAsync(), _settings);
|
||||
data = await LoadSystem(ctx, json);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
throw Errors.InvalidImportFile;
|
||||
}
|
||||
DataFileSystem data;
|
||||
try
|
||||
{
|
||||
var json = JsonConvert.DeserializeObject<JObject>(await response.Content.ReadAsStringAsync(), _settings);
|
||||
data = await LoadSystem(ctx, json);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
throw Errors.InvalidImportFile;
|
||||
}
|
||||
|
||||
if (!data.Valid)
|
||||
throw Errors.InvalidImportFile;
|
||||
if (!data.Valid)
|
||||
throw Errors.InvalidImportFile;
|
||||
|
||||
if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id))
|
||||
{
|
||||
var msg = $"{Emojis.Warn} You seem to importing a system profile belonging to another account. Are you sure you want to proceed?";
|
||||
if (!await ctx.PromptYesNo(msg, "Import")) throw Errors.ImportCancelled;
|
||||
}
|
||||
if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id))
|
||||
{
|
||||
var msg = $"{Emojis.Warn} You seem to importing a system profile belonging to another account. Are you sure you want to proceed?";
|
||||
if (!await ctx.PromptYesNo(msg, "Import")) throw Errors.ImportCancelled;
|
||||
}
|
||||
|
||||
// If passed system is null, it'll create a new one
|
||||
// (and that's okay!)
|
||||
var result = await _dataFiles.ImportSystem(data, ctx.System, ctx.Author.Id);
|
||||
if (!result.Success)
|
||||
await ctx.Reply($"{Emojis.Error} The provided system profile could not be imported. {result.Message}");
|
||||
else if (ctx.System == null)
|
||||
{
|
||||
// We didn't have a system prior to importing, so give them the new system's ID
|
||||
await ctx.Reply($"{Emojis.Success} PluralKit has created a system for you based on the given file. Your system ID is `{result.System.Hid}`. Type `pk;system` for more information.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// We already had a system, so show them what changed
|
||||
await ctx.Reply($"{Emojis.Success} Updated {result.ModifiedNames.Count} members, created {result.AddedNames.Count} members. Type `pk;system list` to check!");
|
||||
}
|
||||
// If passed system is null, it'll create a new one
|
||||
// (and that's okay!)
|
||||
var result = await _dataFiles.ImportSystem(data, ctx.System, ctx.Author.Id);
|
||||
if (!result.Success)
|
||||
await ctx.Reply($"{Emojis.Error} The provided system profile could not be imported. {result.Message}");
|
||||
else if (ctx.System == null)
|
||||
{
|
||||
// We didn't have a system prior to importing, so give them the new system's ID
|
||||
await ctx.Reply($"{Emojis.Success} PluralKit has created a system for you based on the given file. Your system ID is `{result.System.Hid}`. Type `pk;system` for more information.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// We already had a system, so show them what changed
|
||||
await ctx.Reply($"{Emojis.Success} Updated {result.ModifiedNames.Count} members, created {result.AddedNames.Count} members. Type `pk;system list` to check!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -62,7 +62,7 @@ namespace PluralKit.Bot
|
||||
if (avatarArg != null)
|
||||
{
|
||||
try {
|
||||
await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Url);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Url);
|
||||
await _db.Execute(conn => _repo.UpdateMember(conn, member.Id, new MemberPatch { AvatarUrl = avatarArg.Url }));
|
||||
} catch (Exception e) {
|
||||
imageMatchError = e;
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Myriad.Builders;
|
||||
@@ -12,11 +13,13 @@ namespace PluralKit.Bot
|
||||
{
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public MemberAvatar(IDatabase db, ModelRepository repo)
|
||||
public MemberAvatar(IDatabase db, ModelRepository repo, HttpClient client)
|
||||
{
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
private async Task AvatarClear(AvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? mgs)
|
||||
@@ -102,7 +105,7 @@ namespace PluralKit.Bot
|
||||
}
|
||||
|
||||
ctx.CheckSystem().CheckOwnMember(target);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Value.Url);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Value.Url);
|
||||
await UpdateAvatar(location, ctx, target, avatarArg.Value.Url);
|
||||
await PrintResponse(location, ctx, target, avatarArg.Value, guildData);
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
|
||||
using Myriad.Builders;
|
||||
|
||||
@@ -14,11 +15,13 @@ namespace PluralKit.Bot
|
||||
{
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public MemberEdit(IDatabase db, ModelRepository repo)
|
||||
public MemberEdit(IDatabase db, ModelRepository repo, HttpClient client)
|
||||
{
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public async Task Name(Context ctx, PKMember target)
|
||||
@@ -148,7 +151,7 @@ namespace PluralKit.Bot
|
||||
|
||||
async Task SetBannerImage(ParsedImage img)
|
||||
{
|
||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
|
||||
|
||||
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch {BannerImage = img.Url}));
|
||||
|
||||
|
@@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Myriad.Builders;
|
||||
using Myriad.Types;
|
||||
|
||||
using NodaTime;
|
||||
using NodaTime.Text;
|
||||
@@ -18,11 +18,13 @@ namespace PluralKit.Bot
|
||||
{
|
||||
private readonly IDatabase _db;
|
||||
private readonly ModelRepository _repo;
|
||||
private readonly HttpClient _client;
|
||||
|
||||
public SystemEdit(IDatabase db, ModelRepository repo)
|
||||
public SystemEdit(IDatabase db, ModelRepository repo, HttpClient client)
|
||||
{
|
||||
_db = db;
|
||||
_repo = repo;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public async Task Name(Context ctx)
|
||||
@@ -279,7 +281,7 @@ namespace PluralKit.Bot
|
||||
|
||||
async Task SetIcon(ParsedImage img)
|
||||
{
|
||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
|
||||
|
||||
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {AvatarUrl = img.Url}));
|
||||
|
||||
@@ -332,7 +334,7 @@ namespace PluralKit.Bot
|
||||
|
||||
async Task SetImage(ParsedImage img)
|
||||
{
|
||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
|
||||
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
|
||||
|
||||
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {BannerImage = img.Url}));
|
||||
|
||||
|
Reference in New Issue
Block a user