From 68c6fe6395d19eb3826531a177dc4b30642f614e Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Sun, 20 Sep 2020 13:43:08 +1000 Subject: [PATCH] use random IDs instead of just sequential numbers --- Controllers/DocumentController.cs | 2 +- ... 20200920033715_InitialCreate.Designer.cs} | 7 +++--- ...ate.cs => 20200920033715_InitialCreate.cs} | 3 +-- Migrations/SnootalogueContextModelSnapshot.cs | 5 ++-- Models/Document.cs | 23 ++++++++++++++++++- Models/SeedData.cs | 3 +++ Pages/Documents/Details.cshtml | 2 +- Pages/Documents/Details.cshtml.cs | 2 +- Pages/Documents/Edit.cshtml | 2 +- Pages/Documents/Edit.cshtml.cs | 2 +- Pages/Documents/View.cshtml | 2 +- Pages/Documents/View.cshtml.cs | 2 +- Pages/Upload.cshtml.cs | 1 + README.md | 1 + ViewModels/EditDocument.cs | 2 +- 15 files changed, 41 insertions(+), 18 deletions(-) rename Migrations/{20200917125618_InitialCreate.Designer.cs => 20200920033715_InitialCreate.Designer.cs} (90%) rename Migrations/{20200917125618_InitialCreate.cs => 20200920033715_InitialCreate.cs} (91%) diff --git a/Controllers/DocumentController.cs b/Controllers/DocumentController.cs index 8f15aba..e13cf1d 100644 --- a/Controllers/DocumentController.cs +++ b/Controllers/DocumentController.cs @@ -21,7 +21,7 @@ namespace Snootalogue.Controllers { } [HttpGet("{id}")] - public async Task> GetDocumentById(int id) { + public async Task> GetDocumentById(string id) { var document = await _context.Document.FindAsync(id); if (document == null) { diff --git a/Migrations/20200917125618_InitialCreate.Designer.cs b/Migrations/20200920033715_InitialCreate.Designer.cs similarity index 90% rename from Migrations/20200917125618_InitialCreate.Designer.cs rename to Migrations/20200920033715_InitialCreate.Designer.cs index c797f79..1bed0cc 100644 --- a/Migrations/20200917125618_InitialCreate.Designer.cs +++ b/Migrations/20200920033715_InitialCreate.Designer.cs @@ -9,7 +9,7 @@ using Snootalogue.Data; namespace snootalogue.Migrations { [DbContext(typeof(SnootalogueContext))] - [Migration("20200917125618_InitialCreate")] + [Migration("20200920033715_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -20,9 +20,8 @@ namespace snootalogue.Migrations modelBuilder.Entity("Snootalogue.Models.Document", b => { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); + b.Property("ID") + .HasColumnType("TEXT"); b.Property("Authors") .HasColumnType("jsonb"); diff --git a/Migrations/20200917125618_InitialCreate.cs b/Migrations/20200920033715_InitialCreate.cs similarity index 91% rename from Migrations/20200917125618_InitialCreate.cs rename to Migrations/20200920033715_InitialCreate.cs index 22b2c65..15f9493 100644 --- a/Migrations/20200917125618_InitialCreate.cs +++ b/Migrations/20200920033715_InitialCreate.cs @@ -11,8 +11,7 @@ namespace snootalogue.Migrations name: "Document", columns: table => new { - ID = table.Column(nullable: false) - .Annotation("Sqlite:Autoincrement", true), + ID = table.Column(nullable: false), Filename = table.Column(nullable: true), Hash = table.Column(nullable: true), Size = table.Column(nullable: false), diff --git a/Migrations/SnootalogueContextModelSnapshot.cs b/Migrations/SnootalogueContextModelSnapshot.cs index d983f36..4269178 100644 --- a/Migrations/SnootalogueContextModelSnapshot.cs +++ b/Migrations/SnootalogueContextModelSnapshot.cs @@ -18,9 +18,8 @@ namespace snootalogue.Migrations modelBuilder.Entity("Snootalogue.Models.Document", b => { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); + b.Property("ID") + .HasColumnType("TEXT"); b.Property("Authors") .HasColumnType("jsonb"); diff --git a/Models/Document.cs b/Models/Document.cs index 29d8704..27afb97 100644 --- a/Models/Document.cs +++ b/Models/Document.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using Snootalogue.Data; +using System.Runtime; namespace Snootalogue.Models { public class Document { - public int ID { get; set; } + public string ID { get; set; } public string Filename { get; set; } public string Hash { get; set; } [UIHint("FileSize")] @@ -20,5 +22,24 @@ namespace Snootalogue.Models { public List Tags { get; set; } public Boolean Read { get; set; } public string Notes { get; set; } + + public static string NewID(SnootalogueContext context) { + // - no vowels to avoid (most) (non-welsh) naughty words + // - no l1I because it's always confusing + // - no 0 + string idCharacters = "bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ23456789"; + var rng = new Random(); + var collision = true; + string id = ""; + while (collision) { + id = ""; + for (var i = 0; i < 7; i++) { + id += idCharacters[rng.Next(idCharacters.Length)]; + } + collision = context.Document.Find(id) != null; + } + + return id; + } } } diff --git a/Models/SeedData.cs b/Models/SeedData.cs index 00d9095..b1259f3 100644 --- a/Models/SeedData.cs +++ b/Models/SeedData.cs @@ -18,6 +18,7 @@ namespace Snootalogue.Models { context.Document.AddRange( new Document { + ID = Document.NewID(context), Title = "The Peebler Chronicles", Filename = "peebler.pdf", Size = Convert.ToInt64(0.5 * 1024 * 1024), @@ -27,6 +28,7 @@ namespace Snootalogue.Models { Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0" }, new Document { + ID = Document.NewID(context), Title = "Smooched by a Wifeoid", Filename = "smooched_by_a_wifeoid.pdf", Size = 7 * 1024 * 1024, @@ -37,6 +39,7 @@ namespace Snootalogue.Models { Hash = "0d96a4ff68ad6d4b6f1f30f713b18d5184912ba8dd389f86aa7710db079abcb0" }, new Document { + ID = Document.NewID(context), Title = "What on Boo Earth? My Girlfriend can Walk on the Ceiling?!", Filename = "ceiling_gf.pdf", Size = Convert.ToInt64(22.5 * 1024 * 1024), diff --git a/Pages/Documents/Details.cshtml b/Pages/Documents/Details.cshtml index 5dd25d0..374e0c4 100644 --- a/Pages/Documents/Details.cshtml +++ b/Pages/Documents/Details.cshtml @@ -1,4 +1,4 @@ -@page "{id:int}" +@page "{id}" @model Snootalogue.Pages.Documents.DetailsModel @{ diff --git a/Pages/Documents/Details.cshtml.cs b/Pages/Documents/Details.cshtml.cs index aac756c..0ad2d6d 100644 --- a/Pages/Documents/Details.cshtml.cs +++ b/Pages/Documents/Details.cshtml.cs @@ -14,7 +14,7 @@ namespace Snootalogue.Pages.Documents { _context = context; } - public async Task OnGetAsync(int? id) { + public async Task OnGetAsync(string id) { if (id == null) { return NotFound(); } diff --git a/Pages/Documents/Edit.cshtml b/Pages/Documents/Edit.cshtml index a64744f..b9dbb73 100644 --- a/Pages/Documents/Edit.cshtml +++ b/Pages/Documents/Edit.cshtml @@ -1,4 +1,4 @@ -@page "{id:int}" +@page "{id}" @model Snootalogue.Pages.Documents.EditModel @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Pages/Documents/Edit.cshtml.cs b/Pages/Documents/Edit.cshtml.cs index ae35258..55efe40 100644 --- a/Pages/Documents/Edit.cshtml.cs +++ b/Pages/Documents/Edit.cshtml.cs @@ -17,7 +17,7 @@ namespace Snootalogue.Pages.Documents { _context = context; } - public async Task OnGetAsync(int? id) { + public async Task OnGetAsync(string id) { if (id == null) { return NotFound(); } diff --git a/Pages/Documents/View.cshtml b/Pages/Documents/View.cshtml index 0db8525..2daaeec 100644 --- a/Pages/Documents/View.cshtml +++ b/Pages/Documents/View.cshtml @@ -1,4 +1,4 @@ -@page "{id:int}" +@page "{id}" @model Snootalogue.Pages.Documents.ViewModel @{ diff --git a/Pages/Documents/View.cshtml.cs b/Pages/Documents/View.cshtml.cs index f7d6ea2..41fd71b 100644 --- a/Pages/Documents/View.cshtml.cs +++ b/Pages/Documents/View.cshtml.cs @@ -14,7 +14,7 @@ namespace Snootalogue.Pages.Documents { _context = context; } - public async Task OnGetAsync(int? id) { + public async Task OnGetAsync(string id) { if (id == null) { return NotFound(); } diff --git a/Pages/Upload.cshtml.cs b/Pages/Upload.cshtml.cs index 994ff9f..4b20489 100644 --- a/Pages/Upload.cshtml.cs +++ b/Pages/Upload.cshtml.cs @@ -43,6 +43,7 @@ namespace Snootalogue.Pages { // Document d = _context.Document. Document d = new Document { + ID = Document.NewID(_context), Title = ud.Title, Authors = ud.Authors.Split(",").ToList(), Category = ud.Category, diff --git a/README.md b/README.md index 27b07b3..f4bb952 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Snootalogue runs on port **5000**, so make sure you don't have anything currentl ```bash git clone https://git.bune.city/lynnesbian/Snootalogue cd Snootalogue +mkdir wwwroot/Content if ! which dotnet-ef; then dotnet tool install --global dotnet-ef; fi dotnet ef database update dotnet run diff --git a/ViewModels/EditDocument.cs b/ViewModels/EditDocument.cs index 4e5938d..bef9c56 100644 --- a/ViewModels/EditDocument.cs +++ b/ViewModels/EditDocument.cs @@ -7,7 +7,7 @@ using System.ComponentModel.DataAnnotations; namespace Snootalogue.ViewModels { public class EditDocument { [Required] - public int ID { get; set; } + public string ID { get; set; } [Required] public string Title { get; set; } [Required]