mirror of
https://github.com/yawaflua/SkinsApi.git
synced 2025-12-09 03:49:32 +02:00
Add ping controller and cleaning code
This commit is contained in:
15
Controllers/Default/PingController.cs
Normal file
15
Controllers/Default/PingController.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace SkinsApi.Controllers.Default
|
||||||
|
{
|
||||||
|
[Route("/ping")]
|
||||||
|
[ApiController]
|
||||||
|
public class PingController : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> Ping()
|
||||||
|
{
|
||||||
|
return Ok("Pong");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ namespace SkinsApi.Controllers.v1
|
|||||||
{
|
{
|
||||||
[Route("/skin/")]
|
[Route("/skin/")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class AnotherSkinsController ( ISkinService skinService) : ControllerBase
|
public class AnotherSkinsController(ISkinService skinService) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet("{skin_type}/{width}/{user}")]
|
[HttpGet("{skin_type}/{width}/{user}")]
|
||||||
[Produces("image/png")]
|
[Produces("image/png")]
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.RateLimiting;
|
|
||||||
using SkinsApi.Interfaces.Services;
|
using SkinsApi.Interfaces.Services;
|
||||||
|
|
||||||
namespace SkinsApi.Controllers.v1
|
namespace SkinsApi.Controllers.v1
|
||||||
{
|
{
|
||||||
[Route("/api/v1/skin/")]
|
[Route("/api/v1/skin/")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
|
|
||||||
public class SkinsController (ISkinService skinService): ControllerBase
|
public class SkinsController(ISkinService skinService) : ControllerBase
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get user`s skin
|
/// Get user`s skin
|
||||||
@@ -50,11 +49,12 @@ namespace SkinsApi.Controllers.v1
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
return File((await skinService.GetSkinStreamAsync(user)).GetBody(width), "image/png");
|
return File((await skinService.GetSkinStreamAsync(user)).GetBody(width), "image/png");
|
||||||
} catch(Exception ex)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,12 @@ using SkinsApi.Interfaces.SkinService;
|
|||||||
using SkinsApi.Models;
|
using SkinsApi.Models;
|
||||||
using SkinsApi.Models.SkinService;
|
using SkinsApi.Models.SkinService;
|
||||||
using SkinsApi.Sources;
|
using SkinsApi.Sources;
|
||||||
using System.Buffers.Text;
|
|
||||||
using System.Drawing;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace SkinsApi.Services
|
namespace SkinsApi.Services
|
||||||
{
|
{
|
||||||
public class SkinService (HttpClient client): ISkinService
|
public class SkinService(HttpClient client) : ISkinService
|
||||||
{
|
{
|
||||||
private async Task<IProfile> GetProfileByNicknameAsync(string nickname)
|
private async Task<IProfile> GetProfileByNicknameAsync(string nickname)
|
||||||
{
|
{
|
||||||
@@ -37,12 +34,12 @@ namespace SkinsApi.Services
|
|||||||
return JsonSerializer.Deserialize<DecodedSkinProperty>(decodedString);
|
return JsonSerializer.Deserialize<DecodedSkinProperty>(decodedString);
|
||||||
}
|
}
|
||||||
public async Task<Skin> GetSkinStreamAsync(string data)
|
public async Task<Skin> GetSkinStreamAsync(string data)
|
||||||
{
|
{
|
||||||
var uuid = await GetUuidFromDeparsedData(data);
|
var uuid = await GetUuidFromDeparsedData(data);
|
||||||
var skinproperty = await GetSkinProperty(uuid);
|
var skinproperty = await GetSkinProperty(uuid);
|
||||||
var rq = await client.GetAsync(skinproperty.Textures.SKIN.Url);
|
var rq = await client.GetAsync(skinproperty.Textures.SKIN.Url);
|
||||||
return new Skin(rq.Content.ReadAsStream(), skinproperty.Textures.SKIN.Metadata?.Model == "slim");
|
return new Skin(rq.Content.ReadAsStream(), skinproperty.Textures.SKIN.Metadata?.Model == "slim");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.Formats;
|
|
||||||
using SixLabors.ImageSharp.Formats.Png;
|
using SixLabors.ImageSharp.Formats.Png;
|
||||||
using SixLabors.ImageSharp.PixelFormats;
|
using SixLabors.ImageSharp.PixelFormats;
|
||||||
using SixLabors.ImageSharp.Processing;
|
using SixLabors.ImageSharp.Processing;
|
||||||
@@ -45,7 +44,7 @@ namespace SkinsApi.Sources
|
|||||||
public Stream GetAllSkin(float width = 64f)
|
public Stream GetAllSkin(float width = 64f)
|
||||||
{
|
{
|
||||||
var stream = new MemoryStream();
|
var stream = new MemoryStream();
|
||||||
int resizedWidth = (int)(_bitmap.Width * (width/_bitmap.Width));
|
int resizedWidth = (int)(_bitmap.Width * (width / _bitmap.Width));
|
||||||
var resizedPicture = _bitmap.Clone(c => c.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
var resizedPicture = _bitmap.Clone(c => c.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
||||||
resizedPicture.Save(stream, new PngEncoder());
|
resizedPicture.Save(stream, new PngEncoder());
|
||||||
stream.Position = 0;
|
stream.Position = 0;
|
||||||
@@ -121,7 +120,7 @@ namespace SkinsApi.Sources
|
|||||||
.Resize(115, 171, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
.Resize(115, 171, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||||
var bodyImageLayout = sourceImage.Clone(ctx => ctx
|
var bodyImageLayout = sourceImage.Clone(ctx => ctx
|
||||||
.Crop(new(bodyLayoutStart, body))
|
.Crop(new(bodyLayoutStart, body))
|
||||||
.Resize((int)(115*0.2), (int)(171*0.2), KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
.Resize((int)(115 * 0.2), (int)(171 * 0.2), KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||||
finalImage.Mutate(ctx => ctx
|
finalImage.Mutate(ctx => ctx
|
||||||
.DrawImage(bodyImage, new Point(87, 120), 1));
|
.DrawImage(bodyImage, new Point(87, 120), 1));
|
||||||
finalImage.Mutate(ctx => ctx
|
finalImage.Mutate(ctx => ctx
|
||||||
@@ -219,13 +218,13 @@ namespace SkinsApi.Sources
|
|||||||
}
|
}
|
||||||
|
|
||||||
var stream = new MemoryStream();
|
var stream = new MemoryStream();
|
||||||
|
|
||||||
int resizedWidth = (int)(finalImage.Width * (width/ finalImage.Width));
|
int resizedWidth = (int)(finalImage.Width * (width / finalImage.Width));
|
||||||
finalImage.Mutate(k => k.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
finalImage.Mutate(k => k.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
||||||
finalImage.Save(stream, new PngEncoder());
|
finalImage.Save(stream, new PngEncoder());
|
||||||
stream.Position = 0;
|
stream.Position = 0;
|
||||||
return stream;
|
return stream;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
Startup.cs
24
Startup.cs
@@ -1,13 +1,9 @@
|
|||||||
using Microsoft.OpenApi.Models;
|
using AspNetCoreRateLimit;
|
||||||
using System.Net.Http.Headers;
|
using Microsoft.OpenApi.Models;
|
||||||
using System.Net;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
using SkinsApi.Interfaces.Services;
|
using SkinsApi.Interfaces.Services;
|
||||||
using SkinsApi.Services;
|
using SkinsApi.Services;
|
||||||
using System.Threading.RateLimiting;
|
using System.Reflection;
|
||||||
using SixLabors.ImageSharp;
|
using System.Text.Json.Serialization;
|
||||||
using AspNetCoreRateLimit;
|
|
||||||
|
|
||||||
namespace SkinsApi
|
namespace SkinsApi
|
||||||
{
|
{
|
||||||
@@ -51,9 +47,9 @@ namespace SkinsApi
|
|||||||
},
|
},
|
||||||
TermsOfService = new Uri("https://yawaflua.ru/privacy")
|
TermsOfService = new Uri("https://yawaflua.ru/privacy")
|
||||||
});
|
});
|
||||||
|
|
||||||
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||||
// setup.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);
|
// setup.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);
|
||||||
setup.AddServer(new OpenApiServer() { Url = "https://skins.yawaflua.ru/", Description = "Default web-server" });
|
setup.AddServer(new OpenApiServer() { Url = "https://skins.yawaflua.ru/", Description = "Default web-server" });
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
setup.AddServer(new OpenApiServer() { Url = "http://localhost/", Description = "Dev web-server" });
|
setup.AddServer(new OpenApiServer() { Url = "http://localhost/", Description = "Dev web-server" });
|
||||||
@@ -64,11 +60,11 @@ namespace SkinsApi
|
|||||||
.AddSingleton<ISkinService, SkinService>()
|
.AddSingleton<ISkinService, SkinService>()
|
||||||
.AddSingleton(configuration)
|
.AddSingleton(configuration)
|
||||||
.AddMemoryCache();
|
.AddMemoryCache();
|
||||||
services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
|
services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
|
||||||
services.AddInMemoryRateLimiting();
|
services.AddInMemoryRateLimiting();
|
||||||
|
|
||||||
// Добавление Rate Limiting Middleware
|
// Добавление Rate Limiting Middleware
|
||||||
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user