feat(server): lossy avatar thumbnails, gzip upload and separate avatar endpoints
This commit is contained in:
1 parent
ba0b6c258a
commit
31ae3bc39e
16 files changed
+155
-26
No files matched your search
@@ -10,14 +10,20 @@ public static class AvatarData
|
||||
/// <summary>空间头像会跟着空间信息发给所有成员和空间列表,动图上限比账号头像小,约 1.5 MB</summary>
|
||||
public const int MaxSpaceGifChars = 2_000_000;
|
||||
|
||||
/// <summary>缩略图(列表、聊天里默认分发的有损小图)的上限:静态约 75 KB,动图约 370 KB</summary>
|
||||
public const int MaxThumbStaticChars = 100_000;
|
||||
public const int MaxThumbGifChars = 500_000;
|
||||
|
||||
private static readonly string[] Prefixes = ["data:image/png;base64,", "data:image/jpeg;base64,", "data:image/webp;base64,", "data:image/gif;base64,"];
|
||||
|
||||
public static void ValidateThumb(string thumb) => Validate(thumb, MaxThumbGifChars, MaxThumbStaticChars);
|
||||
|
||||
/// <summary>不合法时抛 BadRequest</summary>
|
||||
public static void Validate(string avatar, int maxGifChars)
|
||||
public static void Validate(string avatar, int maxGifChars, int maxStaticChars = MaxStaticChars)
|
||||
{
|
||||
var prefix = Prefixes.FirstOrDefault(p => avatar.StartsWith(p, StringComparison.Ordinal));
|
||||
var gif = prefix?.Contains("gif", StringComparison.Ordinal) == true;
|
||||
if (prefix is null || avatar.Length > (gif ? maxGifChars : MaxStaticChars) || avatar.Length <= prefix.Length) throw new WebDropException(ErrorCode.BadRequest);
|
||||
if (prefix is null || avatar.Length > (gif ? maxGifChars : maxStaticChars) || avatar.Length <= prefix.Length) throw new WebDropException(ErrorCode.BadRequest);
|
||||
byte[] bytes;
|
||||
try { bytes = Convert.FromBase64String(avatar[prefix.Length..]); }
|
||||
catch (FormatException) { throw new WebDropException(ErrorCode.BadRequest); }
|
||||
|
||||
Reference in new issue
Block a user