1
0
Fork 0
mirror of https://github.com/miawinter98/just-short-it.git synced 2024-12-04 04:29:54 +00:00

Added Customization for generated ID length using JSI_CUSTOMIZATION__GENERATEDIDLENGTH

This commit is contained in:
Mia Rose Winter 2024-03-29 19:51:26 +01:00
parent cf4c671d7e
commit 1191e0694d
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
5 changed files with 18 additions and 5 deletions

View file

@ -5,9 +5,12 @@
@using System.Text.RegularExpressions @using System.Text.RegularExpressions
@using System.Web @using System.Web
@using Humanizer @using Humanizer
@using JustShortIt.Model
@using Microsoft.AspNetCore.WebUtilities @using Microsoft.AspNetCore.WebUtilities
@using Microsoft.Extensions.Options
@attribute [Authorize] @attribute [Authorize]
@inject IOptions<Customization> CustomizationOptions
@inject IDistributedCache Db @inject IDistributedCache Db
@inject IConfiguration Configuration @inject IConfiguration Configuration
@inject NavigationManager Navigation @inject NavigationManager Navigation
@ -152,11 +155,14 @@
MessageType = MessageComponent.AlertType.Success; MessageType = MessageComponent.AlertType.Success;
} }
private static string GenerateNewId() { private string GenerateNewId() {
string base64Guid = Regex.Replace( string base64Guid = Regex.Replace(
Convert.ToBase64String(Guid.NewGuid().ToByteArray()), Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
"[/+=]", ""); "[/+=]", "");
return base64Guid[..6];
int length = Math.Min(Math.Max(CustomizationOptions.Value.GeneratedIdLength, 2), 16);
return base64Guid[..length];
} }
#region Models #region Models

View file

@ -1,9 +1,9 @@
<AuthorizeView> <AuthorizeView>
<Authorized> <Authorized>
<span class="mx-3">@(context.User.Identity?.Name ?? "err_username_unknown")</span> <span class="mx-3">@(context.User.Identity?.Name ?? "err_username_unknown")</span>
<a class="btn btn-link text-secondary-content" href="/Logout">Logout</a> <a data-enhance="false" class="btn btn-link text-secondary-content" href="/Logout">Logout</a>
</Authorized> </Authorized>
<NotAuthorized> <NotAuthorized>
<a class="hover:link text-secondary-content" href="/Login">Login</a> <a data-enhance="false" class="hover:link text-secondary-content" href="/Login">Login</a>
</NotAuthorized> </NotAuthorized>
</AuthorizeView> </AuthorizeView>

5
Model/Customization.cs Normal file
View file

@ -0,0 +1,5 @@
namespace JustShortIt.Model;
public class Customization {
public int GeneratedIdLength { get; set; } = 6;
}

View file

@ -15,6 +15,7 @@
var redisConnection = builder.Configuration.GetSection("Redis").Get<RedisConnection>(); var redisConnection = builder.Configuration.GetSection("Redis").Get<RedisConnection>();
var user = builder.Configuration.GetSection("Account").Get<User>(); var user = builder.Configuration.GetSection("Account").Get<User>();
string? baseUrl = builder.Configuration.GetValue<string>("BaseUrl"); string? baseUrl = builder.Configuration.GetValue<string>("BaseUrl");
builder.Services.Configure<Customization>(builder.Configuration.GetSection(nameof(Customization)));
#if DEBUG #if DEBUG
baseUrl = "http://localhost/"; baseUrl = "http://localhost/";

View file

@ -20,7 +20,8 @@
"commandName": "Docker", "commandName": "Docker",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_HTTP_PORTS": "8080" "ASPNETCORE_HTTP_PORTS": "8080",
"JSI_CUSTOMIZATION__GENERATEDIDLENGTH": "12"
}, },
"publishAllPorts": true "publishAllPorts": true
} }