mirror of
https://github.com/yawaflua/SkinsApi.git
synced 2025-12-08 19:39:29 +02:00
Add project files.
This commit is contained in:
30
.dockerignore
Normal file
30
.dockerignore
Normal file
@@ -0,0 +1,30 @@
|
||||
**/.classpath
|
||||
**/.dockerignore
|
||||
**/.env
|
||||
**/.git
|
||||
**/.gitignore
|
||||
**/.project
|
||||
**/.settings
|
||||
**/.toolstarget
|
||||
**/.vs
|
||||
**/.vscode
|
||||
**/*.*proj.user
|
||||
**/*.dbmdl
|
||||
**/*.jfm
|
||||
**/azds.yaml
|
||||
**/bin
|
||||
**/charts
|
||||
**/docker-compose*
|
||||
**/Dockerfile*
|
||||
**/node_modules
|
||||
**/npm-debug.log
|
||||
**/obj
|
||||
**/secrets.dev.yaml
|
||||
**/values.dev.yaml
|
||||
LICENSE
|
||||
README.md
|
||||
!**/.gitignore
|
||||
!.git/HEAD
|
||||
!.git/config
|
||||
!.git/packed-refs
|
||||
!.git/refs/heads/**
|
||||
41
.github/workflows/docker-image.yml
vendored
Normal file
41
.github/workflows/docker-image.yml
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
name: Publish Docker image
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
|
||||
|
||||
jobs:
|
||||
push_to_registries:
|
||||
name: Push Docker image to multiple registries
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
packages: write
|
||||
contents: read
|
||||
steps:
|
||||
- name: Check out the repo
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Log in to the Container registry
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Extract metadata (tags, labels) for Docker
|
||||
id: meta
|
||||
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
|
||||
with:
|
||||
images: |
|
||||
ghcr.io/${{ github.repository }}
|
||||
|
||||
- name: Build and push Docker images
|
||||
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
28
.github/workflows/dotnet.yml
vendored
Normal file
28
.github/workflows/dotnet.yml
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
# This workflow will build a .NET project
|
||||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
|
||||
|
||||
name: .NET
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: 8.0.x
|
||||
- name: Restore dependencies
|
||||
run: dotnet restore
|
||||
- name: Build
|
||||
run: dotnet build --no-restore
|
||||
- name: Test
|
||||
run: dotnet test --no-build --verbosity normal
|
||||
60
Controllers/v1/SkinsController.cs
Normal file
60
Controllers/v1/SkinsController.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using SkinsApi.Interfaces.Services;
|
||||
|
||||
namespace SkinsApi.Controllers.v1
|
||||
{
|
||||
[Route("/api/v1/skin/")]
|
||||
[ApiController]
|
||||
|
||||
public class SkinsController (ISkinService skinService): ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Get user`s skin
|
||||
/// </summary>
|
||||
/// <param name="user">nickname or UUID</param>
|
||||
/// <returns>Full skin</returns>
|
||||
[HttpGet("{user}")]
|
||||
[ProducesResponseType(200)]
|
||||
[Produces("image/png")]
|
||||
public async Task<IActionResult> GetSkinByUser(string user, [FromQuery(Name = "w")] int width = 64)
|
||||
{
|
||||
|
||||
return File((await skinService.GetSkinStreamAsync(user)).GetAllSkin(width), "image/png");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get user`s face
|
||||
/// </summary>
|
||||
/// <param name="user">nickname or UUID</param>
|
||||
/// <returns>Face</returns>
|
||||
[HttpGet("{user}/face")]
|
||||
[ProducesResponseType(200)]
|
||||
[Produces("image/png")]
|
||||
public async Task<IActionResult> GetSkinFaceByUser(string user, [FromQuery(Name = "w")] int width = 8)
|
||||
{
|
||||
|
||||
return File((await skinService.GetSkinStreamAsync(user)).GetFace(width), "image/png");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get user`s front
|
||||
/// </summary>
|
||||
/// <param name="user">nickname or UUID</param>
|
||||
/// <returns>Face</returns>
|
||||
[HttpGet("{user}/front")]
|
||||
[ProducesResponseType(200)]
|
||||
[Produces("image/png")]
|
||||
public async Task<IActionResult> GetSkinFrontByUser(string user, [FromQuery(Name = "w")] int width = 128)
|
||||
{
|
||||
try
|
||||
{
|
||||
return File((await skinService.GetSkinStreamAsync(user)).GetBody(width), "image/png");
|
||||
} catch(Exception ex)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Dockerfile
Normal file
24
Dockerfile
Normal file
@@ -0,0 +1,24 @@
|
||||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER app
|
||||
WORKDIR /app
|
||||
EXPOSE 80
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["SkinsApi.csproj", "."]
|
||||
RUN dotnet restore "./SkinsApi.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/."
|
||||
RUN dotnet build "./SkinsApi.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./SkinsApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "SkinsApi.dll"]
|
||||
9
Interfaces/Services/ISkinService.cs
Normal file
9
Interfaces/Services/ISkinService.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using SkinsApi.Sources;
|
||||
|
||||
namespace SkinsApi.Interfaces.Services
|
||||
{
|
||||
public interface ISkinService
|
||||
{
|
||||
Task<Skin> GetSkinStreamAsync(string data);
|
||||
}
|
||||
}
|
||||
9
Interfaces/SkinService/IProfile.cs
Normal file
9
Interfaces/SkinService/IProfile.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace SkinsApi.Interfaces.SkinService
|
||||
{
|
||||
public interface IProfile
|
||||
{
|
||||
public string id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
42
Models/DecodedSkinProperty.cs
Normal file
42
Models/DecodedSkinProperty.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SkinsApi.Models
|
||||
{
|
||||
public class Metadata
|
||||
{
|
||||
[JsonPropertyName("model")]
|
||||
public string Model { get; set; }
|
||||
}
|
||||
|
||||
public class DecodedSkinProperty
|
||||
{
|
||||
[JsonPropertyName("timestamp")]
|
||||
public long Timestamp { get; set; }
|
||||
|
||||
[JsonPropertyName("profileId")]
|
||||
public string ProfileId { get; set; }
|
||||
|
||||
[JsonPropertyName("profileName")]
|
||||
public string ProfileName { get; set; }
|
||||
|
||||
[JsonPropertyName("textures")]
|
||||
public Textures Textures { get; set; }
|
||||
}
|
||||
|
||||
public class SKIN
|
||||
{
|
||||
[JsonPropertyName("url")]
|
||||
public string Url { get; set; }
|
||||
|
||||
[JsonPropertyName("metadata")]
|
||||
public Metadata Metadata { get; set; }
|
||||
}
|
||||
|
||||
public class Textures
|
||||
{
|
||||
[JsonPropertyName("SKIN")]
|
||||
public SKIN SKIN { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
10
Models/SkinService/MojangProfile.cs
Normal file
10
Models/SkinService/MojangProfile.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using SkinsApi.Interfaces.SkinService;
|
||||
|
||||
namespace SkinsApi.Models.SkinService
|
||||
{
|
||||
public class MojangProfile : IProfile
|
||||
{
|
||||
public string id { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
}
|
||||
16
Models/SkinService/MojangSessionProperty.cs
Normal file
16
Models/SkinService/MojangSessionProperty.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace SkinsApi.Models.SkinService
|
||||
{
|
||||
public class Property
|
||||
{
|
||||
public string name { get; set; }
|
||||
public string value { get; set; }
|
||||
}
|
||||
|
||||
public class MojangSessionProperty
|
||||
{
|
||||
public string id { get; set; }
|
||||
public string name { get; set; }
|
||||
public List<Property> properties { get; set; }
|
||||
public List<object> profileActions { get; set; }
|
||||
}
|
||||
}
|
||||
22
Program.cs
Normal file
22
Program.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
namespace SkinsApi
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHost(builder =>
|
||||
{
|
||||
builder.UseKestrel(kestrelBuilder => kestrelBuilder.ListenAnyIP(80));
|
||||
builder.UseStartup<Startup>();
|
||||
builder.ConfigureLogging(k =>
|
||||
{
|
||||
k.SetMinimumLevel(LogLevel.Warning);
|
||||
});
|
||||
})
|
||||
.Build()
|
||||
.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Properties/launchSettings.json
Normal file
52
Properties/launchSettings.json
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5107"
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7108;http://localhost:5107"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Container (Dockerfile)": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_HTTPS_PORTS": "8081",
|
||||
"ASPNETCORE_HTTP_PORTS": "8080"
|
||||
},
|
||||
"publishAllPorts": true,
|
||||
"useSSL": true
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:63327",
|
||||
"sslPort": 44377
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Services/SkinService.cs
Normal file
48
Services/SkinService.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using SkinsApi.Interfaces.Services;
|
||||
using SkinsApi.Interfaces.SkinService;
|
||||
using SkinsApi.Models;
|
||||
using SkinsApi.Models.SkinService;
|
||||
using SkinsApi.Sources;
|
||||
using System.Buffers.Text;
|
||||
using System.Drawing;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SkinsApi.Services
|
||||
{
|
||||
public class SkinService (HttpClient client): ISkinService
|
||||
{
|
||||
private async Task<IProfile> GetProfileByNicknameAsync(string nickname)
|
||||
{
|
||||
return await client.GetFromJsonAsync<MojangProfile>(Startup.PROFILE_MOJANG_API + nickname);
|
||||
}
|
||||
private async Task<string> GetUuidFromDeparsedData(string data)
|
||||
{
|
||||
if (data.Length < 25)
|
||||
{
|
||||
return (await GetProfileByNicknameAsync(data)).id;
|
||||
}
|
||||
else
|
||||
{
|
||||
return data;
|
||||
}
|
||||
}
|
||||
private async Task<DecodedSkinProperty> GetSkinProperty(string uuid)
|
||||
{
|
||||
var base64EncodedSkinProperty = await client.GetFromJsonAsync<MojangSessionProperty>(Startup.SESSION_MOJANG_API + uuid);
|
||||
var content = base64EncodedSkinProperty.properties.First().value;
|
||||
byte[] data = Convert.FromBase64String(content);
|
||||
string decodedString = Encoding.UTF8.GetString(data);
|
||||
return JsonSerializer.Deserialize<DecodedSkinProperty>(decodedString);
|
||||
}
|
||||
public async Task<Skin> GetSkinStreamAsync(string data)
|
||||
{
|
||||
var uuid = await GetUuidFromDeparsedData(data);
|
||||
var skinproperty = await GetSkinProperty(uuid);
|
||||
var rq = await client.GetAsync(skinproperty.Textures.SKIN.Url);
|
||||
return new Skin(rq.Content.ReadAsStream(), skinproperty.Textures.SKIN.Metadata?.Model == "slim");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
22
SkinsApi.csproj
Normal file
22
SkinsApi.csproj
Normal file
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>d06e5f9d-9a73-433d-a76a-e3ffac579b36</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<DockerfileContext>.</DockerfileContext>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
|
||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
|
||||
<PackageReference Include="System.IO" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
SkinsApi.sln
Normal file
25
SkinsApi.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34728.123
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkinsApi", "SkinsApi.csproj", "{A8968B50-B4B0-45E4-90E5-ADC2BC0CD4F0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A8968B50-B4B0-45E4-90E5-ADC2BC0CD4F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A8968B50-B4B0-45E4-90E5-ADC2BC0CD4F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A8968B50-B4B0-45E4-90E5-ADC2BC0CD4F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A8968B50-B4B0-45E4-90E5-ADC2BC0CD4F0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {9A0A2431-7140-456B-A7E9-3FE2B759F076}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
233
Sources/Skin.cs
Normal file
233
Sources/Skin.cs
Normal file
@@ -0,0 +1,233 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats;
|
||||
using SixLabors.ImageSharp.Formats.Png;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
|
||||
|
||||
namespace SkinsApi.Sources
|
||||
{
|
||||
public class Skin
|
||||
{
|
||||
private Image _bitmap;
|
||||
private bool isSlimSkin = false;
|
||||
private bool isTwoLayedSkin = true;
|
||||
|
||||
Point faceStart = new(8, 8);
|
||||
Size face = new(8, 8);
|
||||
|
||||
Point bodyStart = new(20, 20);
|
||||
Size body = new(9, 12);
|
||||
|
||||
Point rightArmStart = new(44, 20);
|
||||
Point leftArmStart = new(36, 52);
|
||||
Size slimArm = new(3, 12);
|
||||
Size arm = new(4, 12);
|
||||
|
||||
Point leftLegStart = new(20, 52);
|
||||
Point rightLegStart = new(4, 29);
|
||||
Size leg = new(4, 12);
|
||||
|
||||
Point faceLayoutStart = new(40, 8);
|
||||
Point rightArmLayoutStart = new(44, 36);
|
||||
Point leftArmLayoutStart = new(52, 52);
|
||||
Point bodyLayoutStart = new(20, 36);
|
||||
|
||||
|
||||
public Skin(Stream skinStream, bool slim = false)
|
||||
{
|
||||
_bitmap = Image.Load(skinStream);
|
||||
isSlimSkin = slim;
|
||||
isTwoLayedSkin = _bitmap.Width == 64 && _bitmap.Height == 64;
|
||||
|
||||
}
|
||||
|
||||
public Stream GetAllSkin(float width = 64f)
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
int resizedWidth = (int)(_bitmap.Width * (width/_bitmap.Width));
|
||||
var resizedPicture = _bitmap.Clone(c => c.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
||||
resizedPicture.Save(stream, new PngEncoder());
|
||||
stream.Position = 0;
|
||||
// Сохранение результата
|
||||
return stream;
|
||||
}
|
||||
|
||||
public Stream GetFace(float width = 8f)
|
||||
{
|
||||
using (var croppedFace = _bitmap.Clone(k => k.Crop(new(faceStart, face))))
|
||||
{
|
||||
if (isTwoLayedSkin)
|
||||
{
|
||||
var secondLayer = _bitmap.Clone(k => k.Crop(new(faceLayoutStart, face)));
|
||||
croppedFace.Mutate(k => k.DrawImage(secondLayer, new Point(0, 0), 1f));
|
||||
}
|
||||
|
||||
var stream = new MemoryStream();
|
||||
int resizedWidth = (int)(croppedFace.Width * (width / croppedFace.Width));
|
||||
var resizedPicture = croppedFace.Clone(c => c.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
||||
resizedPicture.Save(stream, new PngEncoder());
|
||||
stream.Position = 0;
|
||||
// Сохранение результата
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
|
||||
public Stream GetFull(float width = 128f)
|
||||
{
|
||||
|
||||
using (Image finalImage = new Image<Rgba32>(290, 290))
|
||||
{
|
||||
// Загрузка исходного изображения
|
||||
using (Image sourceImage = _bitmap)
|
||||
{
|
||||
int armPositionDel = isSlimSkin ? 1 : 0;
|
||||
int armSize = isSlimSkin ? 44 : 58;
|
||||
int armPositionSum = isSlimSkin ? 14 : 0;
|
||||
|
||||
var slimRightArm = rightArmStart;
|
||||
var slimRightArmLayout = rightArmLayoutStart;
|
||||
slimRightArm.X -= armPositionDel;
|
||||
slimRightArmLayout.X -= armPositionDel;
|
||||
// left arm
|
||||
var leftArm = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(leftArmStart, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
|
||||
var leftArmLayout = sourceImage.Clone(c => c
|
||||
.Crop(new(leftArmLayoutStart, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(leftArm, new Point(29 + armPositionSum, 120), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(leftArmLayout, new Point(29 + armPositionSum, 120), 1));
|
||||
|
||||
// right arm
|
||||
var rightArm = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(slimRightArm, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
var rightArmLayout = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(slimRightArmLayout, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(rightArm, new Point(200, 120), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(rightArmLayout, new Point(200, 120), 1));
|
||||
|
||||
// body
|
||||
var bodyImage = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(bodyStart, body))
|
||||
.Resize(115, 171, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
var bodyImageLayout = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(bodyLayoutStart, body))
|
||||
.Resize((int)(115*0.2), (int)(171*0.2), KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(bodyImage, new Point(87, 120), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(bodyImageLayout, new Point(87, 120), 1));
|
||||
|
||||
// head
|
||||
var head = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(faceStart, face))
|
||||
.Resize(115, 115, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
var headLayout = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(faceLayoutStart, face))
|
||||
.Resize(115, 115, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(head, new Point(87, 5), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(headLayout, new Point(87, 5), 1));
|
||||
}
|
||||
|
||||
var stream = new MemoryStream();
|
||||
|
||||
int resizedWidth = (int)(finalImage.Width * (width / finalImage.Width));
|
||||
finalImage.Mutate(k => k.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
||||
finalImage.Save(stream, new PngEncoder());
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Stream GetBody(float width = 128f)
|
||||
{
|
||||
|
||||
using (Image finalImage = new Image<Rgba32>(290, 290))
|
||||
{
|
||||
// Загрузка исходного изображения
|
||||
using (Image sourceImage = _bitmap)
|
||||
{
|
||||
int armPositionDel = isSlimSkin ? 1 : 0;
|
||||
int armSize = isSlimSkin ? 44 : 58;
|
||||
int armPositionSum = isSlimSkin ? 14 : 0;
|
||||
|
||||
var slimRightArm = rightArmStart;
|
||||
var slimRightArmLayout = rightArmLayoutStart;
|
||||
slimRightArm.X -= armPositionDel;
|
||||
slimRightArmLayout.X -= armPositionDel;
|
||||
// left arm
|
||||
var leftArm = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(leftArmStart, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
|
||||
var leftArmLayout = sourceImage.Clone(c => c
|
||||
.Crop(new(leftArmLayoutStart, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(leftArm, new Point(29 + armPositionSum, 120), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(leftArmLayout, new Point(29 + armPositionSum, 120), 1));
|
||||
|
||||
// right arm
|
||||
var rightArm = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(slimRightArm, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
var rightArmLayout = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(slimRightArmLayout, arm))
|
||||
.Resize(armSize, 171, KnownResamplers.NearestNeighbor));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(rightArm, new Point(200, 120), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(rightArmLayout, new Point(200, 120), 1));
|
||||
|
||||
// body
|
||||
var bodyImage = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(bodyStart, body))
|
||||
.Resize(115, 171, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
var bodyImageLayout = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(bodyLayoutStart, body))
|
||||
.Resize(115, 171, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(bodyImage, new Point(87, 120), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(bodyImageLayout, new Point(87, 120), 1));
|
||||
|
||||
// head
|
||||
var head = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(faceStart, face))
|
||||
.Resize(115, 115, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
var headLayout = sourceImage.Clone(ctx => ctx
|
||||
.Crop(new(faceLayoutStart, face))
|
||||
.Resize(115, 115, KnownResamplers.NearestNeighbor)); // Изменение размера, если необходимо
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(head, new Point(87, 5), 1));
|
||||
finalImage.Mutate(ctx => ctx
|
||||
.DrawImage(headLayout, new Point(87, 5), 1));
|
||||
}
|
||||
|
||||
var stream = new MemoryStream();
|
||||
|
||||
int resizedWidth = (int)(finalImage.Width * (width/ finalImage.Width));
|
||||
finalImage.Mutate(k => k.Resize(resizedWidth, resizedWidth, KnownResamplers.NearestNeighbor));
|
||||
finalImage.Save(stream, new PngEncoder());
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
115
Startup.cs
Normal file
115
Startup.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text.Json.Serialization;
|
||||
using SkinsApi.Interfaces.Services;
|
||||
using SkinsApi.Services;
|
||||
using System.Threading.RateLimiting;
|
||||
using SixLabors.ImageSharp;
|
||||
using AspNetCoreRateLimit;
|
||||
|
||||
namespace SkinsApi
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public static readonly string PROFILE_MOJANG_API = "https://api.mojang.com/users/profiles/minecraft/";
|
||||
public static readonly string SESSION_MOJANG_API = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||
public IConfiguration configuration { get; internal set; }
|
||||
public Startup()
|
||||
{
|
||||
configuration = new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.Build();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers().AddJsonOptions(l => l.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
|
||||
|
||||
services
|
||||
.AddSwaggerGen(setup =>
|
||||
{
|
||||
setup.SwaggerDoc("v1", new()
|
||||
{
|
||||
Description = "That`s API for getting slices of minecraft skin.",
|
||||
Title = "yawaflua.ru API",
|
||||
Version = "v1.0",
|
||||
Contact = new OpenApiContact()
|
||||
{
|
||||
Name = "Dima Andreev",
|
||||
Email = "skins-swagger@yawaflua.ru",
|
||||
Url = new Uri("https://yawaflua.ru/")
|
||||
},
|
||||
License = new OpenApiLicense()
|
||||
{
|
||||
Name = "MIT Licence",
|
||||
Url = new Uri("https://yawaflua.ru/eula")
|
||||
},
|
||||
TermsOfService = new Uri("https://yawaflua.ru/privacy")
|
||||
});
|
||||
|
||||
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
// setup.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);
|
||||
setup.AddServer(new OpenApiServer() { Url = "https://skins.yawaflua.ru/", Description = "Default web-server" });
|
||||
#if DEBUG
|
||||
setup.AddServer(new OpenApiServer() { Url = "http://localhost/", Description = "Dev web-server" });
|
||||
#endif
|
||||
})
|
||||
.AddRouting()
|
||||
.AddSingleton(new HttpClient())
|
||||
.AddSingleton<ISkinService, SkinService>()
|
||||
.AddSingleton(configuration)
|
||||
.AddMemoryCache();
|
||||
services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
|
||||
services.AddInMemoryRateLimiting();
|
||||
|
||||
// Добавление Rate Limiting Middleware
|
||||
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
|
||||
|
||||
app.UseSwagger(c =>
|
||||
{
|
||||
c.RouteTemplate = "/swagger/v1/swagger.json";
|
||||
c.SerializeAsV2 = true;
|
||||
});
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
|
||||
options.RoutePrefix = string.Empty;
|
||||
options.DisplayRequestDuration();
|
||||
options.EnablePersistAuthorization();
|
||||
options.OAuthAppName("yawaflua.ru/api");
|
||||
options.ShowExtensions();
|
||||
});
|
||||
|
||||
app.UseStaticFiles();
|
||||
app.UseRouting();
|
||||
app.UseCors(k => { k.AllowAnyHeader(); k.AllowAnyMethod(); k.AllowAnyOrigin(); });
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapDefaultControllerRoute();
|
||||
endpoints.MapSwagger();
|
||||
endpoints.MapControllers().AllowAnonymous();
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
8
appsettings.Development.json
Normal file
8
appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
25
appsettings.json
Normal file
25
appsettings.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
|
||||
"IpRateLimiting": {
|
||||
"EnableEndpointRateLimiting": true,
|
||||
"StackBlockedRequests": false,
|
||||
"RealIpHeader": "X-Real-IP",
|
||||
"ClientIdHeader": "X-ClientId",
|
||||
"HttpStatusCode": 429,
|
||||
"GeneralRules": [
|
||||
{
|
||||
"Endpoint": "*",
|
||||
"Period": "1m",
|
||||
"Limit": 15
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user