Browse Source

use random IDs instead of just sequential numbers

master
Lynne Megido 1 month ago
parent
commit
68c6fe6395
Signed by: lynnesbian GPG Key ID: F0A184B5213D9F90
15 changed files with 41 additions and 18 deletions
  1. +1
    -1
      Controllers/DocumentController.cs
  2. +3
    -4
      Migrations/20200920033715_InitialCreate.Designer.cs
  3. +1
    -2
      Migrations/20200920033715_InitialCreate.cs
  4. +2
    -3
      Migrations/SnootalogueContextModelSnapshot.cs
  5. +22
    -1
      Models/Document.cs
  6. +3
    -0
      Models/SeedData.cs
  7. +1
    -1
      Pages/Documents/Details.cshtml
  8. +1
    -1
      Pages/Documents/Details.cshtml.cs
  9. +1
    -1
      Pages/Documents/Edit.cshtml
  10. +1
    -1
      Pages/Documents/Edit.cshtml.cs
  11. +1
    -1
      Pages/Documents/View.cshtml
  12. +1
    -1
      Pages/Documents/View.cshtml.cs
  13. +1
    -0
      Pages/Upload.cshtml.cs
  14. +1
    -0
      README.md
  15. +1
    -1
      ViewModels/EditDocument.cs

+ 1
- 1
Controllers/DocumentController.cs View File

@ -21,7 +21,7 @@ namespace Snootalogue.Controllers {
}
[HttpGet("{id}")]
public async Task<ActionResult<Document>> GetDocumentById(int id) {
public async Task<ActionResult<Document>> GetDocumentById(string id) {
var document = await _context.Document.FindAsync(id);
if (document == null) {

Migrations/20200917125618_InitialCreate.Designer.cs → Migrations/20200920033715_InitialCreate.Designer.cs View File

@ -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<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("ID")
.HasColumnType("TEXT");
b.Property<string>("Authors")
.HasColumnType("jsonb");

Migrations/20200917125618_InitialCreate.cs → Migrations/20200920033715_InitialCreate.cs View File

@ -11,8 +11,7 @@ namespace snootalogue.Migrations
name: "Document",
columns: table => new
{
ID = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
ID = table.Column<string>(nullable: false),
Filename = table.Column<string>(nullable: true),
Hash = table.Column<string>(nullable: true),
Size = table.Column<long>(nullable: false),

+ 2
- 3
Migrations/SnootalogueContextModelSnapshot.cs View File

@ -18,9 +18,8 @@ namespace snootalogue.Migrations
modelBuilder.Entity("Snootalogue.Models.Document", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("ID")
.HasColumnType("TEXT");
b.Property<string>("Authors")
.HasColumnType("jsonb");

+ 22
- 1
Models/Document.cs View File

@ -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<string> 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;
}
}
}

+ 3
- 0
Models/SeedData.cs View File

@ -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),

+ 1
- 1
Pages/Documents/Details.cshtml View File

@ -1,4 +1,4 @@
@page "{id:int}"
@page "{id}"
@model Snootalogue.Pages.Documents.DetailsModel
@{

+ 1
- 1
Pages/Documents/Details.cshtml.cs View File

@ -14,7 +14,7 @@ namespace Snootalogue.Pages.Documents {
_context = context;
}
public async Task<IActionResult> OnGetAsync(int? id) {
public async Task<IActionResult> OnGetAsync(string id) {
if (id == null) {
return NotFound();
}

+ 1
- 1
Pages/Documents/Edit.cshtml View File

@ -1,4 +1,4 @@
@page "{id:int}"
@page "{id}"
@model Snootalogue.Pages.Documents.EditModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

+ 1
- 1
Pages/Documents/Edit.cshtml.cs View File

@ -17,7 +17,7 @@ namespace Snootalogue.Pages.Documents {
_context = context;
}
public async Task<IActionResult> OnGetAsync(int? id) {
public async Task<IActionResult> OnGetAsync(string id) {
if (id == null) {
return NotFound();
}

+ 1
- 1
Pages/Documents/View.cshtml View File

@ -1,4 +1,4 @@
@page "{id:int}"
@page "{id}"
@model Snootalogue.Pages.Documents.ViewModel
@{

+ 1
- 1
Pages/Documents/View.cshtml.cs View File

@ -14,7 +14,7 @@ namespace Snootalogue.Pages.Documents {
_context = context;
}
public async Task<IActionResult> OnGetAsync(int? id) {
public async Task<IActionResult> OnGetAsync(string id) {
if (id == null) {
return NotFound();
}

+ 1
- 0
Pages/Upload.cshtml.cs View File

@ -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<string>(),
Category = ud.Category,

+ 1
- 0
README.md View File

@ -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

+ 1
- 1
ViewModels/EditDocument.cs View File

@ -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]

Loading…
Cancel
Save