Merge pull request #1743 from ONLYOFFICE/feature/urlShortener
update urlShortener
This commit is contained in:
commit
91e359f834
@ -39,6 +39,7 @@ public class UrlShortenerDbContext : DbContext
|
||||
{
|
||||
ModelBuilderWrapper
|
||||
.From(modelBuilder, Database)
|
||||
.AddShortLinks();
|
||||
.AddShortLinks()
|
||||
.AddDbTenant();
|
||||
}
|
||||
}
|
||||
|
@ -28,15 +28,19 @@
|
||||
namespace ASC.Core.Common.EF.Model;
|
||||
public class ShortLink
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public ulong Id { get; set; }
|
||||
public int TenantId { get; set; }
|
||||
public string Short { get; set; }
|
||||
public string Link { get; set; }
|
||||
|
||||
public DbTenant Tenant { get; set; }
|
||||
}
|
||||
|
||||
public static class ShortLinksExtension
|
||||
{
|
||||
public static ModelBuilderWrapper AddShortLinks(this ModelBuilderWrapper modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<ShortLink>().Navigation(e => e.Tenant).AutoInclude(false);
|
||||
modelBuilder
|
||||
.Add(MySqlAddShortLinks, Provider.MySql)
|
||||
.Add(PgSqlAddShortLinks, Provider.PostgreSql);
|
||||
@ -58,18 +62,25 @@ public static class ShortLinksExtension
|
||||
entity.HasKey(e => e.Id)
|
||||
.HasName("PRIMARY");
|
||||
|
||||
entity.HasIndex(e => e.TenantId)
|
||||
.HasDatabaseName("tenant_id");
|
||||
|
||||
entity.Property(e => e.Id)
|
||||
.HasColumnName("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bigint(19)");
|
||||
.HasColumnName("id");
|
||||
|
||||
entity.Property(e => e.Short)
|
||||
.HasColumnName("short")
|
||||
.HasColumnType("varchar(12)")
|
||||
.HasColumnType("char(15)")
|
||||
.HasCharSet("utf8")
|
||||
.UseCollation("utf8_general_ci")
|
||||
.IsRequired(false);
|
||||
|
||||
entity.Property(e => e.TenantId)
|
||||
.IsRequired()
|
||||
.HasColumnName("tenant_id")
|
||||
.HasColumnType("int(10)")
|
||||
.HasDefaultValue("-1");
|
||||
|
||||
entity.Property(e => e.Link)
|
||||
.HasColumnName("link")
|
||||
.HasColumnType("text")
|
||||
@ -92,14 +103,15 @@ public static class ShortLinksExtension
|
||||
entity.HasKey(e => e.Id)
|
||||
.HasName("PRIMARY");
|
||||
|
||||
entity.HasIndex(e => e.TenantId)
|
||||
.HasDatabaseName("tenant_id");
|
||||
|
||||
entity.Property(e => e.Id)
|
||||
.HasColumnName("id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bigint(19)");
|
||||
.HasColumnName("id");
|
||||
|
||||
entity.Property(e => e.Short)
|
||||
.HasColumnName("short")
|
||||
.HasColumnType("varchar(12)")
|
||||
.HasColumnType("char(15)")
|
||||
.HasCharSet("utf8")
|
||||
.UseCollation("utf8_general_ci")
|
||||
.IsRequired(false);
|
||||
@ -109,6 +121,12 @@ public static class ShortLinksExtension
|
||||
.HasColumnType("text")
|
||||
.UseCollation("utf8_bin")
|
||||
.IsRequired(false);
|
||||
|
||||
entity.Property(e => e.TenantId)
|
||||
.IsRequired()
|
||||
.HasColumnName("tenant_id")
|
||||
.HasColumnType("int(10)")
|
||||
.HasDefaultValue("-1");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -223,7 +223,7 @@ server {
|
||||
proxy_redirect off;
|
||||
}
|
||||
|
||||
location ~* /sh/(.*) {
|
||||
location ~* /s/(.*) {
|
||||
proxy_pass http://127.0.0.1:5000;
|
||||
}
|
||||
|
||||
|
7190
migrations/mysql/SaaS/MigrationContext/20230919164723_MigrationContext_Upgrade5.Designer.cs
generated
Normal file
7190
migrations/mysql/SaaS/MigrationContext/20230919164723_MigrationContext_Upgrade5.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,97 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace ASC.Migrations.MySql.SaaS.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class MigrationContext_Upgrade5 : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AlterColumn<string>(
|
||||
name: "short",
|
||||
table: "short_links",
|
||||
type: "char(15)",
|
||||
nullable: true,
|
||||
collation: "utf8_general_ci",
|
||||
oldClrType: typeof(string),
|
||||
oldType: "varchar(12)",
|
||||
oldNullable: true,
|
||||
oldCollation: "utf8_general_ci")
|
||||
.Annotation("MySql:CharSet", "utf8")
|
||||
.OldAnnotation("MySql:CharSet", "utf8");
|
||||
|
||||
migrationBuilder.AlterColumn<ulong>(
|
||||
name: "id",
|
||||
table: "short_links",
|
||||
type: "bigint unsigned",
|
||||
nullable: false,
|
||||
oldClrType: typeof(long),
|
||||
oldType: "bigint(19)")
|
||||
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn)
|
||||
.OldAnnotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "tenant_id",
|
||||
table: "short_links",
|
||||
type: "int(10)",
|
||||
nullable: false,
|
||||
defaultValue: -1);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "tenant_id",
|
||||
table: "short_links",
|
||||
column: "tenant_id");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_short_links_tenants_tenants_tenant_id",
|
||||
table: "short_links",
|
||||
column: "tenant_id",
|
||||
principalTable: "tenants_tenants",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_short_links_tenants_tenants_tenant_id",
|
||||
table: "short_links");
|
||||
|
||||
migrationBuilder.DropIndex(
|
||||
name: "tenant_id",
|
||||
table: "short_links");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "tenant_id",
|
||||
table: "short_links");
|
||||
|
||||
migrationBuilder.AlterColumn<string>(
|
||||
name: "short",
|
||||
table: "short_links",
|
||||
type: "varchar(12)",
|
||||
nullable: true,
|
||||
collation: "utf8_general_ci",
|
||||
oldClrType: typeof(string),
|
||||
oldType: "char(15)",
|
||||
oldNullable: true,
|
||||
oldCollation: "utf8_general_ci")
|
||||
.Annotation("MySql:CharSet", "utf8")
|
||||
.OldAnnotation("MySql:CharSet", "utf8");
|
||||
|
||||
migrationBuilder.AlterColumn<long>(
|
||||
name: "id",
|
||||
table: "short_links",
|
||||
type: "bigint(19)",
|
||||
nullable: false,
|
||||
oldClrType: typeof(ulong),
|
||||
oldType: "bigint unsigned")
|
||||
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn)
|
||||
.OldAnnotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);
|
||||
}
|
||||
}
|
||||
}
|
@ -4648,9 +4648,9 @@ namespace ASC.Migrations.MySql.SaaS.Migrations
|
||||
|
||||
modelBuilder.Entity("ASC.Core.Common.EF.Model.ShortLink", b =>
|
||||
{
|
||||
b.Property<long>("Id")
|
||||
b.Property<ulong>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("bigint(19)")
|
||||
.HasColumnType("bigint unsigned")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<string>("Link")
|
||||
@ -4659,17 +4659,26 @@ namespace ASC.Migrations.MySql.SaaS.Migrations
|
||||
.UseCollation("utf8_bin");
|
||||
|
||||
b.Property<string>("Short")
|
||||
.HasColumnType("varchar(12)")
|
||||
.HasColumnType("char(15)")
|
||||
.HasColumnName("short")
|
||||
.UseCollation("utf8_general_ci")
|
||||
.HasAnnotation("MySql:CharSet", "utf8");
|
||||
|
||||
b.Property<int>("TenantId")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int(10)")
|
||||
.HasDefaultValue(-1)
|
||||
.HasColumnName("tenant_id");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("PRIMARY");
|
||||
|
||||
b.HasIndex("Short")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("TenantId")
|
||||
.HasDatabaseName("tenant_id");
|
||||
|
||||
b.ToTable("short_links", (string)null);
|
||||
|
||||
b
|
||||
@ -6819,6 +6828,17 @@ namespace ASC.Migrations.MySql.SaaS.Migrations
|
||||
b.Navigation("Tenant");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ASC.Core.Common.EF.Model.ShortLink", b =>
|
||||
{
|
||||
b.HasOne("ASC.Core.Common.EF.Model.DbTenant", "Tenant")
|
||||
.WithMany()
|
||||
.HasForeignKey("TenantId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Tenant");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ASC.Core.Common.EF.Model.TelegramUser", b =>
|
||||
{
|
||||
b.HasOne("ASC.Core.Common.EF.Model.DbTenant", "Tenant")
|
||||
|
@ -27,7 +27,7 @@
|
||||
namespace ASC.Web.Core;
|
||||
public class UrlShortRewriter
|
||||
{
|
||||
public const string BasePath = "/sh/";
|
||||
public const string BasePath = "/s/";
|
||||
|
||||
public UrlShortRewriter(RequestDelegate next)
|
||||
{
|
||||
|
@ -83,11 +83,14 @@ public class OnlyoShortener : IUrlShortener
|
||||
{
|
||||
private readonly IDbContextFactory<UrlShortenerDbContext> _contextFactory;
|
||||
private readonly CommonLinkUtility _commonLinkUtility;
|
||||
private readonly TenantManager _tenantManager;
|
||||
public OnlyoShortener(IDbContextFactory<UrlShortenerDbContext> contextFactory,
|
||||
CommonLinkUtility commonLinkUtility)
|
||||
CommonLinkUtility commonLinkUtility,
|
||||
TenantManager tenantManager)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
_commonLinkUtility = commonLinkUtility;
|
||||
_tenantManager = tenantManager;
|
||||
}
|
||||
|
||||
public async Task<string> GetShortenLinkAsync(string shareLink)
|
||||
@ -113,7 +116,8 @@ public class OnlyoShortener : IUrlShortener
|
||||
{
|
||||
Id = id,
|
||||
Link = shareLink,
|
||||
Short = key
|
||||
Short = key,
|
||||
TenantId = (await _tenantManager.GetCurrentTenantAsync()).Id
|
||||
};
|
||||
await context.ShortLinks.AddAsync(newShortLink);
|
||||
await context.SaveChangesAsync();
|
||||
@ -137,7 +141,7 @@ public static class ShortUrl
|
||||
public static string GenerateRandomKey()
|
||||
{
|
||||
var rand = new Random();
|
||||
var length = rand.Next(5, 8);
|
||||
var length = 15;
|
||||
var result = "";
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
@ -147,12 +151,12 @@ public static class ShortUrl
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long Decode(string str)
|
||||
public static ulong Decode(string str)
|
||||
{
|
||||
long num = 0;
|
||||
ulong num = 0;
|
||||
for (var i = 0; i < str.Length; i++)
|
||||
{
|
||||
num = num * _base + Alphabet.IndexOf(str.ElementAt(i));
|
||||
num = num * (ulong)_base + (ulong)Alphabet.IndexOf(str.ElementAt(i));
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user