From 3c37e25485e275550323beaec97c0979808886d4 Mon Sep 17 00:00:00 2001 From: Dmitriy yawaflua Andreev Date: Wed, 31 Jul 2024 07:26:18 +0300 Subject: [PATCH] Add project files. --- .dockerignore | 30 +++ .github/workflows/docker-image.yml | 41 ++++ .github/workflows/dotnet.yml | 28 +++ Controllers/v1/SkinsController.cs | 60 +++++ Dockerfile | 24 ++ Interfaces/Services/ISkinService.cs | 9 + Interfaces/SkinService/IProfile.cs | 9 + Models/DecodedSkinProperty.cs | 42 ++++ Models/SkinService/MojangProfile.cs | 10 + Models/SkinService/MojangSessionProperty.cs | 16 ++ Program.cs | 22 ++ Properties/launchSettings.json | 52 +++++ Services/SkinService.cs | 48 ++++ SkinsApi.csproj | 22 ++ SkinsApi.sln | 25 +++ Sources/Skin.cs | 233 ++++++++++++++++++++ Startup.cs | 115 ++++++++++ appsettings.Development.json | 8 + appsettings.json | 25 +++ 19 files changed, 819 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker-image.yml create mode 100644 .github/workflows/dotnet.yml create mode 100644 Controllers/v1/SkinsController.cs create mode 100644 Dockerfile create mode 100644 Interfaces/Services/ISkinService.cs create mode 100644 Interfaces/SkinService/IProfile.cs create mode 100644 Models/DecodedSkinProperty.cs create mode 100644 Models/SkinService/MojangProfile.cs create mode 100644 Models/SkinService/MojangSessionProperty.cs create mode 100644 Program.cs create mode 100644 Properties/launchSettings.json create mode 100644 Services/SkinService.cs create mode 100644 SkinsApi.csproj create mode 100644 SkinsApi.sln create mode 100644 Sources/Skin.cs create mode 100644 Startup.cs create mode 100644 appsettings.Development.json create mode 100644 appsettings.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/.dockerignore @@ -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/** \ No newline at end of file diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..d7915f5 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -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 }} diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..c62b908 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -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 diff --git a/Controllers/v1/SkinsController.cs b/Controllers/v1/SkinsController.cs new file mode 100644 index 0000000..aae22e1 --- /dev/null +++ b/Controllers/v1/SkinsController.cs @@ -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 + { + /// + /// Get user`s skin + /// + /// nickname or UUID + /// Full skin + [HttpGet("{user}")] + [ProducesResponseType(200)] + [Produces("image/png")] + public async Task GetSkinByUser(string user, [FromQuery(Name = "w")] int width = 64) + { + + return File((await skinService.GetSkinStreamAsync(user)).GetAllSkin(width), "image/png"); + } + + /// + /// Get user`s face + /// + /// nickname or UUID + /// Face + [HttpGet("{user}/face")] + [ProducesResponseType(200)] + [Produces("image/png")] + public async Task GetSkinFaceByUser(string user, [FromQuery(Name = "w")] int width = 8) + { + + return File((await skinService.GetSkinStreamAsync(user)).GetFace(width), "image/png"); + } + + /// + /// Get user`s front + /// + /// nickname or UUID + /// Face + [HttpGet("{user}/front")] + [ProducesResponseType(200)] + [Produces("image/png")] + public async Task 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(); + } + + } + } +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f80e44a --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/Interfaces/Services/ISkinService.cs b/Interfaces/Services/ISkinService.cs new file mode 100644 index 0000000..c772cf9 --- /dev/null +++ b/Interfaces/Services/ISkinService.cs @@ -0,0 +1,9 @@ +using SkinsApi.Sources; + +namespace SkinsApi.Interfaces.Services +{ + public interface ISkinService + { + Task GetSkinStreamAsync(string data); + } +} diff --git a/Interfaces/SkinService/IProfile.cs b/Interfaces/SkinService/IProfile.cs new file mode 100644 index 0000000..504fd63 --- /dev/null +++ b/Interfaces/SkinService/IProfile.cs @@ -0,0 +1,9 @@ +namespace SkinsApi.Interfaces.SkinService +{ + public interface IProfile + { + public string id { get; set; } + public string name { get; set; } + } + +} diff --git a/Models/DecodedSkinProperty.cs b/Models/DecodedSkinProperty.cs new file mode 100644 index 0000000..59965c9 --- /dev/null +++ b/Models/DecodedSkinProperty.cs @@ -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; } + } + + +} diff --git a/Models/SkinService/MojangProfile.cs b/Models/SkinService/MojangProfile.cs new file mode 100644 index 0000000..b388783 --- /dev/null +++ b/Models/SkinService/MojangProfile.cs @@ -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; } + } +} diff --git a/Models/SkinService/MojangSessionProperty.cs b/Models/SkinService/MojangSessionProperty.cs new file mode 100644 index 0000000..50c8a62 --- /dev/null +++ b/Models/SkinService/MojangSessionProperty.cs @@ -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 properties { get; set; } + public List profileActions { get; set; } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..37cd318 --- /dev/null +++ b/Program.cs @@ -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(); + builder.ConfigureLogging(k => + { + k.SetMinimumLevel(LogLevel.Warning); + }); + }) + .Build() + .Run(); + } + } +} diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..77a0b7d --- /dev/null +++ b/Properties/launchSettings.json @@ -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 + } + } +} \ No newline at end of file diff --git a/Services/SkinService.cs b/Services/SkinService.cs new file mode 100644 index 0000000..95979b7 --- /dev/null +++ b/Services/SkinService.cs @@ -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 GetProfileByNicknameAsync(string nickname) + { + return await client.GetFromJsonAsync(Startup.PROFILE_MOJANG_API + nickname); + } + private async Task GetUuidFromDeparsedData(string data) + { + if (data.Length < 25) + { + return (await GetProfileByNicknameAsync(data)).id; + } + else + { + return data; + } + } + private async Task GetSkinProperty(string uuid) + { + var base64EncodedSkinProperty = await client.GetFromJsonAsync(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(decodedString); + } + public async Task 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"); + } + + } +} diff --git a/SkinsApi.csproj b/SkinsApi.csproj new file mode 100644 index 0000000..f484415 --- /dev/null +++ b/SkinsApi.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + d06e5f9d-9a73-433d-a76a-e3ffac579b36 + Linux + . + + + + + + + + + + + + + diff --git a/SkinsApi.sln b/SkinsApi.sln new file mode 100644 index 0000000..e329ce6 --- /dev/null +++ b/SkinsApi.sln @@ -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 diff --git a/Sources/Skin.cs b/Sources/Skin.cs new file mode 100644 index 0000000..fd5b9bd --- /dev/null +++ b/Sources/Skin.cs @@ -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(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(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; + + } + } + + } +} diff --git a/Startup.cs b/Startup.cs new file mode 100644 index 0000000..2f70ca2 --- /dev/null +++ b/Startup.cs @@ -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() + .AddSingleton(configuration) + .AddMemoryCache(); + services.Configure(configuration.GetSection("IpRateLimiting")); + services.AddInMemoryRateLimiting(); + + // Добавление Rate Limiting Middleware + services.AddSingleton(); + + + } + + 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(); + + }); + } + } +} diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..af450b4 --- /dev/null +++ b/appsettings.json @@ -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 + } + ] + } + } +}